xref: /btstack/src/hci.c (revision 66da704455bcb02e24a92afbbe86e8528539fdbc)
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
89e5780900Smatthias.ringwald #ifdef HAVE_TICK
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
103e5780900Smatthias.ringwald #ifdef HAVE_TICK
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 
212c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2132b12a0b9Smatthias.ringwald 
2142b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2152b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2162b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2172b12a0b9Smatthias.ringwald             return 0;
2182b12a0b9Smatthias.ringwald         }
2192b12a0b9Smatthias.ringwald     }
2202b12a0b9Smatthias.ringwald 
2212b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
222c24735b1Smatthias.ringwald     switch (packet_type) {
223c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
224c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
225c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
226de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
227c24735b1Smatthias.ringwald         default:
228c24735b1Smatthias.ringwald             return 0;
229c24735b1Smatthias.ringwald     }
230c24735b1Smatthias.ringwald }
231c24735b1Smatthias.ringwald 
232ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2337856c818Smatthias.ringwald 
2346218e6f1Smatthias.ringwald     // check for free places on BT module
2355932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2366218e6f1Smatthias.ringwald 
2377856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2387856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
23956cf178bSmatthias.ringwald     if (!connection) return 0;
24056cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
24156cf178bSmatthias.ringwald 
24256cf178bSmatthias.ringwald     // count packet
24356cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2447b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2457856c818Smatthias.ringwald 
24600d8e42eSmatthias.ringwald     // send packet
24700d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2486218e6f1Smatthias.ringwald 
24900d8e42eSmatthias.ringwald     return err;
250ee091cf1Smatthias.ringwald }
251ee091cf1Smatthias.ringwald 
25216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2537856c818Smatthias.ringwald 
2547856c818Smatthias.ringwald     // get info
2557856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2567856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2577856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2587856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2597856c818Smatthias.ringwald 
2607856c818Smatthias.ringwald     // ignore non-registered handle
2617856c818Smatthias.ringwald     if (!conn){
2627d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2637856c818Smatthias.ringwald         return;
2647856c818Smatthias.ringwald     }
2657856c818Smatthias.ringwald 
2667856c818Smatthias.ringwald     // update idle timestamp
2677856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2687856c818Smatthias.ringwald 
2697856c818Smatthias.ringwald     // handle different packet types
2707856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2717856c818Smatthias.ringwald 
2727856c818Smatthias.ringwald         case 0x01: // continuation fragment
2737856c818Smatthias.ringwald 
2747856c818Smatthias.ringwald             // sanity check
2757856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2767d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2777856c818Smatthias.ringwald                 return;
2787856c818Smatthias.ringwald             }
2797856c818Smatthias.ringwald 
2807856c818Smatthias.ringwald             // append fragment payload (header already stored)
2817856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2827856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2837856c818Smatthias.ringwald 
2847d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
285decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
2867856c818Smatthias.ringwald 
2877856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2887856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2897856c818Smatthias.ringwald 
2902718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2917856c818Smatthias.ringwald                 // reset recombination buffer
2927856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2937856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2947856c818Smatthias.ringwald             }
2957856c818Smatthias.ringwald             break;
2967856c818Smatthias.ringwald 
2977856c818Smatthias.ringwald         case 0x02: { // first fragment
2987856c818Smatthias.ringwald 
2997856c818Smatthias.ringwald             // sanity check
3007856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3017d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3027856c818Smatthias.ringwald                 return;
3037856c818Smatthias.ringwald             }
3047856c818Smatthias.ringwald 
3057856c818Smatthias.ringwald             // peek into L2CAP packet!
3067856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3077856c818Smatthias.ringwald 
3087d67539fSmatthias.ringwald             // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
309decc01a8Smatthias.ringwald 
3107856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3117856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3127856c818Smatthias.ringwald 
3137856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3142718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3157856c818Smatthias.ringwald 
3167856c818Smatthias.ringwald             } else {
3177856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3187856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3197856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3207856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
321decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3227856c818Smatthias.ringwald             }
3237856c818Smatthias.ringwald             break;
3247856c818Smatthias.ringwald 
3257856c818Smatthias.ringwald         }
3267856c818Smatthias.ringwald         default:
3277d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3287856c818Smatthias.ringwald             return;
3297856c818Smatthias.ringwald     }
33094ab26f8Smatthias.ringwald 
33194ab26f8Smatthias.ringwald     // execute main loop
33294ab26f8Smatthias.ringwald     hci_run();
33316833f0aSmatthias.ringwald }
33422909952Smatthias.ringwald 
33567a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
336339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3373c4d4b90Smatthias.ringwald 
3383c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3393c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3403c4d4b90Smatthias.ringwald 
341c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
342c785ef68Smatthias.ringwald 
343c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
344a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3453c4d4b90Smatthias.ringwald 
3463c4d4b90Smatthias.ringwald     // now it's gone
347c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
348c7e0c5f6Smatthias.ringwald }
349c7e0c5f6Smatthias.ringwald 
3500c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3518f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3528f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3538f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3548f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3558f8108aaSmatthias.ringwald };
3568f8108aaSmatthias.ringwald 
3578f8108aaSmatthias.ringwald static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){
3588f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
3598f8108aaSmatthias.ringwald     int i;
3608f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
3618f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
3628f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
3638f8108aaSmatthias.ringwald             packet_types |= 1 << i;
3648f8108aaSmatthias.ringwald         }
3658f8108aaSmatthias.ringwald     }
3668f8108aaSmatthias.ringwald     // flip bits for "may not be used"
3678f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
3688f8108aaSmatthias.ringwald     return packet_types;
3698f8108aaSmatthias.ringwald }
3708f8108aaSmatthias.ringwald 
3718f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
3728f8108aaSmatthias.ringwald     return hci_stack.packet_types;
3738f8108aaSmatthias.ringwald }
3748f8108aaSmatthias.ringwald 
3757dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
3767dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
3777dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
3787dc17943Smatthias.ringwald }
3797dc17943Smatthias.ringwald 
3807dc17943Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
3817dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
3827dc17943Smatthias.ringwald }
3837dc17943Smatthias.ringwald 
38474ec757aSmatthias.ringwald // avoid huge local variables
385223aafc1Smatthias.ringwald #ifndef EMBEDDED
38674ec757aSmatthias.ringwald static device_name_t device_name;
387223aafc1Smatthias.ringwald #endif
38816833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3891281a47eSmatthias.ringwald     bd_addr_t addr;
3902d00edd4Smatthias.ringwald     uint8_t link_type;
391fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3921f7b95a1Smatthias.ringwald     hci_connection_t * conn;
39356cf178bSmatthias.ringwald     int i;
39422909952Smatthias.ringwald 
3956772a24cSmatthias.ringwald     switch (packet[0]) {
39622909952Smatthias.ringwald 
3976772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
3987ec5eeaaSmatthias.ringwald             // get num cmd packets
3997b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4007ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4017ec5eeaaSmatthias.ringwald 
402e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
403e2edc0c3Smatthias.ringwald                 // from offset 5
404e2edc0c3Smatthias.ringwald                 // status
4051d279b20Smatthias.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"
406e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
40756cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
408e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
40956cf178bSmatthias.ringwald                 // ignore: total num SCO packets
41056cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
411a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
4128fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
4138fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
4148f8108aaSmatthias.ringwald                     }
415a7a04bd9Smatthias.ringwald                     // determine usable ACL packet types
41628c93ceeSmatthias.ringwald                     hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length);
4178f8108aaSmatthias.ringwald 
4188fcba05dSmatthias.ringwald                     log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n",
4198f8108aaSmatthias.ringwald                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types);
420e2edc0c3Smatthias.ringwald                 }
42156cf178bSmatthias.ringwald             }
422381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
423381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
424381fbed8Smatthias.ringwald             }
42556cf178bSmatthias.ringwald             break;
42656cf178bSmatthias.ringwald 
4277ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
4287ec5eeaaSmatthias.ringwald             // get num cmd packets
4297b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
4307ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
4317ec5eeaaSmatthias.ringwald             break;
4327ec5eeaaSmatthias.ringwald 
43356cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
43456cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
43556cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
43656cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
43756cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
43856cf178bSmatthias.ringwald                 if (!conn){
4397d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
44056cf178bSmatthias.ringwald                     continue;
44156cf178bSmatthias.ringwald                 }
44256cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4437b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
44456cf178bSmatthias.ringwald             }
4456772a24cSmatthias.ringwald             break;
4466772a24cSmatthias.ringwald 
4471f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
44837eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
44937eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4502d00edd4Smatthias.ringwald             link_type = packet[11];
451339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
45237eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4531f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4541f7b95a1Smatthias.ringwald                 if (!conn) {
4551f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4561f7b95a1Smatthias.ringwald                 }
457ce4c8fabSmatthias.ringwald                 if (!conn) {
458ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
459ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
460ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
461ce4c8fabSmatthias.ringwald                     break;
462ce4c8fabSmatthias.ringwald                 }
46332ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
46432ab9390Smatthias.ringwald                 hci_run();
46537eaa4cfSmatthias.ringwald             } else {
466ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
467ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
468ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
46937eaa4cfSmatthias.ringwald             }
4701f7b95a1Smatthias.ringwald             break;
4711f7b95a1Smatthias.ringwald 
4726772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
473fe1ed1b8Smatthias.ringwald             // Connection management
474fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
475339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
4761f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
477fe1ed1b8Smatthias.ringwald             if (conn) {
478b448a0e7Smatthias.ringwald                 if (!packet[2]){
479c8e4258aSmatthias.ringwald                     conn->state = OPEN;
480fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
481ee091cf1Smatthias.ringwald 
482c785ef68Smatthias.ringwald                     // restart timer
483c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
484ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
485c785ef68Smatthias.ringwald 
486339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
48743bfb1bdSmatthias.ringwald 
48843bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
489b448a0e7Smatthias.ringwald                 } else {
490b448a0e7Smatthias.ringwald                     // connection failed, remove entry
491b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
492a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
493c12e46e7Smatthias.ringwald 
494c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
495c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
496c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
497c12e46e7Smatthias.ringwald                     }
498fe1ed1b8Smatthias.ringwald                 }
499fe1ed1b8Smatthias.ringwald             }
5006772a24cSmatthias.ringwald             break;
501fe1ed1b8Smatthias.ringwald 
5027fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
5037b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
5047fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
50574ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
50632ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
50764472d52Smatthias.ringwald             hci_run();
50829d53098Smatthias.ringwald             // request already answered
50929d53098Smatthias.ringwald             return;
5107fde4af9Smatthias.ringwald 
5117fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
5127fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
51374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
51429d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
515287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
51629d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
5177fde4af9Smatthias.ringwald             break;
5187fde4af9Smatthias.ringwald 
5197fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
5207fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
5217fde4af9Smatthias.ringwald             break;
5227fde4af9Smatthias.ringwald 
523223aafc1Smatthias.ringwald #ifndef EMBEDDED
52474ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
52574ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
52674ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
52774ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
5285a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
5295a06394aSmatthias.ringwald             for (i=0; i<248;i++){
5305a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
5315a06394aSmatthias.ringwald                     packet[9+i] = 0;
5325a06394aSmatthias.ringwald                     break;
533cdc9101dSmatthias.ringwald                 }
5345a06394aSmatthias.ringwald             }
535d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
53674ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
53774ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
53874ec757aSmatthias.ringwald             break;
53974ec757aSmatthias.ringwald 
54074ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
54174ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
54274ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
54374ec757aSmatthias.ringwald             // first send inq result packet
54474ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
54574ec757aSmatthias.ringwald             // then send cached remote names
54674ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
54774ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
54874ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
54974ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
55074ec757aSmatthias.ringwald                 }
55174ec757aSmatthias.ringwald             }
55274ec757aSmatthias.ringwald             return;
553223aafc1Smatthias.ringwald #endif
55474ec757aSmatthias.ringwald 
5556772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
556fe1ed1b8Smatthias.ringwald             if (!packet[2]){
557fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
558fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
559fe1ed1b8Smatthias.ringwald                 if (conn) {
560c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
561fe1ed1b8Smatthias.ringwald                 }
562fe1ed1b8Smatthias.ringwald             }
5636772a24cSmatthias.ringwald             break;
5646772a24cSmatthias.ringwald 
565c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
566c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
567c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
568c68bdf90Smatthias.ringwald             }
569c68bdf90Smatthias.ringwald             break;
570c68bdf90Smatthias.ringwald 
5716772a24cSmatthias.ringwald         default:
5726772a24cSmatthias.ringwald             break;
573fe1ed1b8Smatthias.ringwald     }
574fe1ed1b8Smatthias.ringwald 
5753429f56bSmatthias.ringwald     // handle BT initialization
5763429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
5777301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
5787301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
5797301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
5807301ad89Smatthias.ringwald         // }
5817301ad89Smatthias.ringwald         // handle normal init sequence
5823429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
5833429f56bSmatthias.ringwald             // odd: waiting for event
5843429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5853429f56bSmatthias.ringwald                 hci_stack.substate++;
5863429f56bSmatthias.ringwald             }
5873429f56bSmatthias.ringwald         }
58822909952Smatthias.ringwald     }
58922909952Smatthias.ringwald 
59089db417bSmatthias.ringwald     // help with BT sleep
59189db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
59289db417bSmatthias.ringwald         && hci_stack.substate == 1
59389db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
59489db417bSmatthias.ringwald         hci_stack.substate++;
59589db417bSmatthias.ringwald     }
59689db417bSmatthias.ringwald 
5972718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
59894ab26f8Smatthias.ringwald 
59994ab26f8Smatthias.ringwald 	// execute main loop
60094ab26f8Smatthias.ringwald 	hci_run();
60116833f0aSmatthias.ringwald }
60216833f0aSmatthias.ringwald 
60310e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
60410e830c9Smatthias.ringwald     switch (packet_type) {
60510e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
60610e830c9Smatthias.ringwald             event_handler(packet, size);
60710e830c9Smatthias.ringwald             break;
60810e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
60910e830c9Smatthias.ringwald             acl_handler(packet, size);
61010e830c9Smatthias.ringwald             break;
61110e830c9Smatthias.ringwald         default:
61210e830c9Smatthias.ringwald             break;
61310e830c9Smatthias.ringwald     }
61410e830c9Smatthias.ringwald }
61510e830c9Smatthias.ringwald 
616fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
6172718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
6182718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
61916833f0aSmatthias.ringwald }
62016833f0aSmatthias.ringwald 
621404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
622475c8125Smatthias.ringwald 
623475c8125Smatthias.ringwald     // reference to use transport layer implementation
62416833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
625475c8125Smatthias.ringwald 
62611e23e5fSmatthias.ringwald     // references to used control implementation
62711e23e5fSmatthias.ringwald     hci_stack.control = control;
62811e23e5fSmatthias.ringwald 
62911e23e5fSmatthias.ringwald     // reference to used config
63011e23e5fSmatthias.ringwald     hci_stack.config = config;
63111e23e5fSmatthias.ringwald 
632fe1ed1b8Smatthias.ringwald     // no connections yet
633fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
6340a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
635fe1ed1b8Smatthias.ringwald 
636b031bebbSmatthias.ringwald     // no pending cmds
637b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
638b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
639b031bebbSmatthias.ringwald 
64016833f0aSmatthias.ringwald     // higher level handler
6412718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
64216833f0aSmatthias.ringwald 
643404843c1Smatthias.ringwald     // store and open remote device db
644404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
645404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
646404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
647404843c1Smatthias.ringwald     }
64829d53098Smatthias.ringwald 
6498fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
6508fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
6518fcba05dSmatthias.ringwald 
65216833f0aSmatthias.ringwald     // register packet handlers with transport
65310e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
654475c8125Smatthias.ringwald }
655475c8125Smatthias.ringwald 
656404843c1Smatthias.ringwald void hci_close(){
657404843c1Smatthias.ringwald     // close remote device db
658404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
659404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
660404843c1Smatthias.ringwald     }
661404843c1Smatthias.ringwald }
662404843c1Smatthias.ringwald 
6638d213e1aSmatthias.ringwald // State-Module-Driver overview
6648d213e1aSmatthias.ringwald // state                    module  low-level
6658d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
6668d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
6678d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
6688d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
669d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
670d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
671c7e0c5f6Smatthias.ringwald 
67240d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
6737301ad89Smatthias.ringwald 
674038bc64cSmatthias.ringwald     // power on
675f9a30166Smatthias.ringwald     int err = 0;
676f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
677f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
678f9a30166Smatthias.ringwald     }
679038bc64cSmatthias.ringwald     if (err){
6807d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
681038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
682038bc64cSmatthias.ringwald         return err;
683038bc64cSmatthias.ringwald     }
684038bc64cSmatthias.ringwald 
685038bc64cSmatthias.ringwald     // open low-level device
686038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
687038bc64cSmatthias.ringwald     if (err){
6887d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
689f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
690f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
691f9a30166Smatthias.ringwald         }
692038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
693038bc64cSmatthias.ringwald         return err;
694038bc64cSmatthias.ringwald     }
6958d213e1aSmatthias.ringwald     return 0;
6968d213e1aSmatthias.ringwald }
697038bc64cSmatthias.ringwald 
69840d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
6998d213e1aSmatthias.ringwald 
7007b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
7019418f9c9Smatthias.ringwald 
7028d213e1aSmatthias.ringwald     // close low-level device
7038d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
7048d213e1aSmatthias.ringwald 
7057b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
7069418f9c9Smatthias.ringwald 
7078d213e1aSmatthias.ringwald     // power off
7088d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
7098d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
7108d213e1aSmatthias.ringwald     }
7119418f9c9Smatthias.ringwald 
7127b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
7139418f9c9Smatthias.ringwald 
71472ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
71572ea5239Smatthias.ringwald }
71672ea5239Smatthias.ringwald 
71740d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
71872ea5239Smatthias.ringwald 
7197b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
7203144bce4Smatthias.ringwald 
721b429b9b7Smatthias.ringwald #if 0
722b429b9b7Smatthias.ringwald     // don't close serial port during sleep
723b429b9b7Smatthias.ringwald 
72472ea5239Smatthias.ringwald     // close low-level device
72572ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
726b429b9b7Smatthias.ringwald #endif
72772ea5239Smatthias.ringwald 
72872ea5239Smatthias.ringwald     // sleep mode
7293144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
73072ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
73172ea5239Smatthias.ringwald     }
732b429b9b7Smatthias.ringwald 
73372ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
7348d213e1aSmatthias.ringwald }
7358d213e1aSmatthias.ringwald 
73640d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
737b429b9b7Smatthias.ringwald 
7387b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
739b429b9b7Smatthias.ringwald 
740b429b9b7Smatthias.ringwald     // wake on
741b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
742b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
743b429b9b7Smatthias.ringwald     }
744b429b9b7Smatthias.ringwald 
745b429b9b7Smatthias.ringwald #if 0
746b429b9b7Smatthias.ringwald     // open low-level device
747b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
748b429b9b7Smatthias.ringwald     if (err){
7497d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
750b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
751b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
752b429b9b7Smatthias.ringwald         }
753b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
754b429b9b7Smatthias.ringwald         return err;
755b429b9b7Smatthias.ringwald     }
756b429b9b7Smatthias.ringwald #endif
757b429b9b7Smatthias.ringwald 
758b429b9b7Smatthias.ringwald     return 0;
759b429b9b7Smatthias.ringwald }
760b429b9b7Smatthias.ringwald 
761b429b9b7Smatthias.ringwald 
7628d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
763d661ed19Smatthias.ringwald 
7647b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
765d661ed19Smatthias.ringwald 
7668d213e1aSmatthias.ringwald     int err = 0;
7678d213e1aSmatthias.ringwald     switch (hci_stack.state){
7688d213e1aSmatthias.ringwald 
7698d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
7708d213e1aSmatthias.ringwald             switch (power_mode){
7718d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7728d213e1aSmatthias.ringwald                     err = hci_power_control_on();
7738d213e1aSmatthias.ringwald                     if (err) return err;
7747301ad89Smatthias.ringwald                     // set up state machine
7757301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
7767301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7777301ad89Smatthias.ringwald                     hci_stack.substate = 0;
7788d213e1aSmatthias.ringwald                     break;
7798d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7808d213e1aSmatthias.ringwald                     // do nothing
7818d213e1aSmatthias.ringwald                     break;
7828d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
783b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
7848d213e1aSmatthias.ringwald                     break;
7858d213e1aSmatthias.ringwald             }
7868d213e1aSmatthias.ringwald             break;
7877301ad89Smatthias.ringwald 
7888d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7898d213e1aSmatthias.ringwald             switch (power_mode){
7908d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7918d213e1aSmatthias.ringwald                     // do nothing
7928d213e1aSmatthias.ringwald                     break;
7938d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7948d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
7958d213e1aSmatthias.ringwald                     hci_power_control_off();
7968d213e1aSmatthias.ringwald                     break;
7978d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
798b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
79972ea5239Smatthias.ringwald                     hci_power_control_sleep();
8008d213e1aSmatthias.ringwald                     break;
8018d213e1aSmatthias.ringwald             }
8028d213e1aSmatthias.ringwald             break;
8037301ad89Smatthias.ringwald 
8048d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
8058d213e1aSmatthias.ringwald             switch (power_mode){
8068d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8078d213e1aSmatthias.ringwald                     // do nothing
8088d213e1aSmatthias.ringwald                     break;
8098d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
810c7e0c5f6Smatthias.ringwald                     // see hci_run
811c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8128d213e1aSmatthias.ringwald                     break;
8138d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
814b546ac54Smatthias.ringwald                     // see hci_run
815b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
81689db417bSmatthias.ringwald                     hci_stack.substate = 0;
8178d213e1aSmatthias.ringwald                     break;
8188d213e1aSmatthias.ringwald             }
8198d213e1aSmatthias.ringwald             break;
8207301ad89Smatthias.ringwald 
8218d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
8228d213e1aSmatthias.ringwald             switch (power_mode){
8238d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8248d213e1aSmatthias.ringwald                     // set up state machine
8258d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8268d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
8278d213e1aSmatthias.ringwald                     break;
8288d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8298d213e1aSmatthias.ringwald                     // do nothing
8308d213e1aSmatthias.ringwald                     break;
8318d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
832b546ac54Smatthias.ringwald                     // see hci_run
833b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
83489db417bSmatthias.ringwald                     hci_stack.substate = 0;
8358d213e1aSmatthias.ringwald                     break;
8368d213e1aSmatthias.ringwald             }
8378d213e1aSmatthias.ringwald             break;
8388d213e1aSmatthias.ringwald 
8398d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
8408d213e1aSmatthias.ringwald             switch (power_mode){
8418d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
842b546ac54Smatthias.ringwald                     // set up state machine
843b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
844b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
845b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8468d213e1aSmatthias.ringwald                     break;
8478d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
848b546ac54Smatthias.ringwald                     // see hci_run
849b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8508d213e1aSmatthias.ringwald                     break;
8518d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
852b546ac54Smatthias.ringwald                     // do nothing
8538d213e1aSmatthias.ringwald                     break;
8548d213e1aSmatthias.ringwald             }
8558d213e1aSmatthias.ringwald             break;
8568d213e1aSmatthias.ringwald 
8578d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
8588d213e1aSmatthias.ringwald             switch (power_mode){
8598d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8603144bce4Smatthias.ringwald                     err = hci_power_control_wake();
8613144bce4Smatthias.ringwald                     if (err) return err;
862b546ac54Smatthias.ringwald                     // set up state machine
863b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
864b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
865b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8668d213e1aSmatthias.ringwald                     break;
8678d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8684ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8698d213e1aSmatthias.ringwald                     break;
8708d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
871b546ac54Smatthias.ringwald                     // do nothing
8728d213e1aSmatthias.ringwald                     break;
8738d213e1aSmatthias.ringwald             }
8748d213e1aSmatthias.ringwald             break;
87511e23e5fSmatthias.ringwald     }
87668d92d03Smatthias.ringwald 
877038bc64cSmatthias.ringwald     // create internal event
878ee8bf225Smatthias.ringwald 	hci_emit_state();
879ee8bf225Smatthias.ringwald 
88068d92d03Smatthias.ringwald 	// trigger next/first action
88168d92d03Smatthias.ringwald 	hci_run();
88268d92d03Smatthias.ringwald 
883475c8125Smatthias.ringwald     return 0;
884475c8125Smatthias.ringwald }
885475c8125Smatthias.ringwald 
886381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
887381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
888381fbed8Smatthias.ringwald 
889381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
890381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
891381fbed8Smatthias.ringwald         return;
892381fbed8Smatthias.ringwald     }
893381fbed8Smatthias.ringwald 
894b031bebbSmatthias.ringwald     // store request to send command but accept in higher layer view
895b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 2 | enable; // 1 = inq scan, 2 = page scan
896381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
897b031bebbSmatthias.ringwald 
898b031bebbSmatthias.ringwald     hci_run();
899381fbed8Smatthias.ringwald }
900381fbed8Smatthias.ringwald 
90106b35ec0Smatthias.ringwald void hci_run(){
9028a485f27Smatthias.ringwald 
90332ab9390Smatthias.ringwald     hci_connection_t * connection;
90432ab9390Smatthias.ringwald     linked_item_t * it;
90532ab9390Smatthias.ringwald 
906ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
907ce4c8fabSmatthias.ringwald 
908b031bebbSmatthias.ringwald     // global/non-connection oriented commands
909b031bebbSmatthias.ringwald 
910b031bebbSmatthias.ringwald     // decline incoming connections
911ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
912ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
913ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
914ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
915ce4c8fabSmatthias.ringwald     }
916ce4c8fabSmatthias.ringwald 
917b031bebbSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
918b031bebbSmatthias.ringwald 
919b031bebbSmatthias.ringwald     // send scan enable
920eda18bf8Smatthias.ringwald     if (hci_stack.new_scan_enable_value != 0xff){
921b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
922b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
923b031bebbSmatthias.ringwald     }
924b031bebbSmatthias.ringwald 
92532ab9390Smatthias.ringwald     // send pending HCI commands
92632ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
92732ab9390Smatthias.ringwald 
928b031bebbSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
92932ab9390Smatthias.ringwald 
930b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
93132ab9390Smatthias.ringwald 
93232ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
9337b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
93432ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
93532ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
936c7e0c5f6Smatthias.ringwald         }
937c7e0c5f6Smatthias.ringwald 
938de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
93932ab9390Smatthias.ringwald 
94032ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
94132ab9390Smatthias.ringwald             link_key_t link_key;
9427b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
94332ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
94432ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
94532ab9390Smatthias.ringwald             } else {
94632ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
94732ab9390Smatthias.ringwald             }
94832ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
94932ab9390Smatthias.ringwald         }
95032ab9390Smatthias.ringwald     }
95132ab9390Smatthias.ringwald 
952de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
953c7e0c5f6Smatthias.ringwald 
9543429f56bSmatthias.ringwald     switch (hci_stack.state){
9553429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
9567b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
9573429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
9583429f56bSmatthias.ringwald                 // odd: waiting for command completion
95906b35ec0Smatthias.ringwald                 return;
9603429f56bSmatthias.ringwald             }
96190919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
962c7492964Smatthias.ringwald                 case 0: // RESET
96322909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
964c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
965c7492964Smatthias.ringwald                         // skip baud change
966c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
967c7492964Smatthias.ringwald                     }
9683429f56bSmatthias.ringwald                     break;
969c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
9707dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
9717dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
972c7492964Smatthias.ringwald                     break;
973c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
974c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
975c7492964Smatthias.ringwald                     hci_stack.substate += 2;
976c7492964Smatthias.ringwald                     // break missing here for fall through
977c7492964Smatthias.ringwald 
978c7492964Smatthias.ringwald                 case 3:
97990919203Smatthias.ringwald                     // custom initialization
980db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
9817dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
982d62aca9fSmatthias.ringwald                         if (valid_cmd){
9837dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
9847dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
985c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
98690919203Smatthias.ringwald                             break;
98790919203Smatthias.ringwald                         }
988d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
98990919203Smatthias.ringwald                     }
9902f6c30e1Smatthias.ringwald                     // otherwise continue
991f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
992f432a6ddSmatthias.ringwald 					break;
993c7492964Smatthias.ringwald 				case 4:
994e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
995e2edc0c3Smatthias.ringwald 					break;
996c7492964Smatthias.ringwald                 case 5:
9973429f56bSmatthias.ringwald                     // ca. 15 sec
9983429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
9993429f56bSmatthias.ringwald                     break;
1000c7492964Smatthias.ringwald 				case 6:
10010a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
1002f432a6ddSmatthias.ringwald 					break;
1003c7492964Smatthias.ringwald                 case 7:
10048a485f27Smatthias.ringwald #ifndef EMBEDDED
10058a485f27Smatthias.ringwald                 {
10068a485f27Smatthias.ringwald                     char hostname[30];
10078a485f27Smatthias.ringwald                     gethostname(hostname, 30);
10088a485f27Smatthias.ringwald                     hostname[29] = '\0';
10098a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
10108a485f27Smatthias.ringwald                     break;
10118a485f27Smatthias.ringwald                 }
1012c7492964Smatthias.ringwald                 case 8:
10133c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
10143c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
10153c4d4b90Smatthias.ringwald                     break;
10163c4d4b90Smatthias.ringwald 
1017c7492964Smatthias.ringwald                 case 9:
10183c4d4b90Smatthias.ringwald #endif
10198a485f27Smatthias.ringwald #endif
10203429f56bSmatthias.ringwald                     // done.
10213429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1022b360b6adSmatthias.ringwald                     hci_emit_state();
10233429f56bSmatthias.ringwald                     break;
10243429f56bSmatthias.ringwald                 default:
10253429f56bSmatthias.ringwald                     break;
1026475c8125Smatthias.ringwald             }
10273429f56bSmatthias.ringwald             hci_stack.substate++;
10283429f56bSmatthias.ringwald             break;
1029c7e0c5f6Smatthias.ringwald 
1030c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1031c7e0c5f6Smatthias.ringwald 
10327b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1033c7e0c5f6Smatthias.ringwald             // close all open connections
1034c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1035c7e0c5f6Smatthias.ringwald             if (connection){
103632ab9390Smatthias.ringwald 
1037c7e0c5f6Smatthias.ringwald                 // send disconnect
103832ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
103932ab9390Smatthias.ringwald 
10407b5fbe1fSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
10416ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1042c7e0c5f6Smatthias.ringwald 
1043c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1044c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1045c7e0c5f6Smatthias.ringwald                 return;
1046c7e0c5f6Smatthias.ringwald             }
10477b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1048c7e0c5f6Smatthias.ringwald 
104972ea5239Smatthias.ringwald             // switch mode
1050c7e0c5f6Smatthias.ringwald             hci_power_control_off();
10519418f9c9Smatthias.ringwald 
10527b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
105372ea5239Smatthias.ringwald             hci_emit_state();
10547b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
105572ea5239Smatthias.ringwald             break;
1056c7e0c5f6Smatthias.ringwald 
105772ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
105889db417bSmatthias.ringwald             switch(hci_stack.substate) {
105989db417bSmatthias.ringwald                 case 0:
10607b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
106172ea5239Smatthias.ringwald                     // close all open connections
106272ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
1063*66da7044Smatthias.ringwald 
1064*66da7044Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(BLUETOOL)
1065*66da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
1066*66da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
1067*66da7044Smatthias.ringwald                         connection = NULL;
1068*66da7044Smatthias.ringwald                     }
1069*66da7044Smatthias.ringwald #endif
107072ea5239Smatthias.ringwald                     if (connection){
107132ab9390Smatthias.ringwald 
107272ea5239Smatthias.ringwald                         // send disconnect
107332ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
107432ab9390Smatthias.ringwald 
10757b5fbe1fSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
10766ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
107772ea5239Smatthias.ringwald 
107872ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
107972ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
108072ea5239Smatthias.ringwald                         return;
108172ea5239Smatthias.ringwald                     }
108272ea5239Smatthias.ringwald 
108389db417bSmatthias.ringwald                     // disable page and inquiry scan
108432ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
108532ab9390Smatthias.ringwald 
10867b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq & page scans\n");
108789db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
108889db417bSmatthias.ringwald 
108989db417bSmatthias.ringwald                     // continue in next sub state
109089db417bSmatthias.ringwald                     hci_stack.substate++;
109189db417bSmatthias.ringwald                     break;
109289db417bSmatthias.ringwald                 case 1:
109389db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
109489db417bSmatthias.ringwald                     break;
109589db417bSmatthias.ringwald                 case 2:
10967b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
109772ea5239Smatthias.ringwald                     // switch mode
109889db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1099c7e0c5f6Smatthias.ringwald                     hci_emit_state();
110089db417bSmatthias.ringwald                 default:
110189db417bSmatthias.ringwald                     break;
110289db417bSmatthias.ringwald             }
1103c7e0c5f6Smatthias.ringwald             break;
1104c7e0c5f6Smatthias.ringwald 
11053429f56bSmatthias.ringwald         default:
11063429f56bSmatthias.ringwald             break;
11071f504dbdSmatthias.ringwald     }
11083429f56bSmatthias.ringwald }
110916833f0aSmatthias.ringwald 
111031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1111c8e4258aSmatthias.ringwald     bd_addr_t addr;
1112c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1113c8e4258aSmatthias.ringwald     // house-keeping
1114c8e4258aSmatthias.ringwald 
1115c8e4258aSmatthias.ringwald     // create_connection?
1116c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1117c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1118339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1119c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1120c8e4258aSmatthias.ringwald         if (conn) {
1121c8e4258aSmatthias.ringwald             // if connection exists
1122c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
112317f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
112417f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1125c8e4258aSmatthias.ringwald             }
112617f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1127c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1128c8e4258aSmatthias.ringwald 
112917f1ba2aSmatthias.ringwald         }
1130c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
113117f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
113217f1ba2aSmatthias.ringwald         if (!conn){
113317f1ba2aSmatthias.ringwald             // notify client that alloc failed
113417f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
113517f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
113617f1ba2aSmatthias.ringwald         }
1137c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1138c8e4258aSmatthias.ringwald     }
1139c8e4258aSmatthias.ringwald 
11407fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
11417fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
11427fde4af9Smatthias.ringwald     }
11437fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
11447fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
11457fde4af9Smatthias.ringwald     }
11467fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
11477fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
11487fde4af9Smatthias.ringwald     }
11497fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
11507fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
11517fde4af9Smatthias.ringwald     }
11527fde4af9Smatthias.ringwald 
11538ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
11548ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
11558ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
11568ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
11578ef73945Smatthias.ringwald         }
11588ef73945Smatthias.ringwald     }
1159c8e4258aSmatthias.ringwald 
116031452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1161622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
116231452debSmatthias.ringwald }
11638adf0ddaSmatthias.ringwald 
11641cd208adSmatthias.ringwald /**
11651cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
11661cd208adSmatthias.ringwald  */
1167fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
11681cd208adSmatthias.ringwald     va_list argptr;
11691cd208adSmatthias.ringwald     va_start(argptr, cmd);
11707dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
11711cd208adSmatthias.ringwald     va_end(argptr);
11727dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
117393b8dc03Smatthias.ringwald }
1174c8e4258aSmatthias.ringwald 
1175ee091cf1Smatthias.ringwald // Create various non-HCI events.
1176ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1177ee091cf1Smatthias.ringwald 
1178c8e4258aSmatthias.ringwald void hci_emit_state(){
1179425d1371Smatthias.ringwald     uint8_t event[3];
118080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1181425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1182c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1183425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1184425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1185c8e4258aSmatthias.ringwald }
1186c8e4258aSmatthias.ringwald 
118717f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1188425d1371Smatthias.ringwald     uint8_t event[13];
1189c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1190425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
119117f1ba2aSmatthias.ringwald     event[2] = status;
1192c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1193c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1194c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1195c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1196425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1197425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1198c8e4258aSmatthias.ringwald }
1199c8e4258aSmatthias.ringwald 
12003c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1201425d1371Smatthias.ringwald     uint8_t event[6];
12023c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1203e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
12043c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
12053c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
12063c4d4b90Smatthias.ringwald     event[5] = reason;
1207425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1208425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
12093c4d4b90Smatthias.ringwald }
12103c4d4b90Smatthias.ringwald 
1211ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1212425d1371Smatthias.ringwald     uint8_t event[4];
121380d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1214425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1215ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1216425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1217425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1218ee091cf1Smatthias.ringwald }
121943bfb1bdSmatthias.ringwald 
122043bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1221425d1371Smatthias.ringwald     uint8_t event[3];
122280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1223425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
122443bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1225425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1226425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
122743bfb1bdSmatthias.ringwald }
1228038bc64cSmatthias.ringwald 
1229038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1230425d1371Smatthias.ringwald     uint8_t event[2];
123180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1232425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1233425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1234425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1235038bc64cSmatthias.ringwald }
12361b0e3922Smatthias.ringwald 
12371b0e3922Smatthias.ringwald 
12381b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1239425d1371Smatthias.ringwald     uint8_t event[6];
12401b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1241425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1242425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1243425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1244425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1245425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1246425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
12471b0e3922Smatthias.ringwald }
12481b0e3922Smatthias.ringwald 
12492ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1250425d1371Smatthias.ringwald     uint8_t event[3];
12512ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1252425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
12532ed6235cSmatthias.ringwald     event[2] = enabled;
1254425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1255e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
12562ed6235cSmatthias.ringwald }
1257627c2f45Smatthias.ringwald 
1258627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1259425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1260627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1261425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1262f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
12632f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1264f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1265425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1266425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1267627c2f45Smatthias.ringwald }
1268381fbed8Smatthias.ringwald 
1269381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1270425d1371Smatthias.ringwald     uint8_t event[3];
1271381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1272425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1273381fbed8Smatthias.ringwald     event[2] = enabled;
1274425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1275425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1276381fbed8Smatthias.ringwald }
1277