xref: /btstack/src/hci.c (revision a3b02b7188908b1a71b8df1fd01a63b7a3345906)
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 
49*a3b02b71Smatthias.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 
56*a3b02b71Smatthias.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 
79c7492964Smatthias.ringwald #ifdef HAVE_TIME
802b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
81ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
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);
87c21e6239Smatthias.ringwald         run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
88ee091cf1Smatthias.ringwald     } else {
89ee091cf1Smatthias.ringwald         // next timeout check at
90c21e6239Smatthias.ringwald         timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
91ee091cf1Smatthias.ringwald     }
92ee091cf1Smatthias.ringwald     run_loop_add_timer(timer);
93ee091cf1Smatthias.ringwald }
942b12a0b9Smatthias.ringwald #endif
95ee091cf1Smatthias.ringwald 
96ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
97c7492964Smatthias.ringwald #ifdef HAVE_TIME
98ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
99c7492964Smatthias.ringwald #endif
100ee091cf1Smatthias.ringwald }
101ee091cf1Smatthias.ringwald 
102ee091cf1Smatthias.ringwald /**
103c8e4258aSmatthias.ringwald  * create connection for given address
104c8e4258aSmatthias.ringwald  *
105c8e4258aSmatthias.ringwald  * @return connection OR NULL, if not found
106c8e4258aSmatthias.ringwald  */
107c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
108*a3b02b71Smatthias.ringwald     hci_connection_t * conn = btstack_memory_hci_connection_get();
109c8e4258aSmatthias.ringwald     if (!conn) return NULL;
110c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
111c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1127d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
113c7492964Smatthias.ringwald #ifdef HAVE_TIME
114ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
115ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
116ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
117c7492964Smatthias.ringwald #endif
118d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1197856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
12056cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
121c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
122c8e4258aSmatthias.ringwald     return conn;
123c8e4258aSmatthias.ringwald }
124c8e4258aSmatthias.ringwald 
125c8e4258aSmatthias.ringwald /**
12606b35ec0Smatthias.ringwald  * get connection for given address
12797addcc5Smatthias.ringwald  *
12897addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
12997addcc5Smatthias.ringwald  */
130fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
13106b35ec0Smatthias.ringwald     linked_item_t *it;
13206b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
13306b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
13406b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
13506b35ec0Smatthias.ringwald         }
13606b35ec0Smatthias.ringwald     }
13706b35ec0Smatthias.ringwald     return NULL;
13806b35ec0Smatthias.ringwald }
13906b35ec0Smatthias.ringwald 
14043bfb1bdSmatthias.ringwald /**
14180ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1427fde4af9Smatthias.ringwald  */
1437fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1447fde4af9Smatthias.ringwald     bd_addr_t addr;
1457fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1467fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1477fde4af9Smatthias.ringwald     if (conn) {
1487fde4af9Smatthias.ringwald         conn->authentication_flags |= flags;
14980ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1507fde4af9Smatthias.ringwald     }
1517fde4af9Smatthias.ringwald }
1527fde4af9Smatthias.ringwald 
15380ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
15480ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
15580ca58a0Smatthias.ringwald     if (!conn) return 0;
15680ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
15780ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
15880ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
15980ca58a0Smatthias.ringwald     return 1;
16080ca58a0Smatthias.ringwald }
16180ca58a0Smatthias.ringwald 
162c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
163c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
164c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
165c12e46e7Smatthias.ringwald     }
166c12e46e7Smatthias.ringwald }
167c12e46e7Smatthias.ringwald 
1687fde4af9Smatthias.ringwald 
1697fde4af9Smatthias.ringwald /**
17043bfb1bdSmatthias.ringwald  * count connections
17143bfb1bdSmatthias.ringwald  */
17240d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
17356c253c9Smatthias.ringwald     int count = 0;
17443bfb1bdSmatthias.ringwald     linked_item_t *it;
17543bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
17643bfb1bdSmatthias.ringwald     return count;
17743bfb1bdSmatthias.ringwald }
178c8e4258aSmatthias.ringwald 
17997addcc5Smatthias.ringwald /**
180ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
18116833f0aSmatthias.ringwald  */
1822718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
18316833f0aSmatthias.ringwald }
18416833f0aSmatthias.ringwald 
185998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
186998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
187998906cdSmatthias.ringwald     if (!connection) {
1887d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
189998906cdSmatthias.ringwald         return 0;
190998906cdSmatthias.ringwald     }
191998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
192998906cdSmatthias.ringwald }
193998906cdSmatthias.ringwald 
194998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
195998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
196998906cdSmatthias.ringwald     linked_item_t *it;
197998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
198998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
199998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2007d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
201998906cdSmatthias.ringwald             return 0;
202998906cdSmatthias.ringwald         }
203998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
204998906cdSmatthias.ringwald     }
205998906cdSmatthias.ringwald     return free_slots;
206998906cdSmatthias.ringwald }
207998906cdSmatthias.ringwald 
2085250fb9eSmatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
2095250fb9eSmatthias.ringwald     return hci_stack.acl_data_packet_length;
2105250fb9eSmatthias.ringwald }
2115250fb9eSmatthias.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 
2466218e6f1Smatthias.ringwald     // send packet - ignore errors
247622d0de9Smatthias.ringwald     hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2486218e6f1Smatthias.ringwald 
2496218e6f1Smatthias.ringwald     return 0;
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){
3367b5fbe1fSmatthias.ringwald     log_info("Connection closed: handle %u, ", conn->con_handle);
337c7e0c5f6Smatthias.ringwald     print_bd_addr( conn->address );
3387b5fbe1fSmatthias.ringwald     log_info("\n");
3393c4d4b90Smatthias.ringwald 
3403c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3413c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3423c4d4b90Smatthias.ringwald 
343c7492964Smatthias.ringwald #ifdef HAVE_TIME
344c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
345c7492964Smatthias.ringwald #endif
346c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
347*a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3483c4d4b90Smatthias.ringwald 
3493c4d4b90Smatthias.ringwald     // now it's gone
350c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
351c7e0c5f6Smatthias.ringwald }
352c7e0c5f6Smatthias.ringwald 
35374ec757aSmatthias.ringwald // avoid huge local variables
35474ec757aSmatthias.ringwald static device_name_t device_name;
35516833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3561281a47eSmatthias.ringwald     bd_addr_t addr;
3572d00edd4Smatthias.ringwald     uint8_t link_type;
358fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3591f7b95a1Smatthias.ringwald     hci_connection_t * conn;
36056cf178bSmatthias.ringwald     int i;
36122909952Smatthias.ringwald 
3626772a24cSmatthias.ringwald     switch (packet[0]) {
36322909952Smatthias.ringwald 
3646772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
3657ec5eeaaSmatthias.ringwald             // get num cmd packets
3667b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
3677ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
3687ec5eeaaSmatthias.ringwald 
369e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
370e2edc0c3Smatthias.ringwald                 // from offset 5
371e2edc0c3Smatthias.ringwald                 // status
3721d279b20Smatthias.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"
373e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
37456cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
375e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
37656cf178bSmatthias.ringwald                 // ignore: total num SCO packets
37756cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
3787b5fbe1fSmatthias.ringwald                     log_info("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
379e2edc0c3Smatthias.ringwald                 }
38056cf178bSmatthias.ringwald             }
381381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
382381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
383381fbed8Smatthias.ringwald             }
38456cf178bSmatthias.ringwald             break;
38556cf178bSmatthias.ringwald 
3867ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
3877ec5eeaaSmatthias.ringwald             // get num cmd packets
3887b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
3897ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
3907ec5eeaaSmatthias.ringwald             break;
3917ec5eeaaSmatthias.ringwald 
39256cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
39356cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
39456cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
39556cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
39656cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
39756cf178bSmatthias.ringwald                 if (!conn){
3987d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
39956cf178bSmatthias.ringwald                     continue;
40056cf178bSmatthias.ringwald                 }
40156cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4027b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
40356cf178bSmatthias.ringwald             }
4046772a24cSmatthias.ringwald             break;
4056772a24cSmatthias.ringwald 
4061f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
40737eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
40837eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4092d00edd4Smatthias.ringwald             link_type = packet[11];
4107b5fbe1fSmatthias.ringwald             log_info("Connection_incoming: "); print_bd_addr(addr); log_info(", type %u\n", link_type);
41137eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4121f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4131f7b95a1Smatthias.ringwald                 if (!conn) {
4141f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4151f7b95a1Smatthias.ringwald                 }
4161f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
41732ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
41832ab9390Smatthias.ringwald                 hci_run();
41937eaa4cfSmatthias.ringwald             } else {
42037eaa4cfSmatthias.ringwald                 // TODO: decline request
42137eaa4cfSmatthias.ringwald             }
4221f7b95a1Smatthias.ringwald             break;
4231f7b95a1Smatthias.ringwald 
4246772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
425fe1ed1b8Smatthias.ringwald             // Connection management
426fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
4277b5fbe1fSmatthias.ringwald             log_info("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_info("\n");
4281f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
429fe1ed1b8Smatthias.ringwald             if (conn) {
430b448a0e7Smatthias.ringwald                 if (!packet[2]){
431c8e4258aSmatthias.ringwald                     conn->state = OPEN;
432fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
433ee091cf1Smatthias.ringwald 
434c7492964Smatthias.ringwald #ifdef HAVE_TIME
435ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
436c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
437ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
438c7492964Smatthias.ringwald #endif
4397b5fbe1fSmatthias.ringwald                     log_info("New connection: handle %u, ", conn->con_handle);
440fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
4417b5fbe1fSmatthias.ringwald                     log_info("\n");
44243bfb1bdSmatthias.ringwald 
44343bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
444b448a0e7Smatthias.ringwald                 } else {
445b448a0e7Smatthias.ringwald                     // connection failed, remove entry
446b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
447*a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
448c12e46e7Smatthias.ringwald 
449c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
450c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
451c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
452c12e46e7Smatthias.ringwald                     }
453fe1ed1b8Smatthias.ringwald                 }
454fe1ed1b8Smatthias.ringwald             }
4556772a24cSmatthias.ringwald             break;
456fe1ed1b8Smatthias.ringwald 
4577fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
4587b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
4597fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
46074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
46132ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
46264472d52Smatthias.ringwald             hci_run();
46329d53098Smatthias.ringwald             // request already answered
46429d53098Smatthias.ringwald             return;
4657fde4af9Smatthias.ringwald 
4667fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
4677fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
46874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
46929d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
470287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
47129d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
4727fde4af9Smatthias.ringwald             break;
4737fde4af9Smatthias.ringwald 
4747fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4757fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
4767fde4af9Smatthias.ringwald             break;
4777fde4af9Smatthias.ringwald 
47874ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
47974ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
48074ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
48174ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
4825a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
4835a06394aSmatthias.ringwald             for (i=0; i<248;i++){
4845a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
4855a06394aSmatthias.ringwald                     packet[9+i] = 0;
4865a06394aSmatthias.ringwald                     break;
487cdc9101dSmatthias.ringwald                 }
4885a06394aSmatthias.ringwald             }
48974ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
49074ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
49174ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
49274ec757aSmatthias.ringwald             break;
49374ec757aSmatthias.ringwald 
49474ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
49574ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
49674ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
49774ec757aSmatthias.ringwald             // first send inq result packet
49874ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
49974ec757aSmatthias.ringwald             // then send cached remote names
50074ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
50174ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
50274ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
50374ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
50474ec757aSmatthias.ringwald                 }
50574ec757aSmatthias.ringwald             }
50674ec757aSmatthias.ringwald             return;
50774ec757aSmatthias.ringwald 
5086772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
509fe1ed1b8Smatthias.ringwald             if (!packet[2]){
510fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
511fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
512fe1ed1b8Smatthias.ringwald                 if (conn) {
513c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
514fe1ed1b8Smatthias.ringwald                 }
515fe1ed1b8Smatthias.ringwald             }
5166772a24cSmatthias.ringwald             break;
5176772a24cSmatthias.ringwald 
518c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
519c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
520c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
521c68bdf90Smatthias.ringwald             }
522c68bdf90Smatthias.ringwald             break;
523c68bdf90Smatthias.ringwald 
5246772a24cSmatthias.ringwald         default:
5256772a24cSmatthias.ringwald             break;
526fe1ed1b8Smatthias.ringwald     }
527fe1ed1b8Smatthias.ringwald 
5283429f56bSmatthias.ringwald     // handle BT initialization
5293429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
5307301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
5317301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
5327301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
5337301ad89Smatthias.ringwald         // }
5347301ad89Smatthias.ringwald         // handle normal init sequence
5353429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
5363429f56bSmatthias.ringwald             // odd: waiting for event
5373429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5383429f56bSmatthias.ringwald                 hci_stack.substate++;
5393429f56bSmatthias.ringwald             }
5403429f56bSmatthias.ringwald         }
54122909952Smatthias.ringwald     }
54222909952Smatthias.ringwald 
54389db417bSmatthias.ringwald     // help with BT sleep
54489db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
54589db417bSmatthias.ringwald         && hci_stack.substate == 1
54689db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
54789db417bSmatthias.ringwald         hci_stack.substate++;
54889db417bSmatthias.ringwald     }
54989db417bSmatthias.ringwald 
5502718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
55194ab26f8Smatthias.ringwald 
55294ab26f8Smatthias.ringwald 	// execute main loop
55394ab26f8Smatthias.ringwald 	hci_run();
55416833f0aSmatthias.ringwald }
55516833f0aSmatthias.ringwald 
55610e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
55710e830c9Smatthias.ringwald     switch (packet_type) {
55810e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
55910e830c9Smatthias.ringwald             event_handler(packet, size);
56010e830c9Smatthias.ringwald             break;
56110e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
56210e830c9Smatthias.ringwald             acl_handler(packet, size);
56310e830c9Smatthias.ringwald             break;
56410e830c9Smatthias.ringwald         default:
56510e830c9Smatthias.ringwald             break;
56610e830c9Smatthias.ringwald     }
56710e830c9Smatthias.ringwald }
56810e830c9Smatthias.ringwald 
569fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
5702718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
5712718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
57216833f0aSmatthias.ringwald }
57316833f0aSmatthias.ringwald 
574404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
575475c8125Smatthias.ringwald 
576475c8125Smatthias.ringwald     // reference to use transport layer implementation
57716833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
578475c8125Smatthias.ringwald 
57911e23e5fSmatthias.ringwald     // references to used control implementation
58011e23e5fSmatthias.ringwald     hci_stack.control = control;
58111e23e5fSmatthias.ringwald 
58211e23e5fSmatthias.ringwald     // reference to used config
58311e23e5fSmatthias.ringwald     hci_stack.config = config;
58411e23e5fSmatthias.ringwald 
585fe1ed1b8Smatthias.ringwald     // no connections yet
586fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
5870a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
588fe1ed1b8Smatthias.ringwald 
58916833f0aSmatthias.ringwald     // higher level handler
5902718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
59116833f0aSmatthias.ringwald 
592404843c1Smatthias.ringwald     // store and open remote device db
593404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
594404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
595404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
596404843c1Smatthias.ringwald     }
59729d53098Smatthias.ringwald 
59816833f0aSmatthias.ringwald     // register packet handlers with transport
59910e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
600475c8125Smatthias.ringwald }
601475c8125Smatthias.ringwald 
602404843c1Smatthias.ringwald void hci_close(){
603404843c1Smatthias.ringwald     // close remote device db
604404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
605404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
606404843c1Smatthias.ringwald     }
607404843c1Smatthias.ringwald }
608404843c1Smatthias.ringwald 
6098d213e1aSmatthias.ringwald // State-Module-Driver overview
6108d213e1aSmatthias.ringwald // state                    module  low-level
6118d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
6128d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
6138d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
6148d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
615d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
616d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
617c7e0c5f6Smatthias.ringwald 
61840d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
6197301ad89Smatthias.ringwald 
620038bc64cSmatthias.ringwald     // power on
621f9a30166Smatthias.ringwald     int err = 0;
622f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
623f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
624f9a30166Smatthias.ringwald     }
625038bc64cSmatthias.ringwald     if (err){
6267d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
627038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
628038bc64cSmatthias.ringwald         return err;
629038bc64cSmatthias.ringwald     }
630038bc64cSmatthias.ringwald 
631038bc64cSmatthias.ringwald     // open low-level device
632038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
633038bc64cSmatthias.ringwald     if (err){
6347d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
635f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
636f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
637f9a30166Smatthias.ringwald         }
638038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
639038bc64cSmatthias.ringwald         return err;
640038bc64cSmatthias.ringwald     }
6418d213e1aSmatthias.ringwald     return 0;
6428d213e1aSmatthias.ringwald }
643038bc64cSmatthias.ringwald 
64440d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
6458d213e1aSmatthias.ringwald 
6467b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
6479418f9c9Smatthias.ringwald 
6488d213e1aSmatthias.ringwald     // close low-level device
6498d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
6508d213e1aSmatthias.ringwald 
6517b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
6529418f9c9Smatthias.ringwald 
6538d213e1aSmatthias.ringwald     // power off
6548d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
6558d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
6568d213e1aSmatthias.ringwald     }
6579418f9c9Smatthias.ringwald 
6587b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
6599418f9c9Smatthias.ringwald 
66072ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
66172ea5239Smatthias.ringwald }
66272ea5239Smatthias.ringwald 
66340d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
66472ea5239Smatthias.ringwald 
6657b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
6663144bce4Smatthias.ringwald 
667b429b9b7Smatthias.ringwald #if 0
668b429b9b7Smatthias.ringwald     // don't close serial port during sleep
669b429b9b7Smatthias.ringwald 
67072ea5239Smatthias.ringwald     // close low-level device
67172ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
672b429b9b7Smatthias.ringwald #endif
67372ea5239Smatthias.ringwald 
67472ea5239Smatthias.ringwald     // sleep mode
6753144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
67672ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
67772ea5239Smatthias.ringwald     }
678b429b9b7Smatthias.ringwald 
67972ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
6808d213e1aSmatthias.ringwald }
6818d213e1aSmatthias.ringwald 
68240d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
683b429b9b7Smatthias.ringwald 
6847b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
685b429b9b7Smatthias.ringwald 
686b429b9b7Smatthias.ringwald     // wake on
687b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
688b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
689b429b9b7Smatthias.ringwald     }
690b429b9b7Smatthias.ringwald 
691b429b9b7Smatthias.ringwald #if 0
692b429b9b7Smatthias.ringwald     // open low-level device
693b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
694b429b9b7Smatthias.ringwald     if (err){
6957d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
696b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
697b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
698b429b9b7Smatthias.ringwald         }
699b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
700b429b9b7Smatthias.ringwald         return err;
701b429b9b7Smatthias.ringwald     }
702b429b9b7Smatthias.ringwald #endif
703b429b9b7Smatthias.ringwald 
704b429b9b7Smatthias.ringwald     return 0;
705b429b9b7Smatthias.ringwald }
706b429b9b7Smatthias.ringwald 
707b429b9b7Smatthias.ringwald 
7088d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
709d661ed19Smatthias.ringwald 
7107b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
711d661ed19Smatthias.ringwald 
7128d213e1aSmatthias.ringwald     int err = 0;
7138d213e1aSmatthias.ringwald     switch (hci_stack.state){
7148d213e1aSmatthias.ringwald 
7158d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
7168d213e1aSmatthias.ringwald             switch (power_mode){
7178d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7188d213e1aSmatthias.ringwald                     err = hci_power_control_on();
7198d213e1aSmatthias.ringwald                     if (err) return err;
7207301ad89Smatthias.ringwald                     // set up state machine
7217301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
7227301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7237301ad89Smatthias.ringwald                     hci_stack.substate = 0;
7248d213e1aSmatthias.ringwald                     break;
7258d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7268d213e1aSmatthias.ringwald                     // do nothing
7278d213e1aSmatthias.ringwald                     break;
7288d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
729b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
7308d213e1aSmatthias.ringwald                     break;
7318d213e1aSmatthias.ringwald             }
7328d213e1aSmatthias.ringwald             break;
7337301ad89Smatthias.ringwald 
7348d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7358d213e1aSmatthias.ringwald             switch (power_mode){
7368d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7378d213e1aSmatthias.ringwald                     // do nothing
7388d213e1aSmatthias.ringwald                     break;
7398d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7408d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
7418d213e1aSmatthias.ringwald                     hci_power_control_off();
7428d213e1aSmatthias.ringwald                     break;
7438d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
744b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
74572ea5239Smatthias.ringwald                     hci_power_control_sleep();
7468d213e1aSmatthias.ringwald                     break;
7478d213e1aSmatthias.ringwald             }
7488d213e1aSmatthias.ringwald             break;
7497301ad89Smatthias.ringwald 
7508d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
7518d213e1aSmatthias.ringwald             switch (power_mode){
7528d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7538d213e1aSmatthias.ringwald                     // do nothing
7548d213e1aSmatthias.ringwald                     break;
7558d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
756c7e0c5f6Smatthias.ringwald                     // see hci_run
757c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7588d213e1aSmatthias.ringwald                     break;
7598d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
760b546ac54Smatthias.ringwald                     // see hci_run
761b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
76289db417bSmatthias.ringwald                     hci_stack.substate = 0;
7638d213e1aSmatthias.ringwald                     break;
7648d213e1aSmatthias.ringwald             }
7658d213e1aSmatthias.ringwald             break;
7667301ad89Smatthias.ringwald 
7678d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
7688d213e1aSmatthias.ringwald             switch (power_mode){
7698d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7708d213e1aSmatthias.ringwald                     // set up state machine
7718d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7728d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
7738d213e1aSmatthias.ringwald                     break;
7748d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7758d213e1aSmatthias.ringwald                     // do nothing
7768d213e1aSmatthias.ringwald                     break;
7778d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
778b546ac54Smatthias.ringwald                     // see hci_run
779b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
78089db417bSmatthias.ringwald                     hci_stack.substate = 0;
7818d213e1aSmatthias.ringwald                     break;
7828d213e1aSmatthias.ringwald             }
7838d213e1aSmatthias.ringwald             break;
7848d213e1aSmatthias.ringwald 
7858d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
7868d213e1aSmatthias.ringwald             switch (power_mode){
7878d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
788b546ac54Smatthias.ringwald                     // set up state machine
789b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
790b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
791b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
7928d213e1aSmatthias.ringwald                     break;
7938d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
794b546ac54Smatthias.ringwald                     // see hci_run
795b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7968d213e1aSmatthias.ringwald                     break;
7978d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
798b546ac54Smatthias.ringwald                     // do nothing
7998d213e1aSmatthias.ringwald                     break;
8008d213e1aSmatthias.ringwald             }
8018d213e1aSmatthias.ringwald             break;
8028d213e1aSmatthias.ringwald 
8038d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
8048d213e1aSmatthias.ringwald             switch (power_mode){
8058d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8063144bce4Smatthias.ringwald                     err = hci_power_control_wake();
8073144bce4Smatthias.ringwald                     if (err) return err;
808b546ac54Smatthias.ringwald                     // set up state machine
809b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
810b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
811b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8128d213e1aSmatthias.ringwald                     break;
8138d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8144ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8158d213e1aSmatthias.ringwald                     break;
8168d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
817b546ac54Smatthias.ringwald                     // do nothing
8188d213e1aSmatthias.ringwald                     break;
8198d213e1aSmatthias.ringwald             }
8208d213e1aSmatthias.ringwald             break;
82111e23e5fSmatthias.ringwald     }
82268d92d03Smatthias.ringwald 
823038bc64cSmatthias.ringwald     // create internal event
824ee8bf225Smatthias.ringwald 	hci_emit_state();
825ee8bf225Smatthias.ringwald 
82668d92d03Smatthias.ringwald 	// trigger next/first action
82768d92d03Smatthias.ringwald 	hci_run();
82868d92d03Smatthias.ringwald 
829475c8125Smatthias.ringwald     return 0;
830475c8125Smatthias.ringwald }
831475c8125Smatthias.ringwald 
832381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
833381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
834381fbed8Smatthias.ringwald 
835381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
836381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
837381fbed8Smatthias.ringwald         return;
838381fbed8Smatthias.ringwald     }
839381fbed8Smatthias.ringwald 
840381fbed8Smatthias.ringwald     hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan
841381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
842381fbed8Smatthias.ringwald }
843381fbed8Smatthias.ringwald 
84406b35ec0Smatthias.ringwald void hci_run(){
8458a485f27Smatthias.ringwald 
84632ab9390Smatthias.ringwald     hci_connection_t * connection;
84732ab9390Smatthias.ringwald     linked_item_t * it;
84832ab9390Smatthias.ringwald 
84932ab9390Smatthias.ringwald     // send pending HCI commands
85032ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
85132ab9390Smatthias.ringwald 
85232ab9390Smatthias.ringwald         connection = (hci_connection_t *) it;
85332ab9390Smatthias.ringwald 
85453b86778Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) {
8557b5fbe1fSmatthias.ringwald             // log_info("hci_run: cannot send command packet\n");
85653b86778Smatthias.ringwald             return;
85753b86778Smatthias.ringwald         }
85832ab9390Smatthias.ringwald 
85932ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
8607b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
86132ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
86232ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
863c7e0c5f6Smatthias.ringwald         }
864c7e0c5f6Smatthias.ringwald 
865de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
86632ab9390Smatthias.ringwald 
86732ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
86832ab9390Smatthias.ringwald             link_key_t link_key;
8697b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
87032ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
87132ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
87232ab9390Smatthias.ringwald             } else {
87332ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
87432ab9390Smatthias.ringwald             }
87532ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
87632ab9390Smatthias.ringwald         }
87732ab9390Smatthias.ringwald     }
87832ab9390Smatthias.ringwald 
879de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
880c7e0c5f6Smatthias.ringwald 
8813429f56bSmatthias.ringwald     switch (hci_stack.state){
8823429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8837b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
8843429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
8853429f56bSmatthias.ringwald                 // odd: waiting for command completion
88606b35ec0Smatthias.ringwald                 return;
8873429f56bSmatthias.ringwald             }
88890919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
889c7492964Smatthias.ringwald                 case 0: // RESET
89022909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
891c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
892c7492964Smatthias.ringwald                         // skip baud change
893c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
894c7492964Smatthias.ringwald                     }
8953429f56bSmatthias.ringwald                     break;
896c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
897c7492964Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_cmd_buffer);
898c7492964Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_cmd_buffer, 3 + hci_stack.hci_cmd_buffer[2]);
899c7492964Smatthias.ringwald                     break;
900c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
901c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
902c7492964Smatthias.ringwald                     hci_stack.substate += 2;
903c7492964Smatthias.ringwald                     // break missing here for fall through
904c7492964Smatthias.ringwald 
905c7492964Smatthias.ringwald                 case 3:
90690919203Smatthias.ringwald                     // custom initialization
90790919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
90890919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
90990919203Smatthias.ringwald                         if (cmd) {
91090919203Smatthias.ringwald                             int size = 3 + cmd[2];
911622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
912c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
91390919203Smatthias.ringwald                             break;
91490919203Smatthias.ringwald                         }
915c7492964Smatthias.ringwald                         printf("hci_run: init script done\n\r");
91690919203Smatthias.ringwald                     }
9172f6c30e1Smatthias.ringwald                     // otherwise continue
918f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
919f432a6ddSmatthias.ringwald 					break;
920c7492964Smatthias.ringwald 				case 4:
921e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
922e2edc0c3Smatthias.ringwald 					break;
923c7492964Smatthias.ringwald                 case 5:
9243429f56bSmatthias.ringwald                     // ca. 15 sec
9253429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
9263429f56bSmatthias.ringwald                     break;
927c7492964Smatthias.ringwald 				case 6:
9280a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
929f432a6ddSmatthias.ringwald 					break;
930c7492964Smatthias.ringwald                 case 7:
9318a485f27Smatthias.ringwald #ifndef EMBEDDED
9328a485f27Smatthias.ringwald                 {
9338a485f27Smatthias.ringwald                     char hostname[30];
9348a485f27Smatthias.ringwald                     gethostname(hostname, 30);
9358a485f27Smatthias.ringwald                     hostname[29] = '\0';
9368a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
9378a485f27Smatthias.ringwald                     break;
9388a485f27Smatthias.ringwald                 }
939c7492964Smatthias.ringwald                 case 8:
9403c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
9413c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
9423c4d4b90Smatthias.ringwald                     break;
9433c4d4b90Smatthias.ringwald 
944c7492964Smatthias.ringwald                 case 9:
9453c4d4b90Smatthias.ringwald #endif
9468a485f27Smatthias.ringwald #endif
9473429f56bSmatthias.ringwald                     // done.
9483429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
949b360b6adSmatthias.ringwald                     hci_emit_state();
9503429f56bSmatthias.ringwald                     break;
9513429f56bSmatthias.ringwald                 default:
9523429f56bSmatthias.ringwald                     break;
953475c8125Smatthias.ringwald             }
9543429f56bSmatthias.ringwald             hci_stack.substate++;
9553429f56bSmatthias.ringwald             break;
956c7e0c5f6Smatthias.ringwald 
957c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
958c7e0c5f6Smatthias.ringwald 
9597b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
960c7e0c5f6Smatthias.ringwald             // close all open connections
961c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
962c7e0c5f6Smatthias.ringwald             if (connection){
96332ab9390Smatthias.ringwald 
964c7e0c5f6Smatthias.ringwald                 // send disconnect
96532ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
96632ab9390Smatthias.ringwald 
9677b5fbe1fSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
9686ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
969c7e0c5f6Smatthias.ringwald 
970c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
971c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
972c7e0c5f6Smatthias.ringwald                 return;
973c7e0c5f6Smatthias.ringwald             }
9747b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
975c7e0c5f6Smatthias.ringwald 
97672ea5239Smatthias.ringwald             // switch mode
977c7e0c5f6Smatthias.ringwald             hci_power_control_off();
9789418f9c9Smatthias.ringwald 
9797b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
98072ea5239Smatthias.ringwald             hci_emit_state();
9817b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
98272ea5239Smatthias.ringwald             break;
983c7e0c5f6Smatthias.ringwald 
98472ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
98589db417bSmatthias.ringwald             switch(hci_stack.substate) {
98689db417bSmatthias.ringwald                 case 0:
9877b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
98872ea5239Smatthias.ringwald                     // close all open connections
98972ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
99072ea5239Smatthias.ringwald                     if (connection){
99132ab9390Smatthias.ringwald 
99272ea5239Smatthias.ringwald                         // send disconnect
99332ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
99432ab9390Smatthias.ringwald 
9957b5fbe1fSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
9966ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
99772ea5239Smatthias.ringwald 
99872ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
99972ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
100072ea5239Smatthias.ringwald                         return;
100172ea5239Smatthias.ringwald                     }
100272ea5239Smatthias.ringwald 
100389db417bSmatthias.ringwald                     // disable page and inquiry scan
100432ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
100532ab9390Smatthias.ringwald 
10067b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq & page scans\n");
100789db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
100889db417bSmatthias.ringwald 
100989db417bSmatthias.ringwald                     // continue in next sub state
101089db417bSmatthias.ringwald                     hci_stack.substate++;
101189db417bSmatthias.ringwald                     break;
101289db417bSmatthias.ringwald                 case 1:
101389db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
101489db417bSmatthias.ringwald                     break;
101589db417bSmatthias.ringwald                 case 2:
10167b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
101772ea5239Smatthias.ringwald                     // switch mode
101889db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1019c7e0c5f6Smatthias.ringwald                     hci_emit_state();
102089db417bSmatthias.ringwald                 default:
102189db417bSmatthias.ringwald                     break;
102289db417bSmatthias.ringwald             }
1023c7e0c5f6Smatthias.ringwald             break;
1024c7e0c5f6Smatthias.ringwald 
10253429f56bSmatthias.ringwald         default:
10263429f56bSmatthias.ringwald             break;
10271f504dbdSmatthias.ringwald     }
10283429f56bSmatthias.ringwald }
102916833f0aSmatthias.ringwald 
103031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1031c8e4258aSmatthias.ringwald     bd_addr_t addr;
1032c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1033c8e4258aSmatthias.ringwald     // house-keeping
1034c8e4258aSmatthias.ringwald 
1035c8e4258aSmatthias.ringwald     // create_connection?
1036c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1037c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
10387b5fbe1fSmatthias.ringwald         log_info("Create_connection to "); print_bd_addr(addr); log_info("\n");
1039c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1040c8e4258aSmatthias.ringwald         if (conn) {
1041c8e4258aSmatthias.ringwald             // if connection exists
1042c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
1043c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
1044c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
1045c8e4258aSmatthias.ringwald             }
1046c8e4258aSmatthias.ringwald             //    otherwise, just ignore
1047c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1048c8e4258aSmatthias.ringwald 
1049c8e4258aSmatthias.ringwald         } else{
1050c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
1051c8e4258aSmatthias.ringwald             if (conn){
1052c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
1053c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
1054c8e4258aSmatthias.ringwald             }
1055c8e4258aSmatthias.ringwald         }
1056c8e4258aSmatthias.ringwald     }
1057c8e4258aSmatthias.ringwald 
10587fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
10597fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
10607fde4af9Smatthias.ringwald     }
10617fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
10627fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
10637fde4af9Smatthias.ringwald     }
10647fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
10657fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
10667fde4af9Smatthias.ringwald     }
10677fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
10687fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
10697fde4af9Smatthias.ringwald     }
10707fde4af9Smatthias.ringwald 
10718ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
10728ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
10738ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10748ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
10758ef73945Smatthias.ringwald         }
10768ef73945Smatthias.ringwald     }
1077c8e4258aSmatthias.ringwald 
107831452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1079622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
108031452debSmatthias.ringwald }
10818adf0ddaSmatthias.ringwald 
10821cd208adSmatthias.ringwald /**
10831cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
10841cd208adSmatthias.ringwald  */
1085fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
10861cd208adSmatthias.ringwald     va_list argptr;
10871cd208adSmatthias.ringwald     va_start(argptr, cmd);
10881cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
10891cd208adSmatthias.ringwald     va_end(argptr);
1090124062bcSmatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_cmd_buffer, size);
109193b8dc03Smatthias.ringwald }
1092c8e4258aSmatthias.ringwald 
1093ee091cf1Smatthias.ringwald // Create various non-HCI events.
1094ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1095ee091cf1Smatthias.ringwald 
1096c8e4258aSmatthias.ringwald void hci_emit_state(){
1097425d1371Smatthias.ringwald     uint8_t event[3];
109880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1099425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1100c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1101425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1102425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1103c8e4258aSmatthias.ringwald }
1104c8e4258aSmatthias.ringwald 
1105c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
1106425d1371Smatthias.ringwald     uint8_t event[13];
1107c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1108425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1109c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
1110c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1111c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1112c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1113c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1114425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1115425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1116c8e4258aSmatthias.ringwald }
1117c8e4258aSmatthias.ringwald 
11183c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1119425d1371Smatthias.ringwald     uint8_t event[6];
11203c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1121e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
11223c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
11233c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
11243c4d4b90Smatthias.ringwald     event[5] = reason;
1125425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1126425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11273c4d4b90Smatthias.ringwald }
11283c4d4b90Smatthias.ringwald 
1129ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1130425d1371Smatthias.ringwald     uint8_t event[4];
113180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1132425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1133ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1134425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1135425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1136ee091cf1Smatthias.ringwald }
113743bfb1bdSmatthias.ringwald 
113843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1139425d1371Smatthias.ringwald     uint8_t event[3];
114080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1141425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
114243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1143425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1144425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
114543bfb1bdSmatthias.ringwald }
1146038bc64cSmatthias.ringwald 
1147038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1148425d1371Smatthias.ringwald     uint8_t event[2];
114980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1150425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1151425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1152425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1153038bc64cSmatthias.ringwald }
11541b0e3922Smatthias.ringwald 
11551b0e3922Smatthias.ringwald 
11561b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1157425d1371Smatthias.ringwald     uint8_t event[6];
11581b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1159425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1160425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1161425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1162425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1163425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1164425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11651b0e3922Smatthias.ringwald }
11661b0e3922Smatthias.ringwald 
11672ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1168425d1371Smatthias.ringwald     uint8_t event[3];
11692ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1170425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
11712ed6235cSmatthias.ringwald     event[2] = enabled;
1172425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1173e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11742ed6235cSmatthias.ringwald }
1175627c2f45Smatthias.ringwald 
1176627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1177425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1178627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1179425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1180f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
11812f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1182f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1183425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1184425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1185627c2f45Smatthias.ringwald }
1186381fbed8Smatthias.ringwald 
1187381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1188425d1371Smatthias.ringwald     uint8_t event[3];
1189381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1190425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1191381fbed8Smatthias.ringwald     event[2] = enabled;
1192425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1193425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1194381fbed8Smatthias.ringwald }
1195