xref: /btstack/src/hci.c (revision 219eea5f01e3e2b5b6dabd0ad44ae70646cde1b8)
11f504dbdSmatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 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.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
381f504dbdSmatthias.ringwald  *  hci.c
391f504dbdSmatthias.ringwald  *
401f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
411f504dbdSmatthias.ringwald  *
421f504dbdSmatthias.ringwald  */
431f504dbdSmatthias.ringwald 
44bde315ceS[email protected] #include "btstack-config.h"
4528171530Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "hci.h"
474c57c146S[email protected] #include "gap.h"
487f2435e6Smatthias.ringwald 
4993b8dc03Smatthias.ringwald #include <stdarg.h>
5093b8dc03Smatthias.ringwald #include <string.h>
5156fe0872Smatthias.ringwald #include <stdio.h>
527f2435e6Smatthias.ringwald 
53549e6ebeSmatthias.ringwald #ifndef EMBEDDED
54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5509ba8edeSmatthias.ringwald #include <btstack/version.h>
56549e6ebeSmatthias.ringwald #endif
57549e6ebeSmatthias.ringwald 
58a3b02b71Smatthias.ringwald #include "btstack_memory.h"
597f2435e6Smatthias.ringwald #include "debug.h"
60d8905019Smatthias.ringwald #include "hci_dump.h"
6193b8dc03Smatthias.ringwald 
62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
631b0e3922Smatthias.ringwald 
64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
65ee091cf1Smatthias.ringwald 
66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
67da5275c5S[email protected] 
6828171530Smatthias.ringwald #ifdef USE_BLUETOOL
6928171530Smatthias.ringwald #include "bt_control_iphone.h"
7028171530Smatthias.ringwald #endif
7128171530Smatthias.ringwald 
72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
73a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
7496a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer);
7596a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection);
767586ee35S[email protected] static int  hci_power_control_on(void);
777586ee35S[email protected] static void hci_power_control_off(void);
786155b3d3S[email protected] static void hci_state_reset();
79758b46ceSmatthias.ringwald 
8006b35ec0Smatthias.ringwald // the STACK is here
813a9fb326S[email protected] #ifndef HAVE_MALLOC
823a9fb326S[email protected] static hci_stack_t   hci_stack_static;
833a9fb326S[email protected] #endif
843a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
8516833f0aSmatthias.ringwald 
8666fb9560S[email protected] // test helper
8766fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
8866fb9560S[email protected] 
8996a45072S[email protected] /**
9096a45072S[email protected]  * create connection for given address
9196a45072S[email protected]  *
9296a45072S[email protected]  * @return connection OR NULL, if no memory left
9396a45072S[email protected]  */
9496a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
9596a45072S[email protected] 
965127cc62S[email protected]     log_info("create_connection_for_addr %s", bd_addr_to_str(addr));
9796a45072S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
9896a45072S[email protected]     if (!conn) return NULL;
9996a45072S[email protected]     BD_ADDR_COPY(conn->address, addr);
10096a45072S[email protected]     conn->address_type = addr_type;
10196a45072S[email protected]     conn->con_handle = 0xffff;
10296a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
10396a45072S[email protected]     conn->bonding_flags = 0;
10496a45072S[email protected]     conn->requested_security_level = LEVEL_0;
10596a45072S[email protected]     linked_item_set_user(&conn->timeout.item, conn);
10696a45072S[email protected]     conn->timeout.process = hci_connection_timeout_handler;
10796a45072S[email protected]     hci_connection_timestamp(conn);
10896a45072S[email protected]     conn->acl_recombination_length = 0;
10996a45072S[email protected]     conn->acl_recombination_pos = 0;
11096a45072S[email protected]     conn->num_acl_packets_sent = 0;
11196a45072S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
11296a45072S[email protected]     return conn;
11396a45072S[email protected] }
11466fb9560S[email protected] 
11597addcc5Smatthias.ringwald /**
116ee091cf1Smatthias.ringwald  * get connection for a given handle
117ee091cf1Smatthias.ringwald  *
118ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
119ee091cf1Smatthias.ringwald  */
1205061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
121ee091cf1Smatthias.ringwald     linked_item_t *it;
1223a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
123ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
124ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
125ee091cf1Smatthias.ringwald         }
126ee091cf1Smatthias.ringwald     }
127ee091cf1Smatthias.ringwald     return NULL;
128ee091cf1Smatthias.ringwald }
129ee091cf1Smatthias.ringwald 
13096a45072S[email protected] /**
13196a45072S[email protected]  * get connection for given address
13296a45072S[email protected]  *
13396a45072S[email protected]  * @return connection OR NULL, if not found
13496a45072S[email protected]  */
13596a45072S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t * addr, bd_addr_type_t addr_type){
13662bda3fbS[email protected]     linked_item_t *it;
13762bda3fbS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
13862bda3fbS[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
13996a45072S[email protected]         if (connection->address_type != addr_type)  continue;
14096a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
14162bda3fbS[email protected]         return connection;
14262bda3fbS[email protected]     }
14362bda3fbS[email protected]     return NULL;
14462bda3fbS[email protected] }
14562bda3fbS[email protected] 
1462b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
14728ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
148c785ef68Smatthias.ringwald #ifdef HAVE_TIME
149ee091cf1Smatthias.ringwald     struct timeval tv;
150ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
151c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
152ee091cf1Smatthias.ringwald         // connections might be timed out
153ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
154ee091cf1Smatthias.ringwald     }
1552b12a0b9Smatthias.ringwald #endif
156e5780900Smatthias.ringwald #ifdef HAVE_TICK
157c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
158c785ef68Smatthias.ringwald         // connections might be timed out
159c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
160c785ef68Smatthias.ringwald     }
161c785ef68Smatthias.ringwald #endif
162c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
163c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
164c785ef68Smatthias.ringwald }
165ee091cf1Smatthias.ringwald 
166ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
167c7492964Smatthias.ringwald #ifdef HAVE_TIME
168ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
169c7492964Smatthias.ringwald #endif
170e5780900Smatthias.ringwald #ifdef HAVE_TICK
171c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
172c785ef68Smatthias.ringwald #endif
173ee091cf1Smatthias.ringwald }
174ee091cf1Smatthias.ringwald 
17506b35ec0Smatthias.ringwald 
17628ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
17728ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
17828ca2b46S[email protected] }
17928ca2b46S[email protected] 
18028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
18128ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
18228ca2b46S[email protected] }
18328ca2b46S[email protected] 
18428ca2b46S[email protected] 
18543bfb1bdSmatthias.ringwald /**
18680ca58a0Smatthias.ringwald  * add authentication flags and reset timer
18796a45072S[email protected]  * @note: assumes classic connection
1887fde4af9Smatthias.ringwald  */
1897fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1907fde4af9Smatthias.ringwald     bd_addr_t addr;
1917fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
19296a45072S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1937fde4af9Smatthias.ringwald     if (conn) {
19428ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
19580ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1967fde4af9Smatthias.ringwald     }
1977fde4af9Smatthias.ringwald }
1987fde4af9Smatthias.ringwald 
19980ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2005061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
20180ca58a0Smatthias.ringwald     if (!conn) return 0;
2026724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2036724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2046724cd9eS[email protected]     return 0;
20580ca58a0Smatthias.ringwald }
20680ca58a0Smatthias.ringwald 
207c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
2083a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2093a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
210c12e46e7Smatthias.ringwald     }
211c12e46e7Smatthias.ringwald }
212c12e46e7Smatthias.ringwald 
2130bf6344aS[email protected] int hci_is_le_connection(hci_connection_t * connection){
2140bf6344aS[email protected]     return  connection->address_type == BD_ADDR_TYPE_LE_PUBLIC ||
2150bf6344aS[email protected]     connection->address_type == BD_ADDR_TYPE_LE_RANDOM;
2160bf6344aS[email protected] }
2170bf6344aS[email protected] 
2187fde4af9Smatthias.ringwald 
2197fde4af9Smatthias.ringwald /**
22043bfb1bdSmatthias.ringwald  * count connections
22143bfb1bdSmatthias.ringwald  */
22240d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
22356c253c9Smatthias.ringwald     int count = 0;
22443bfb1bdSmatthias.ringwald     linked_item_t *it;
2253a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
22643bfb1bdSmatthias.ringwald     return count;
22743bfb1bdSmatthias.ringwald }
228c8e4258aSmatthias.ringwald 
22997addcc5Smatthias.ringwald /**
230ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
23116833f0aSmatthias.ringwald  */
2322718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
23316833f0aSmatthias.ringwald }
23416833f0aSmatthias.ringwald 
235998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2365061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
237998906cdSmatthias.ringwald     if (!connection) {
238ee303eddS[email protected]         log_error("hci_number_outgoing_packets: connection for handle %u does not exist!\n", handle);
239998906cdSmatthias.ringwald         return 0;
240998906cdSmatthias.ringwald     }
241998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
242998906cdSmatthias.ringwald }
243998906cdSmatthias.ringwald 
244e79abdd6S[email protected] uint8_t hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){
245ee303eddS[email protected] 
246ee303eddS[email protected]     int num_packets_sent_classic = 0;
247ee303eddS[email protected]     int num_packets_sent_le = 0;
248ee303eddS[email protected] 
249ee303eddS[email protected]     bd_addr_type_t address_type = BD_ADDR_TYPE_UNKNOWN;
250ee303eddS[email protected] 
251998906cdSmatthias.ringwald     linked_item_t *it;
2523a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
253998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
254ee303eddS[email protected]         if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
255ee303eddS[email protected]             num_packets_sent_classic += connection->num_acl_packets_sent;
256ee303eddS[email protected]         } else {
257ee303eddS[email protected]             num_packets_sent_le += connection->num_acl_packets_sent;
258ee303eddS[email protected]         }
259ee303eddS[email protected]         if (connection->con_handle == con_handle){
260ee303eddS[email protected]             address_type = connection->address_type;
261ee303eddS[email protected]         }
262ee303eddS[email protected]     }
263ee303eddS[email protected] 
264ee303eddS[email protected]     int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic;
265ee303eddS[email protected]     int free_slots_le = 0;
266ee303eddS[email protected] 
267ee303eddS[email protected]     if (free_slots_classic < 0){
268ee303eddS[email protected]         log_error("hci_number_free_acl_slots: outgoing classic packets (%u) > total classic packets (%u)\n", num_packets_sent_classic, hci_stack->acl_packets_total_num);
269998906cdSmatthias.ringwald         return 0;
270998906cdSmatthias.ringwald     }
271ee303eddS[email protected] 
272ee303eddS[email protected]     if (hci_stack->le_acl_packets_total_num){
273ee303eddS[email protected]         // if we have LE slots, they are used
274ee303eddS[email protected]         free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le;
275ee303eddS[email protected]         if (free_slots_le < 0){
276ee303eddS[email protected]             log_error("hci_number_free_acl_slots: outgoing le packets (%u) > total le packets (%u)\n", num_packets_sent_le, hci_stack->le_acl_packets_total_num);
277ee303eddS[email protected]             return 0;
278998906cdSmatthias.ringwald         }
279ee303eddS[email protected]     } else {
280ee303eddS[email protected]         // otherwise, classic slots are used for LE, too
281ee303eddS[email protected]         free_slots_classic -= num_packets_sent_le;
282ee303eddS[email protected]         if (free_slots_classic < 0){
283ee303eddS[email protected]             log_error("hci_number_free_acl_slots: outgoing classic + le packets (%u + %u) > total packets (%u)\n", num_packets_sent_classic, num_packets_sent_le, hci_stack->acl_packets_total_num);
284ee303eddS[email protected]             return 0;
285ee303eddS[email protected]         }
286ee303eddS[email protected]     }
287ee303eddS[email protected] 
288ee303eddS[email protected]     switch (address_type){
289ee303eddS[email protected]         case BD_ADDR_TYPE_UNKNOWN:
290ee303eddS[email protected]             log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list\n", con_handle);
291ee303eddS[email protected]             return 0;
292ee303eddS[email protected] 
293ee303eddS[email protected]         case BD_ADDR_TYPE_CLASSIC:
294ee303eddS[email protected]             return free_slots_classic;
295ee303eddS[email protected] 
296ee303eddS[email protected]         default:
297ee303eddS[email protected]             return free_slots_le;
298ee303eddS[email protected]     }
299998906cdSmatthias.ringwald }
300998906cdSmatthias.ringwald 
301ac928cc2S[email protected] 
302ac928cc2S[email protected] // @deprecated
303c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
304c24735b1Smatthias.ringwald     switch (packet_type) {
305c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
306ac928cc2S[email protected]             return hci_can_send_prepared_acl_packet_now(0);
307c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
308ac928cc2S[email protected]             return hci_can_send_command_packet_now();
309c24735b1Smatthias.ringwald         default:
310c24735b1Smatthias.ringwald             return 0;
311c24735b1Smatthias.ringwald     }
312c24735b1Smatthias.ringwald }
313c24735b1Smatthias.ringwald 
314ac928cc2S[email protected] // @deprecated
3156b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use
3166b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
317ac928cc2S[email protected] 
3186b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
319ac928cc2S[email protected] 
320ac928cc2S[email protected]     switch (packet_type) {
321ac928cc2S[email protected]         case HCI_ACL_DATA_PACKET:
322ac928cc2S[email protected]             return hci_can_send_acl_packet_now(0);
323ac928cc2S[email protected]         case HCI_COMMAND_DATA_PACKET:
324ac928cc2S[email protected]             return hci_can_send_command_packet_now();
325ac928cc2S[email protected]         default:
326ac928cc2S[email protected]             return 0;
327ac928cc2S[email protected]     }
328ac928cc2S[email protected] }
329ac928cc2S[email protected] 
330ac928cc2S[email protected] 
331ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer]
332ac928cc2S[email protected] int hci_can_send_command_packet_now(void){
333ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
334ac928cc2S[email protected] 
335ac928cc2S[email protected]     // check for async hci transport implementations
336ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
337ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){
338ac928cc2S[email protected]             return 0;
339ac928cc2S[email protected]         }
340ac928cc2S[email protected]     }
341ac928cc2S[email protected] 
342ac928cc2S[email protected]     return hci_stack->num_cmd_packets > 0;
343ac928cc2S[email protected] }
344ac928cc2S[email protected] 
345ac928cc2S[email protected] int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) {
346ac928cc2S[email protected]     // check for async hci transport implementations
347ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
348ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_ACL_DATA_PACKET)){
349ac928cc2S[email protected]             return 0;
350ac928cc2S[email protected]         }
351ac928cc2S[email protected]     }
352e79abdd6S[email protected]     return hci_number_free_acl_slots_for_handle(con_handle) > 0;
353ac928cc2S[email protected] }
354ac928cc2S[email protected] 
355ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){
356ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
357ac928cc2S[email protected]     return hci_can_send_prepared_acl_packet_now(con_handle);
3586b4af23dS[email protected] }
3596b4af23dS[email protected] 
360c8b9416aS[email protected] // used for internal checks in l2cap[-le].c
361c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
362c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
363c8b9416aS[email protected] }
364c8b9416aS[email protected] 
3656b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
3666b4af23dS[email protected] int hci_reserve_packet_buffer(void){
3679d14b626S[email protected]     if (hci_stack->hci_packet_buffer_reserved) {
3689d14b626S[email protected]         log_error("hci_reserve_packet_buffer called but buffer already reserved");
3699d14b626S[email protected]         return 0;
3709d14b626S[email protected]     }
3716b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
3726b4af23dS[email protected]     return 1;
3736b4af23dS[email protected] }
3746b4af23dS[email protected] 
37568a0fcf7S[email protected] void hci_release_packet_buffer(void){
37668a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
37768a0fcf7S[email protected] }
37868a0fcf7S[email protected] 
3796b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
3806b4af23dS[email protected] int hci_transport_synchronous(void){
3816b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
3826b4af23dS[email protected] }
3836b4af23dS[email protected] 
384826f7347S[email protected] // pre: caller has reserved the packet buffer
385826f7347S[email protected] int hci_send_acl_packet_buffer(int size){
3867856c818Smatthias.ringwald 
387826f7347S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
388826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
389826f7347S[email protected]         return 0;
390826f7347S[email protected]     }
391826f7347S[email protected] 
392d713a683S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
393d713a683S[email protected]     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
394d713a683S[email protected] 
395826f7347S[email protected]     // check for free places on Bluetooth module
396d713a683S[email protected]     if (!hci_can_send_prepared_acl_packet_now(con_handle)) {
397826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller");
39897b61c7bS[email protected]         hci_release_packet_buffer();
39997b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
40097b61c7bS[email protected]     }
4016218e6f1Smatthias.ringwald 
4025061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
40397b61c7bS[email protected]     if (!connection) {
4045fa0b7cfS[email protected]         log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle);
40597b61c7bS[email protected]         hci_release_packet_buffer();
40697b61c7bS[email protected]         return 0;
40797b61c7bS[email protected]     }
40856cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
40956cf178bSmatthias.ringwald 
41056cf178bSmatthias.ringwald     // count packet
41156cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
4127b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
4137856c818Smatthias.ringwald 
41400d8e42eSmatthias.ringwald     // send packet
4153a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
4166218e6f1Smatthias.ringwald 
4176b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
418826f7347S[email protected]     if (hci_transport_synchronous()){
41997b61c7bS[email protected]         hci_release_packet_buffer();
4206b4af23dS[email protected]     }
4216b4af23dS[email protected] 
42200d8e42eSmatthias.ringwald     return err;
423ee091cf1Smatthias.ringwald }
424ee091cf1Smatthias.ringwald 
42516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
4267856c818Smatthias.ringwald 
427e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
428e76a89eeS[email protected] 
4297856c818Smatthias.ringwald     // get info
4307856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
4315061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
4327856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
4337856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
4347856c818Smatthias.ringwald 
4357856c818Smatthias.ringwald     // ignore non-registered handle
4367856c818Smatthias.ringwald     if (!conn){
4377d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
4387856c818Smatthias.ringwald         return;
4397856c818Smatthias.ringwald     }
4407856c818Smatthias.ringwald 
441e76a89eeS[email protected]     // assert packet is complete
4429ecc3e17S[email protected]     if (acl_length + 4 != size){
443e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
444e76a89eeS[email protected]         return;
445e76a89eeS[email protected]     }
446e76a89eeS[email protected] 
4477856c818Smatthias.ringwald     // update idle timestamp
4487856c818Smatthias.ringwald     hci_connection_timestamp(conn);
4497856c818Smatthias.ringwald 
4507856c818Smatthias.ringwald     // handle different packet types
4517856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
4527856c818Smatthias.ringwald 
4537856c818Smatthias.ringwald         case 0x01: // continuation fragment
4547856c818Smatthias.ringwald 
4557856c818Smatthias.ringwald             // sanity check
4567856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
4577d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
4587856c818Smatthias.ringwald                 return;
4597856c818Smatthias.ringwald             }
4607856c818Smatthias.ringwald 
4617856c818Smatthias.ringwald             // append fragment payload (header already stored)
4627856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
4637856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
4647856c818Smatthias.ringwald 
4657d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
466decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
4677856c818Smatthias.ringwald 
4687856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
4697856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
4707856c818Smatthias.ringwald 
4713a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
4727856c818Smatthias.ringwald                 // reset recombination buffer
4737856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
4747856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
4757856c818Smatthias.ringwald             }
4767856c818Smatthias.ringwald             break;
4777856c818Smatthias.ringwald 
4787856c818Smatthias.ringwald         case 0x02: { // first fragment
4797856c818Smatthias.ringwald 
4807856c818Smatthias.ringwald             // sanity check
4817856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
4827d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
4837856c818Smatthias.ringwald                 return;
4847856c818Smatthias.ringwald             }
4857856c818Smatthias.ringwald 
4867856c818Smatthias.ringwald             // peek into L2CAP packet!
4877856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
4887856c818Smatthias.ringwald 
489e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
490decc01a8Smatthias.ringwald 
4917856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
4927856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
4937856c818Smatthias.ringwald 
4947856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
4953a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
4967856c818Smatthias.ringwald 
4977856c818Smatthias.ringwald             } else {
4987856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
4997856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
5007856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
5017856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
502decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
5037856c818Smatthias.ringwald             }
5047856c818Smatthias.ringwald             break;
5057856c818Smatthias.ringwald 
5067856c818Smatthias.ringwald         }
5077856c818Smatthias.ringwald         default:
5087d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
5097856c818Smatthias.ringwald             return;
5107856c818Smatthias.ringwald     }
51194ab26f8Smatthias.ringwald 
51294ab26f8Smatthias.ringwald     // execute main loop
51394ab26f8Smatthias.ringwald     hci_run();
51416833f0aSmatthias.ringwald }
51522909952Smatthias.ringwald 
51667a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
5171f479f8cS[email protected]     log_info("Connection closed: handle 0x%x, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
5183c4d4b90Smatthias.ringwald 
519c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
520c785ef68Smatthias.ringwald 
5213a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
522a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
5233c4d4b90Smatthias.ringwald 
5243c4d4b90Smatthias.ringwald     // now it's gone
525c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
526c7e0c5f6Smatthias.ringwald }
527c7e0c5f6Smatthias.ringwald 
5280c042179S[email protected] static const uint16_t packet_type_sizes[] = {
5298f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
5308f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
5318f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
5328f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
5338f8108aaSmatthias.ringwald };
53465389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
53565389bfcS[email protected]      0, // 3 slot packets
53665389bfcS[email protected]      1, // 5 slot packets
53765389bfcS[email protected]     25, // EDR 2 mpbs
53865389bfcS[email protected]     26, // EDR 3 mbps
53965389bfcS[email protected]     39, // 3 slot EDR packts
54065389bfcS[email protected]     40, // 5 slot EDR packet
54165389bfcS[email protected] };
54265389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
54365389bfcS[email protected]     0x0f00, // 3 slot packets
54465389bfcS[email protected]     0xf000, // 5 slot packets
54565389bfcS[email protected]     0x1102, // EDR 2 mpbs
54665389bfcS[email protected]     0x2204, // EDR 3 mbps
54765389bfcS[email protected]     0x0300, // 3 slot EDR packts
54865389bfcS[email protected]     0x3000, // 5 slot EDR packet
54965389bfcS[email protected] };
5508f8108aaSmatthias.ringwald 
55165389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
55265389bfcS[email protected]     // enable packet types based on size
5538f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
5548f8108aaSmatthias.ringwald     int i;
5558f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
5568f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
5578f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
5588f8108aaSmatthias.ringwald             packet_types |= 1 << i;
5598f8108aaSmatthias.ringwald         }
5608f8108aaSmatthias.ringwald     }
56165389bfcS[email protected]     // disable packet types due to missing local supported features
56265389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
56365389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
56465389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
56565389bfcS[email protected]         if (feature_set) continue;
56665389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
56765389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
56865389bfcS[email protected]     }
5698f8108aaSmatthias.ringwald     // flip bits for "may not be used"
5708f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
5718f8108aaSmatthias.ringwald     return packet_types;
5728f8108aaSmatthias.ringwald }
5738f8108aaSmatthias.ringwald 
5748f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
5753a9fb326S[email protected]     return hci_stack->packet_types;
5768f8108aaSmatthias.ringwald }
5778f8108aaSmatthias.ringwald 
578facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
5797dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
5803a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
5817dc17943Smatthias.ringwald }
5827dc17943Smatthias.ringwald 
583f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
5843a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
5857dc17943Smatthias.ringwald }
5867dc17943Smatthias.ringwald 
5876ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
5886ac9a97eS[email protected]     // No. 54, byte 6, bit 6
5896ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
5906ac9a97eS[email protected] }
5916ac9a97eS[email protected] 
592f5d8d141S[email protected] int hci_ssp_supported(void){
5936ac9a97eS[email protected]     // No. 51, byte 6, bit 3
5943a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
595f5d8d141S[email protected] }
596f5d8d141S[email protected] 
597f5d8d141S[email protected] int hci_classic_supported(void){
5986ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
5993a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
600f5d8d141S[email protected] }
601f5d8d141S[email protected] 
602f5d8d141S[email protected] int hci_le_supported(void){
603f5d8d141S[email protected] #ifdef HAVE_BLE
6046ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
6053a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
606f5d8d141S[email protected] #else
607f5d8d141S[email protected]     return 0;
608f5d8d141S[email protected] #endif
609f5d8d141S[email protected] }
610f5d8d141S[email protected] 
61169a97523S[email protected] // get addr type and address used in advertisement packets
61218904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
6133a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
6143a9fb326S[email protected]     if (hci_stack->adv_addr_type){
6153a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
61669a97523S[email protected]     } else {
6173a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
61869a97523S[email protected]     }
61969a97523S[email protected] }
62069a97523S[email protected] 
621b2f949feS[email protected] #ifdef HAVE_BLE
622e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){
62357c9da5bS[email protected]     int num_reports = packet[3];
62457c9da5bS[email protected]     int i;
62557c9da5bS[email protected]     int total_data_length = 0;
62657c9da5bS[email protected]     int data_offset = 0;
62757c9da5bS[email protected] 
62857c9da5bS[email protected]     for (i=0; i<num_reports;i++){
62957c9da5bS[email protected]         total_data_length += packet[4+num_reports*8+i];
63057c9da5bS[email protected]     }
63157c9da5bS[email protected] 
6322e440c8aS[email protected]     log_info("num reports: %d, ", num_reports);
63357c9da5bS[email protected]     for (i=0; i<num_reports;i++){
63457c9da5bS[email protected]         int pos = 0;
63557c9da5bS[email protected]         uint8_t data_length = packet[4+num_reports*8+i];
6362b552b23S[email protected]         uint8_t event_size = 10 + data_length;
6372b552b23S[email protected]         uint8_t event[2 + event_size ];
6382b552b23S[email protected]         event[pos++] = GAP_LE_ADVERTISING_REPORT;
63957c9da5bS[email protected]         event[pos++] = event_size;
64057c9da5bS[email protected]         event[pos++] = packet[4+i]; // event_type;
64157c9da5bS[email protected]         event[pos++] = packet[4+num_reports+i]; // address_type;
642564fca32S[email protected]         memcpy(&event[pos], &packet[4+num_reports*2+i*6], 6); // bt address
64357c9da5bS[email protected]         pos += 6;
6442b552b23S[email protected]         event[pos++] = packet[4+num_reports*9+total_data_length + i];
6452e440c8aS[email protected]         log_info("rssi: %d, ", event[pos-1]);
64657c9da5bS[email protected]         event[pos++] = data_length;
64788ed79cfS[email protected]         memcpy(&event[pos], &packet[4+num_reports*9+data_offset], data_length);
64857c9da5bS[email protected]         data_offset += data_length;
64957c9da5bS[email protected]         pos += data_length;
65057c9da5bS[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
65157c9da5bS[email protected]     }
65257c9da5bS[email protected] }
653b2f949feS[email protected] #endif
65457c9da5bS[email protected] 
6556155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
656a2481739S[email protected]     uint8_t command_completed = 0;
657a2481739S[email protected]     if ((hci_stack->substate % 2) == 0) return;
6586155b3d3S[email protected]     // odd: waiting for event
6596155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
6606155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,3);
6616155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
662a2481739S[email protected]             command_completed = 1;
6636155b3d3S[email protected]             log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate);
6646155b3d3S[email protected]         } else {
6656155b3d3S[email protected]             log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
6666155b3d3S[email protected]         }
6676155b3d3S[email protected]     }
6686155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_STATUS){
6696155b3d3S[email protected]         uint8_t  status = packet[2];
6706155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,4);
6716155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
6726155b3d3S[email protected]             if (status){
673a2481739S[email protected]                 command_completed = 1;
6746155b3d3S[email protected]                 log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate);
6756155b3d3S[email protected]             } else {
6766155b3d3S[email protected]                 log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
6776155b3d3S[email protected]             }
6786155b3d3S[email protected]         } else {
6796155b3d3S[email protected]             log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
6806155b3d3S[email protected]         }
6816155b3d3S[email protected]     }
682a2481739S[email protected] 
683a2481739S[email protected]     if (!command_completed) return;
684a2481739S[email protected] 
685a2481739S[email protected]     switch(hci_stack->substate >> 1){
686a2481739S[email protected]         default:
687a2481739S[email protected]             hci_stack->substate++;
688a2481739S[email protected]             break;
6896155b3d3S[email protected]     }
6906155b3d3S[email protected] }
6916155b3d3S[email protected] 
6926155b3d3S[email protected] static void hci_initializing_state_machine(){
6936155b3d3S[email protected]         // log_info("hci_init: substate %u\n", hci_stack->substate);
6946155b3d3S[email protected]     if (hci_stack->substate % 2) {
6956155b3d3S[email protected]         // odd: waiting for command completion
6966155b3d3S[email protected]         return;
6976155b3d3S[email protected]     }
6986155b3d3S[email protected]     switch (hci_stack->substate >> 1){
6996155b3d3S[email protected]         case 0: // RESET
7006155b3d3S[email protected]             hci_state_reset();
7016155b3d3S[email protected] 
7026155b3d3S[email protected]             hci_send_cmd(&hci_reset);
7036155b3d3S[email protected]             if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
7046155b3d3S[email protected]                 // skip baud change
7056155b3d3S[email protected]                 hci_stack->substate = 4; // >> 1 = 2
7066155b3d3S[email protected]             }
7076155b3d3S[email protected]             break;
7086155b3d3S[email protected]         case 1: // SEND BAUD CHANGE
7096155b3d3S[email protected]             hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
71088789c61Smatthias.ringwald             hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
7116155b3d3S[email protected]             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
7126155b3d3S[email protected]             break;
7136155b3d3S[email protected]         case 2: // LOCAL BAUD CHANGE
7146155b3d3S[email protected]             log_info("Local baud rate change");
7156155b3d3S[email protected]             hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
7166155b3d3S[email protected]             hci_stack->substate += 2;
7176155b3d3S[email protected]             // break missing here for fall through
7186155b3d3S[email protected] 
7196155b3d3S[email protected]         case 3:
7206155b3d3S[email protected]             log_info("Custom init");
7216155b3d3S[email protected]             // Custom initialization
7226155b3d3S[email protected]             if (hci_stack->control && hci_stack->control->next_cmd){
7236155b3d3S[email protected]                 int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
7246155b3d3S[email protected]                 if (valid_cmd){
7256155b3d3S[email protected]                     int size = 3 + hci_stack->hci_packet_buffer[2];
7266155b3d3S[email protected]                     hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
7276155b3d3S[email protected]                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
7286155b3d3S[email protected]                     hci_stack->substate = 4; // more init commands
7296155b3d3S[email protected]                     break;
7306155b3d3S[email protected]                 }
7316155b3d3S[email protected]                 log_info("hci_run: init script done\n\r");
7326155b3d3S[email protected]             }
7336155b3d3S[email protected]             // otherwise continue
7346155b3d3S[email protected]             hci_send_cmd(&hci_read_bd_addr);
7356155b3d3S[email protected]             break;
7366155b3d3S[email protected]         case 4:
7376155b3d3S[email protected]             hci_send_cmd(&hci_read_buffer_size);
7386155b3d3S[email protected]             break;
7396155b3d3S[email protected]         case 5:
7406155b3d3S[email protected]             hci_send_cmd(&hci_read_local_supported_features);
7416155b3d3S[email protected]             break;
7426155b3d3S[email protected]         case 6:
7436155b3d3S[email protected]             if (hci_le_supported()){
7446155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
7456155b3d3S[email protected]             } else {
7466155b3d3S[email protected]                 // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
7476155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
7486155b3d3S[email protected]             }
7496155b3d3S[email protected] 
7506155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
7516155b3d3S[email protected]             if (!hci_classic_supported()){
7526155b3d3S[email protected]                 if (hci_le_supported()){
7536155b3d3S[email protected]                     hci_stack->substate = 11 << 1;    // skip all classic command
7546155b3d3S[email protected]                 } else {
7556155b3d3S[email protected]                     log_error("Neither BR/EDR nor LE supported");
7566155b3d3S[email protected]                     hci_stack->substate = 14 << 1;    // skip all
7576155b3d3S[email protected]                 }
7586155b3d3S[email protected]             }
7596155b3d3S[email protected]             break;
7606155b3d3S[email protected]         case 7:
7616155b3d3S[email protected]             if (hci_ssp_supported()){
7626155b3d3S[email protected]                 hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
7636155b3d3S[email protected]                 break;
7646155b3d3S[email protected]             }
7656155b3d3S[email protected]             hci_stack->substate += 2;
7666155b3d3S[email protected]             // break missing here for fall through
7676155b3d3S[email protected] 
7686155b3d3S[email protected]         case 8:
7696155b3d3S[email protected]             // ca. 15 sec
7706155b3d3S[email protected]             hci_send_cmd(&hci_write_page_timeout, 0x6000);
7716155b3d3S[email protected]             break;
7726155b3d3S[email protected]         case 9:
7736155b3d3S[email protected]             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
7746155b3d3S[email protected]             break;
7756155b3d3S[email protected]         case 10:
7766155b3d3S[email protected]             if (hci_stack->local_name){
7776155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
7786155b3d3S[email protected]             } else {
7796155b3d3S[email protected]                 char hostname[30];
7806155b3d3S[email protected] #ifdef EMBEDDED
7816155b3d3S[email protected]                 // BTstack-11:22:33:44:55:66
7826155b3d3S[email protected]                 strcpy(hostname, "BTstack ");
7836155b3d3S[email protected]                 strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
7846155b3d3S[email protected]                 log_info("---> Name %s", hostname);
7856155b3d3S[email protected] #else
7866155b3d3S[email protected]                 // hostname for POSIX systems
7876155b3d3S[email protected]                 gethostname(hostname, 30);
7886155b3d3S[email protected]                 hostname[29] = '\0';
7896155b3d3S[email protected] #endif
7906155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hostname);
7916155b3d3S[email protected]             }
7926155b3d3S[email protected]             break;
7936155b3d3S[email protected]         case 11:
7946155b3d3S[email protected]             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
7956155b3d3S[email protected]             if (!hci_le_supported()){
7966155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
7976155b3d3S[email protected]                 hci_stack->substate = 14 << 1;
7986155b3d3S[email protected]             }
7996155b3d3S[email protected]             break;
8006155b3d3S[email protected] 
8016155b3d3S[email protected] #ifdef HAVE_BLE
8026155b3d3S[email protected]         // LE INIT
8036155b3d3S[email protected]         case 12:
8046155b3d3S[email protected]             hci_send_cmd(&hci_le_read_buffer_size);
8056155b3d3S[email protected]             break;
8066155b3d3S[email protected]         case 13:
8076155b3d3S[email protected]             // LE Supported Host = 1, Simultaneous Host = 0
8086155b3d3S[email protected]             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
8096155b3d3S[email protected]             break;
8106155b3d3S[email protected]         case 14:
8116155b3d3S[email protected]             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
8126155b3d3S[email protected]             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
8136155b3d3S[email protected]             break;
8146155b3d3S[email protected] #endif
8156155b3d3S[email protected] 
8166155b3d3S[email protected]         // DONE
8176155b3d3S[email protected]         case 15:
8186155b3d3S[email protected]             // done.
8196155b3d3S[email protected]             hci_stack->state = HCI_STATE_WORKING;
8206155b3d3S[email protected]             hci_emit_state();
8216155b3d3S[email protected]             break;
8226155b3d3S[email protected]         default:
8236155b3d3S[email protected]             break;
8246155b3d3S[email protected]     }
8256155b3d3S[email protected]     hci_stack->substate++;
8266155b3d3S[email protected] }
8276155b3d3S[email protected] 
82874ec757aSmatthias.ringwald // avoid huge local variables
829223aafc1Smatthias.ringwald #ifndef EMBEDDED
83074ec757aSmatthias.ringwald static device_name_t device_name;
831223aafc1Smatthias.ringwald #endif
83216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
833e76a89eeS[email protected] 
834e76a89eeS[email protected]     uint16_t event_length = packet[1];
835e76a89eeS[email protected] 
836e76a89eeS[email protected]     // assert packet is complete
837e76a89eeS[email protected]     if (size != event_length + 2){
838e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
839e76a89eeS[email protected]         return;
840e76a89eeS[email protected]     }
841e76a89eeS[email protected] 
8421281a47eSmatthias.ringwald     bd_addr_t addr;
84396a45072S[email protected]     bd_addr_type_t addr_type;
8442d00edd4Smatthias.ringwald     uint8_t link_type;
845fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
8461f7b95a1Smatthias.ringwald     hci_connection_t * conn;
84756cf178bSmatthias.ringwald     int i;
84822909952Smatthias.ringwald 
8495909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
8505909f7f2Smatthias.ringwald 
8516772a24cSmatthias.ringwald     switch (packet[0]) {
85222909952Smatthias.ringwald 
8536772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
8547ec5eeaaSmatthias.ringwald             // get num cmd packets
8553a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
8563a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
8577ec5eeaaSmatthias.ringwald 
858e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
859e2edc0c3Smatthias.ringwald                 // from offset 5
860e2edc0c3Smatthias.ringwald                 // status
8611d279b20Smatthias.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"
8623a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
86356cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
864ee303eddS[email protected]                 hci_stack->acl_packets_total_num  = packet[9];
86556cf178bSmatthias.ringwald                 // ignore: total num SCO packets
8663a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
867a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
8683a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
8693a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
8708f8108aaSmatthias.ringwald                     }
87165389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
872ee303eddS[email protected]                              hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num);
873e2edc0c3Smatthias.ringwald                 }
87456cf178bSmatthias.ringwald             }
87565a46ef3S[email protected] #ifdef HAVE_BLE
87665a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
877*219eea5fS[email protected]                 hci_stack->le_data_packets_length = READ_BT_16(packet, 6);
878ee303eddS[email protected]                 hci_stack->le_acl_packets_total_num  = packet[8];
879*219eea5fS[email protected]                 log_info("hci_le_read_buffer_size: size %u, count %u\n", hci_stack->le_data_packets_length, hci_stack->le_acl_packets_total_num);
88065a46ef3S[email protected]             }
88165a46ef3S[email protected] #endif
882188981d2Smatthias.ringwald             // Dump local address
883188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
8843a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
885188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
8863a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
887188981d2Smatthias.ringwald             }
888381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
8893a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
890381fbed8Smatthias.ringwald             }
89165389bfcS[email protected]             // Note: HCI init checks
892559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
8933a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
894559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
8953a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
8963a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
8973a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
8983a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
89965389bfcS[email protected] 
90065389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
9013a9fb326S[email protected]                 hci_stack->packet_types = hci_acl_packet_types_for_buffer_size_and_local_features(hci_stack->acl_data_packet_length, &hci_stack->local_supported_features[0]);
9023a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
903f5d8d141S[email protected] 
904f5d8d141S[email protected]                 // Classic/LE
905f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
906559e517eS[email protected]             }
90756cf178bSmatthias.ringwald             break;
90856cf178bSmatthias.ringwald 
9097ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
9107ec5eeaaSmatthias.ringwald             // get num cmd packets
9113a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
9123a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
9137ec5eeaaSmatthias.ringwald             break;
9147ec5eeaaSmatthias.ringwald 
9152e440c8aS[email protected]         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{
9162e440c8aS[email protected]             int offset = 3;
91756cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
9182e440c8aS[email protected]                 handle = READ_BT_16(packet, offset);
9192e440c8aS[email protected]                 offset += 2;
9202e440c8aS[email protected]                 uint16_t num_packets = READ_BT_16(packet, offset);
9212e440c8aS[email protected]                 offset += 2;
9222e440c8aS[email protected] 
9235061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
92456cf178bSmatthias.ringwald                 if (!conn){
9257d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
92656cf178bSmatthias.ringwald                     continue;
92756cf178bSmatthias.ringwald                 }
92856cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
9297b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
93056cf178bSmatthias.ringwald             }
9316772a24cSmatthias.ringwald             break;
9322e440c8aS[email protected]         }
9331f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
93437eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
93537eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
9362d00edd4Smatthias.ringwald             link_type = packet[11];
937339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
93837eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
93996a45072S[email protected]                 conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
9401f7b95a1Smatthias.ringwald                 if (!conn) {
94196a45072S[email protected]                     conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
9421f7b95a1Smatthias.ringwald                 }
943ce4c8fabSmatthias.ringwald                 if (!conn) {
944ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
9453a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
9463a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
947ce4c8fabSmatthias.ringwald                     break;
948ce4c8fabSmatthias.ringwald                 }
94932ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
95032ab9390Smatthias.ringwald                 hci_run();
95137eaa4cfSmatthias.ringwald             } else {
952ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
9533a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
9543a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
95537eaa4cfSmatthias.ringwald             }
9561f7b95a1Smatthias.ringwald             break;
9571f7b95a1Smatthias.ringwald 
9586772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
959fe1ed1b8Smatthias.ringwald             // Connection management
960fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
961339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
96296a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
96396a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
964fe1ed1b8Smatthias.ringwald             if (conn) {
965b448a0e7Smatthias.ringwald                 if (!packet[2]){
966c8e4258aSmatthias.ringwald                     conn->state = OPEN;
967fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
968ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
969ee091cf1Smatthias.ringwald 
970c785ef68Smatthias.ringwald                     // restart timer
971c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
972ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
973c785ef68Smatthias.ringwald 
974339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
97543bfb1bdSmatthias.ringwald 
97643bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
977b448a0e7Smatthias.ringwald                 } else {
978ad83dc6aS[email protected]                     // notify client if dedicated bonding
979ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
980ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
981ad83dc6aS[email protected]                     }
982ad83dc6aS[email protected] 
983b448a0e7Smatthias.ringwald                     // connection failed, remove entry
9843a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
985a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
986c12e46e7Smatthias.ringwald 
987c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
988c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
989c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
990c12e46e7Smatthias.ringwald                     }
991fe1ed1b8Smatthias.ringwald                 }
992fe1ed1b8Smatthias.ringwald             }
9936772a24cSmatthias.ringwald             break;
994fe1ed1b8Smatthias.ringwald 
995afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
996afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
997afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
998afd4e962S[email protected]             if (!conn) break;
999afd4e962S[email protected]             if (!packet[2]){
1000afd4e962S[email protected]                 uint8_t * features = &packet[5];
1001afd4e962S[email protected]                 if (features[6] & (1 << 3)){
1002afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
1003afd4e962S[email protected]                 }
1004afd4e962S[email protected]             }
1005afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
1006ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
1007ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1008ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1009ad83dc6aS[email protected]             }
1010afd4e962S[email protected]             break;
1011afd4e962S[email protected] 
10127fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
10137b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
10147fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
101574d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
10163a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
101732ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
101864472d52Smatthias.ringwald             hci_run();
1019d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
102029d53098Smatthias.ringwald             return;
10217fde4af9Smatthias.ringwald 
10229ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
102329d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
102496a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
10259ab95c90S[email protected]             if (!conn) break;
10269ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
10277bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
10289ab95c90S[email protected]             // Change Connection Encryption keeps link key type
10299ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
10309ab95c90S[email protected]                 conn->link_key_type = link_key_type;
10319ab95c90S[email protected]             }
10323a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
10333a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
103429d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
10357fde4af9Smatthias.ringwald             break;
10369ab95c90S[email protected]         }
10377fde4af9Smatthias.ringwald 
10387fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
10396724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
10404c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
10413a9fb326S[email protected]             if (!hci_stack->bondable){
1042899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
1043f8fb5f6eS[email protected]                 hci_run();
1044f8fb5f6eS[email protected]                 return;
10454c57c146S[email protected]             }
1046d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
10473a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
1048d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
10493a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
10507fde4af9Smatthias.ringwald             break;
10517fde4af9Smatthias.ringwald 
10521d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
10531d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
1054dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
1055dbe1a790S[email protected]             break;
1056dbe1a790S[email protected] 
1057dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
10586724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
10593a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1060dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
1061dbe1a790S[email protected]             break;
1062dbe1a790S[email protected] 
1063dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
10646724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
10653a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1066dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
10671d6b20aeS[email protected]             break;
10681d6b20aeS[email protected] 
1069f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
1070f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
1071f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
1072f0944df2S[email protected]             if (!conn) break;
1073ad83dc6aS[email protected]             if (packet[2] == 0) {
1074f0944df2S[email protected]                 if (packet[5]){
1075f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
1076f0944df2S[email protected]                 } else {
1077536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
1078f0944df2S[email protected]                 }
1079ad83dc6aS[email protected]             }
1080a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
1081f0944df2S[email protected]             break;
1082f0944df2S[email protected] 
10831eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
10841eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
10851eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
10861eb2563eS[email protected]             if (!conn) break;
1087ad83dc6aS[email protected] 
1088ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
1089ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1090ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
1091ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
1092ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
1093ad83dc6aS[email protected]                 break;
1094ad83dc6aS[email protected]             }
1095ad83dc6aS[email protected] 
1096b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
10971eb2563eS[email protected]                 // link key sufficient for requested security
10981eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1099ad83dc6aS[email protected]                 break;
1100ad83dc6aS[email protected]             }
11011eb2563eS[email protected]             // not enough
11021eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
11031eb2563eS[email protected]             break;
110434d2123cS[email protected] 
1105223aafc1Smatthias.ringwald #ifndef EMBEDDED
110674ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
11073a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
110874ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
110974ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
11105a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
11115a06394aSmatthias.ringwald             for (i=0; i<248;i++){
11125a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
11135a06394aSmatthias.ringwald                     packet[9+i] = 0;
11145a06394aSmatthias.ringwald                     break;
1115cdc9101dSmatthias.ringwald                 }
11165a06394aSmatthias.ringwald             }
1117d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
111874ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
11193a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
112074ec757aSmatthias.ringwald             break;
112174ec757aSmatthias.ringwald 
112274ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
1123206a9031S[email protected]         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
11243a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
112574ec757aSmatthias.ringwald             // first send inq result packet
11263a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
112774ec757aSmatthias.ringwald             // then send cached remote names
1128206a9031S[email protected]             int offset = 3;
112974ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
1130206a9031S[email protected]                 bt_flip_addr(addr, &packet[offset]);
1131206a9031S[email protected]                 offset += 14; // 6 + 1 + 1 + 1 + 3 + 2;
11323a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
113374ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
113474ec757aSmatthias.ringwald                 }
113574ec757aSmatthias.ringwald             }
113674ec757aSmatthias.ringwald             return;
1137206a9031S[email protected]         }
1138223aafc1Smatthias.ringwald #endif
113974ec757aSmatthias.ringwald 
11406772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1141fe1ed1b8Smatthias.ringwald             if (!packet[2]){
1142fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
11435061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
1144fe1ed1b8Smatthias.ringwald                 if (conn) {
1145c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
1146fe1ed1b8Smatthias.ringwald                 }
1147fe1ed1b8Smatthias.ringwald             }
11486772a24cSmatthias.ringwald             break;
11496772a24cSmatthias.ringwald 
1150c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
11513a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
11523a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
11537586ee35S[email protected]             } else {
11547586ee35S[email protected]                 // if no special requests, just reboot stack
11557586ee35S[email protected]                 hci_power_control_off();
11567586ee35S[email protected]                 hci_power_control_on();
1157c68bdf90Smatthias.ringwald             }
1158c68bdf90Smatthias.ringwald             break;
1159c68bdf90Smatthias.ringwald 
11606b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
11616b4af23dS[email protected]             // free packet buffer for asynchronous transport
11626b4af23dS[email protected]             if (hci_transport_synchronous()) break;
11636b4af23dS[email protected]             hci_stack->hci_packet_buffer_reserved = 0;
11646b4af23dS[email protected]             break;
11656b4af23dS[email protected] 
11665909f7f2Smatthias.ringwald #ifdef HAVE_BLE
11675909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
11685909f7f2Smatthias.ringwald             switch (packet[2]){
116957c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
11702e440c8aS[email protected]                     log_info("advertising report received\n");
11717bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
117257c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
11737bdc6798S[email protected]                     break;
11745909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
11755909f7f2Smatthias.ringwald                     // Connection management
11765909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
11777bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
117896a45072S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr));
11795909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
118096a45072S[email protected]                     conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
11815909f7f2Smatthias.ringwald                     if (packet[3]){
11825909f7f2Smatthias.ringwald                         if (conn){
11835909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
11843a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
11855909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
11865909f7f2Smatthias.ringwald                         }
11875909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
11885909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
11895909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
11905909f7f2Smatthias.ringwald                         }
11915909f7f2Smatthias.ringwald                         break;
11925909f7f2Smatthias.ringwald                     }
11935909f7f2Smatthias.ringwald                     if (!conn){
119496a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
11955909f7f2Smatthias.ringwald                     }
11965909f7f2Smatthias.ringwald                     if (!conn){
11975909f7f2Smatthias.ringwald                         // no memory
11985909f7f2Smatthias.ringwald                         break;
11995909f7f2Smatthias.ringwald                     }
12005909f7f2Smatthias.ringwald 
12015909f7f2Smatthias.ringwald                     conn->state = OPEN;
12025909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
12035909f7f2Smatthias.ringwald 
12045909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
12055909f7f2Smatthias.ringwald 
12065909f7f2Smatthias.ringwald                     // restart timer
12075909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
12085909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
12095909f7f2Smatthias.ringwald 
12105909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
12115909f7f2Smatthias.ringwald 
12125909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
12135909f7f2Smatthias.ringwald                     break;
12145909f7f2Smatthias.ringwald 
121565a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
121665a46ef3S[email protected] 
12175909f7f2Smatthias.ringwald                 default:
12185909f7f2Smatthias.ringwald                     break;
12195909f7f2Smatthias.ringwald             }
12205909f7f2Smatthias.ringwald             break;
12215909f7f2Smatthias.ringwald #endif
12226772a24cSmatthias.ringwald         default:
12236772a24cSmatthias.ringwald             break;
1224fe1ed1b8Smatthias.ringwald     }
1225fe1ed1b8Smatthias.ringwald 
12263429f56bSmatthias.ringwald     // handle BT initialization
12273a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
12286155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
1229c9af4d3fS[email protected]     }
123022909952Smatthias.ringwald 
123189db417bSmatthias.ringwald     // help with BT sleep
12323a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
12333a9fb326S[email protected]         && hci_stack->substate == 1
123489db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
12353a9fb326S[email protected]         hci_stack->substate++;
123689db417bSmatthias.ringwald     }
123789db417bSmatthias.ringwald 
12383a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
123994ab26f8Smatthias.ringwald 
124094ab26f8Smatthias.ringwald 	// execute main loop
124194ab26f8Smatthias.ringwald 	hci_run();
124216833f0aSmatthias.ringwald }
124316833f0aSmatthias.ringwald 
12440a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
124510e830c9Smatthias.ringwald     switch (packet_type) {
124610e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
124710e830c9Smatthias.ringwald             event_handler(packet, size);
124810e830c9Smatthias.ringwald             break;
124910e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
125010e830c9Smatthias.ringwald             acl_handler(packet, size);
125110e830c9Smatthias.ringwald             break;
125210e830c9Smatthias.ringwald         default:
125310e830c9Smatthias.ringwald             break;
125410e830c9Smatthias.ringwald     }
125510e830c9Smatthias.ringwald }
125610e830c9Smatthias.ringwald 
1257fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
12582718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
12593a9fb326S[email protected]     hci_stack->packet_handler = handler;
126016833f0aSmatthias.ringwald }
126116833f0aSmatthias.ringwald 
12626155b3d3S[email protected] static void hci_state_reset(){
1263595bdbfbS[email protected]     // no connections yet
1264595bdbfbS[email protected]     hci_stack->connections = NULL;
126574308b23Smatthias.ringwald 
126674308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
126774308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
126874308b23Smatthias.ringwald     // hci_stack->connectable = 0;
126974308b23Smatthias.ringwald     // hci_stack->bondable = 1;
1270595bdbfbS[email protected] 
127144935e40S[email protected]     // buffer is free
127244935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
127344935e40S[email protected] 
1274595bdbfbS[email protected]     // no pending cmds
1275595bdbfbS[email protected]     hci_stack->decline_reason = 0;
1276595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
1277595bdbfbS[email protected] 
1278595bdbfbS[email protected]     // LE
1279595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
1280595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
1281595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
1282e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
1283595bdbfbS[email protected] }
1284595bdbfbS[email protected] 
12854f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
1286475c8125Smatthias.ringwald 
12873a9fb326S[email protected] #ifdef HAVE_MALLOC
12883a9fb326S[email protected]     if (!hci_stack) {
12893a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
12903a9fb326S[email protected]     }
12913a9fb326S[email protected] #else
12923a9fb326S[email protected]     hci_stack = &hci_stack_static;
12933a9fb326S[email protected] #endif
129466fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
12953a9fb326S[email protected] 
1296475c8125Smatthias.ringwald     // reference to use transport layer implementation
12973a9fb326S[email protected]     hci_stack->hci_transport = transport;
1298475c8125Smatthias.ringwald 
129911e23e5fSmatthias.ringwald     // references to used control implementation
13003a9fb326S[email protected]     hci_stack->control = control;
130111e23e5fSmatthias.ringwald 
130211e23e5fSmatthias.ringwald     // reference to used config
13033a9fb326S[email protected]     hci_stack->config = config;
130411e23e5fSmatthias.ringwald 
130516833f0aSmatthias.ringwald     // higher level handler
13063a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
130716833f0aSmatthias.ringwald 
1308404843c1Smatthias.ringwald     // store and open remote device db
13093a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
13103a9fb326S[email protected]     if (hci_stack->remote_device_db) {
13113a9fb326S[email protected]         hci_stack->remote_device_db->open();
1312404843c1Smatthias.ringwald     }
131329d53098Smatthias.ringwald 
13148fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
13153a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
13168fcba05dSmatthias.ringwald 
131716833f0aSmatthias.ringwald     // register packet handlers with transport
131810e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
1319f5454fc6Smatthias.ringwald 
13203a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
1321e2386ba1S[email protected] 
1322e2386ba1S[email protected]     // class of device
13233a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
1324a45d6b9fS[email protected] 
1325f20168b8Smatthias.ringwald     // bondable by default
1326f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
1327f20168b8Smatthias.ringwald 
132863048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
13293a9fb326S[email protected]     hci_stack->ssp_enable = 1;
13303a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
13313a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
13323a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
133369a97523S[email protected] 
1334595bdbfbS[email protected]     hci_state_reset();
1335475c8125Smatthias.ringwald }
1336475c8125Smatthias.ringwald 
1337404843c1Smatthias.ringwald void hci_close(){
1338404843c1Smatthias.ringwald     // close remote device db
13393a9fb326S[email protected]     if (hci_stack->remote_device_db) {
13403a9fb326S[email protected]         hci_stack->remote_device_db->close();
1341404843c1Smatthias.ringwald     }
13423a9fb326S[email protected]     while (hci_stack->connections) {
1343bc68afe2S[email protected]         // cancel all l2cap connections
1344bc68afe2S[email protected]         hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host
13453a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1346f5454fc6Smatthias.ringwald     }
1347f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
13483a9fb326S[email protected] 
13493a9fb326S[email protected] #ifdef HAVE_MALLOC
13503a9fb326S[email protected]     free(hci_stack);
13513a9fb326S[email protected] #endif
13523a9fb326S[email protected]     hci_stack = NULL;
1353404843c1Smatthias.ringwald }
1354404843c1Smatthias.ringwald 
13559e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
13569e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
13579e61646fS[email protected] }
13589e61646fS[email protected] 
135966fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
136066fb9560S[email protected]     disable_l2cap_timeouts = 1;
136166fb9560S[email protected] }
13628d213e1aSmatthias.ringwald // State-Module-Driver overview
13638d213e1aSmatthias.ringwald // state                    module  low-level
13648d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
13658d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
13668d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
13678d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1368d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1369d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1370c7e0c5f6Smatthias.ringwald 
137140d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
13727301ad89Smatthias.ringwald 
1373038bc64cSmatthias.ringwald     // power on
1374f9a30166Smatthias.ringwald     int err = 0;
13753a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
13763a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1377f9a30166Smatthias.ringwald     }
1378038bc64cSmatthias.ringwald     if (err){
13797d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1380038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1381038bc64cSmatthias.ringwald         return err;
1382038bc64cSmatthias.ringwald     }
1383038bc64cSmatthias.ringwald 
1384038bc64cSmatthias.ringwald     // open low-level device
13853a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1386038bc64cSmatthias.ringwald     if (err){
13877d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
13883a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
13893a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1390f9a30166Smatthias.ringwald         }
1391038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1392038bc64cSmatthias.ringwald         return err;
1393038bc64cSmatthias.ringwald     }
13948d213e1aSmatthias.ringwald     return 0;
13958d213e1aSmatthias.ringwald }
1396038bc64cSmatthias.ringwald 
139740d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
13988d213e1aSmatthias.ringwald 
13997b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
14009418f9c9Smatthias.ringwald 
14018d213e1aSmatthias.ringwald     // close low-level device
14023a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
14038d213e1aSmatthias.ringwald 
14047b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
14059418f9c9Smatthias.ringwald 
14068d213e1aSmatthias.ringwald     // power off
14073a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
14083a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
14098d213e1aSmatthias.ringwald     }
14109418f9c9Smatthias.ringwald 
14117b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
14129418f9c9Smatthias.ringwald 
14133a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
141472ea5239Smatthias.ringwald }
141572ea5239Smatthias.ringwald 
141640d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
141772ea5239Smatthias.ringwald 
14187b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
14193144bce4Smatthias.ringwald 
1420b429b9b7Smatthias.ringwald #if 0
1421b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1422b429b9b7Smatthias.ringwald 
142372ea5239Smatthias.ringwald     // close low-level device
14243a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1425b429b9b7Smatthias.ringwald #endif
142672ea5239Smatthias.ringwald 
142772ea5239Smatthias.ringwald     // sleep mode
14283a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
14293a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
143072ea5239Smatthias.ringwald     }
1431b429b9b7Smatthias.ringwald 
14323a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
14338d213e1aSmatthias.ringwald }
14348d213e1aSmatthias.ringwald 
143540d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1436b429b9b7Smatthias.ringwald 
14377b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1438b429b9b7Smatthias.ringwald 
1439b429b9b7Smatthias.ringwald     // wake on
14403a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
14413a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1442b429b9b7Smatthias.ringwald     }
1443b429b9b7Smatthias.ringwald 
1444b429b9b7Smatthias.ringwald #if 0
1445b429b9b7Smatthias.ringwald     // open low-level device
14463a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1447b429b9b7Smatthias.ringwald     if (err){
14487d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
14493a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
14503a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1451b429b9b7Smatthias.ringwald         }
1452b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1453b429b9b7Smatthias.ringwald         return err;
1454b429b9b7Smatthias.ringwald     }
1455b429b9b7Smatthias.ringwald #endif
1456b429b9b7Smatthias.ringwald 
1457b429b9b7Smatthias.ringwald     return 0;
1458b429b9b7Smatthias.ringwald }
1459b429b9b7Smatthias.ringwald 
146044935e40S[email protected] static void hci_power_transition_to_initializing(void){
146144935e40S[email protected]     // set up state machine
146244935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
146344935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
146444935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
146544935e40S[email protected]     hci_stack->substate = 0;
146644935e40S[email protected] }
1467b429b9b7Smatthias.ringwald 
14688d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1469d661ed19Smatthias.ringwald 
14703a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1471d661ed19Smatthias.ringwald 
14728d213e1aSmatthias.ringwald     int err = 0;
14733a9fb326S[email protected]     switch (hci_stack->state){
14748d213e1aSmatthias.ringwald 
14758d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
14768d213e1aSmatthias.ringwald             switch (power_mode){
14778d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
14788d213e1aSmatthias.ringwald                     err = hci_power_control_on();
147997b61c7bS[email protected]                     if (err) {
148097b61c7bS[email protected]                         log_error("hci_power_control_on() error %u", err);
148197b61c7bS[email protected]                         return err;
148297b61c7bS[email protected]                     }
148344935e40S[email protected]                     hci_power_transition_to_initializing();
14848d213e1aSmatthias.ringwald                     break;
14858d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14868d213e1aSmatthias.ringwald                     // do nothing
14878d213e1aSmatthias.ringwald                     break;
14888d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1489b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
14908d213e1aSmatthias.ringwald                     break;
14918d213e1aSmatthias.ringwald             }
14928d213e1aSmatthias.ringwald             break;
14937301ad89Smatthias.ringwald 
14948d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
14958d213e1aSmatthias.ringwald             switch (power_mode){
14968d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
14978d213e1aSmatthias.ringwald                     // do nothing
14988d213e1aSmatthias.ringwald                     break;
14998d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
15008d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
15018d213e1aSmatthias.ringwald                     hci_power_control_off();
15028d213e1aSmatthias.ringwald                     break;
15038d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1504b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
150572ea5239Smatthias.ringwald                     hci_power_control_sleep();
15068d213e1aSmatthias.ringwald                     break;
15078d213e1aSmatthias.ringwald             }
15088d213e1aSmatthias.ringwald             break;
15097301ad89Smatthias.ringwald 
15108d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
15118d213e1aSmatthias.ringwald             switch (power_mode){
15128d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
15138d213e1aSmatthias.ringwald                     // do nothing
15148d213e1aSmatthias.ringwald                     break;
15158d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1516c7e0c5f6Smatthias.ringwald                     // see hci_run
15173a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
15188d213e1aSmatthias.ringwald                     break;
15198d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1520b546ac54Smatthias.ringwald                     // see hci_run
15213a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
15223a9fb326S[email protected]                     hci_stack->substate = 0;
15238d213e1aSmatthias.ringwald                     break;
15248d213e1aSmatthias.ringwald             }
15258d213e1aSmatthias.ringwald             break;
15267301ad89Smatthias.ringwald 
15278d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
15288d213e1aSmatthias.ringwald             switch (power_mode){
15298d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
153044935e40S[email protected]                     hci_power_transition_to_initializing();
15318d213e1aSmatthias.ringwald                     break;
15328d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
15338d213e1aSmatthias.ringwald                     // do nothing
15348d213e1aSmatthias.ringwald                     break;
15358d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1536b546ac54Smatthias.ringwald                     // see hci_run
15373a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
15383a9fb326S[email protected]                     hci_stack->substate = 0;
15398d213e1aSmatthias.ringwald                     break;
15408d213e1aSmatthias.ringwald             }
15418d213e1aSmatthias.ringwald             break;
15428d213e1aSmatthias.ringwald 
15438d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
15448d213e1aSmatthias.ringwald             switch (power_mode){
15458d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
154628171530Smatthias.ringwald 
154728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
154828171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
154928171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
15503a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
15513a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
155228171530Smatthias.ringwald                         break;
155328171530Smatthias.ringwald                     }
155428171530Smatthias.ringwald #endif
155544935e40S[email protected]                     hci_power_transition_to_initializing();
15568d213e1aSmatthias.ringwald                     break;
15578d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1558b546ac54Smatthias.ringwald                     // see hci_run
15593a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
15608d213e1aSmatthias.ringwald                     break;
15618d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1562b546ac54Smatthias.ringwald                     // do nothing
15638d213e1aSmatthias.ringwald                     break;
15648d213e1aSmatthias.ringwald             }
15658d213e1aSmatthias.ringwald             break;
15668d213e1aSmatthias.ringwald 
15678d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
15688d213e1aSmatthias.ringwald             switch (power_mode){
15698d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
157028171530Smatthias.ringwald 
157128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
157228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
157328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
15743a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
15753a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1576758b46ceSmatthias.ringwald                         hci_update_scan_enable();
157728171530Smatthias.ringwald                         break;
157828171530Smatthias.ringwald                     }
157928171530Smatthias.ringwald #endif
15803144bce4Smatthias.ringwald                     err = hci_power_control_wake();
15813144bce4Smatthias.ringwald                     if (err) return err;
158244935e40S[email protected]                     hci_power_transition_to_initializing();
15838d213e1aSmatthias.ringwald                     break;
15848d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
15853a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
15868d213e1aSmatthias.ringwald                     break;
15878d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1588b546ac54Smatthias.ringwald                     // do nothing
15898d213e1aSmatthias.ringwald                     break;
15908d213e1aSmatthias.ringwald             }
15918d213e1aSmatthias.ringwald             break;
159211e23e5fSmatthias.ringwald     }
159368d92d03Smatthias.ringwald 
1594038bc64cSmatthias.ringwald     // create internal event
1595ee8bf225Smatthias.ringwald 	hci_emit_state();
1596ee8bf225Smatthias.ringwald 
159768d92d03Smatthias.ringwald 	// trigger next/first action
159868d92d03Smatthias.ringwald 	hci_run();
159968d92d03Smatthias.ringwald 
1600475c8125Smatthias.ringwald     return 0;
1601475c8125Smatthias.ringwald }
1602475c8125Smatthias.ringwald 
1603758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1604758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
16053a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1606758b46ceSmatthias.ringwald     hci_run();
1607758b46ceSmatthias.ringwald }
1608758b46ceSmatthias.ringwald 
1609381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1610381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1611381fbed8Smatthias.ringwald 
16123a9fb326S[email protected]     if (hci_stack->discoverable == enable){
16133a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1614381fbed8Smatthias.ringwald         return;
1615381fbed8Smatthias.ringwald     }
1616381fbed8Smatthias.ringwald 
16173a9fb326S[email protected]     hci_stack->discoverable = enable;
1618758b46ceSmatthias.ringwald     hci_update_scan_enable();
1619758b46ceSmatthias.ringwald }
1620b031bebbSmatthias.ringwald 
1621758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1622758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1623758b46ceSmatthias.ringwald 
1624758b46ceSmatthias.ringwald     // don't emit event
16253a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1626758b46ceSmatthias.ringwald 
16273a9fb326S[email protected]     hci_stack->connectable = enable;
1628758b46ceSmatthias.ringwald     hci_update_scan_enable();
1629381fbed8Smatthias.ringwald }
1630381fbed8Smatthias.ringwald 
16315061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
16323a9fb326S[email protected]     return &hci_stack->local_bd_addr;
16335061f3afS[email protected] }
16345061f3afS[email protected] 
163506b35ec0Smatthias.ringwald void hci_run(){
16368a485f27Smatthias.ringwald 
163732ab9390Smatthias.ringwald     hci_connection_t * connection;
163832ab9390Smatthias.ringwald     linked_item_t * it;
163932ab9390Smatthias.ringwald 
1640d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()) return;
1641ce4c8fabSmatthias.ringwald 
1642b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1643b031bebbSmatthias.ringwald 
1644b031bebbSmatthias.ringwald     // decline incoming connections
16453a9fb326S[email protected]     if (hci_stack->decline_reason){
16463a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
16473a9fb326S[email protected]         hci_stack->decline_reason = 0;
16483a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1649dbe1a790S[email protected]         return;
1650ce4c8fabSmatthias.ringwald     }
1651ce4c8fabSmatthias.ringwald 
1652b031bebbSmatthias.ringwald     // send scan enable
16533a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
16543a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
16553a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1656dbe1a790S[email protected]         return;
1657b031bebbSmatthias.ringwald     }
1658b031bebbSmatthias.ringwald 
1659b2f949feS[email protected] #ifdef HAVE_BLE
16607bdc6798S[email protected]     // handle le scan
16617bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
16627bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
16637bdc6798S[email protected]             case LE_START_SCAN:
16647bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
16657bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
16667bdc6798S[email protected]                 return;
16677bdc6798S[email protected] 
16687bdc6798S[email protected]             case LE_STOP_SCAN:
16697bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
16707bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
16717bdc6798S[email protected]                 return;
16727bdc6798S[email protected]             default:
16737bdc6798S[email protected]                 break;
16747bdc6798S[email protected]         }
1675e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
1676e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
1677e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
1678e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
1679e2602ea2Smatthias.ringwald             hci_send_cmd(&hci_le_set_scan_parameters, scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, hci_stack->adv_addr_type, 0);
1680e2602ea2Smatthias.ringwald             return;
1681e2602ea2Smatthias.ringwald         }
16827bdc6798S[email protected]     }
1683b2f949feS[email protected] #endif
16847bdc6798S[email protected] 
168532ab9390Smatthias.ringwald     // send pending HCI commands
16863a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
1687b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
168832ab9390Smatthias.ringwald 
16890bf6344aS[email protected]         switch(connection->state){
16900bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
16914f3229d8S[email protected]                 switch(connection->address_type){
16924f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
1693564fca32S[email protected]                         log_info("sending hci_create_connection\n");
1694ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
16954f3229d8S[email protected]                         break;
16964f3229d8S[email protected]                     default:
1697b2f949feS[email protected] #ifdef HAVE_BLE
1698564fca32S[email protected]                         log_info("sending hci_le_create_connection\n");
16994f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
1700b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
1701b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
17024f3229d8S[email protected]                                      0,         // don't use whitelist
17034f3229d8S[email protected]                                      connection->address_type, // peer address type
17044f3229d8S[email protected]                                      connection->address,      // peer bd addr
1705b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
1706b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
1707b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
17084f3229d8S[email protected]                                      0,         // conn latency
1709b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
1710b2571179Smatthias.ringwald                                      0x0001,    // min ce length
1711b2571179Smatthias.ringwald                                      0x0001     // max ce length
17124f3229d8S[email protected]                                      );
17134f3229d8S[email protected] 
17144f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
1715b2f949feS[email protected] #endif
17164f3229d8S[email protected]                         break;
17174f3229d8S[email protected]                 }
1718ad83dc6aS[email protected]                 return;
1719ad83dc6aS[email protected] 
17200bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
17217b5fbe1fSmatthias.ringwald                 log_info("sending hci_accept_connection_request\n");
172232ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
172334d2123cS[email protected]                 hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1724dbe1a790S[email protected]                 return;
17250bf6344aS[email protected] 
1726a6725849S[email protected] #ifdef HAVE_BLE
17270bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
17280bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
17290bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
17300bf6344aS[email protected]                 return;
1731a6725849S[email protected] #endif
17320bf6344aS[email protected]             case SEND_DISCONNECT:
17330bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
17347851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
17350bf6344aS[email protected]                 return;
17360bf6344aS[email protected] 
17370bf6344aS[email protected]             default:
17380bf6344aS[email protected]                 break;
1739c7e0c5f6Smatthias.ringwald         }
1740c7e0c5f6Smatthias.ringwald 
174132ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
174234d2123cS[email protected]             log_info("responding to link key request\n");
174334d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
174432ab9390Smatthias.ringwald             link_key_t link_key;
1745c77e8838S[email protected]             link_key_type_t link_key_type;
17463a9fb326S[email protected]             if ( hci_stack->remote_device_db
17473a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
174834d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
17499ab95c90S[email protected]                connection->link_key_type = link_key_type;
175032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
175132ab9390Smatthias.ringwald             } else {
175232ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
175332ab9390Smatthias.ringwald             }
1754dbe1a790S[email protected]             return;
175532ab9390Smatthias.ringwald         }
17561d6b20aeS[email protected] 
1757899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
17584c57c146S[email protected]             log_info("denying to pin request\n");
1759899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
176034d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
17614c57c146S[email protected]             return;
17624c57c146S[email protected]         }
17634c57c146S[email protected] 
1764dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
176534d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
176682d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
176782d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1768106d6d11S[email protected]                 // tweak authentication requirements
17693a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1770106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
17719faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
17729faad3abS[email protected]                 }
17739faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
17749faad3abS[email protected]                     authreq |= 1;
1775106d6d11S[email protected]                 }
17763a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1777f8fb5f6eS[email protected]             } else {
1778f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1779f8fb5f6eS[email protected]             }
1780dbe1a790S[email protected]             return;
178132ab9390Smatthias.ringwald         }
178232ab9390Smatthias.ringwald 
1783dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1784dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
178534d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1786dbe1a790S[email protected]             return;
1787dbe1a790S[email protected]         }
1788dbe1a790S[email protected] 
1789dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1790dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
179134d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1792dbe1a790S[email protected]             return;
1793dbe1a790S[email protected]         }
1794afd4e962S[email protected] 
1795afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1796afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
179734d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
17982bd8b7e7S[email protected]             return;
17992bd8b7e7S[email protected]         }
18002bd8b7e7S[email protected] 
18012bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
18022bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
180334d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
180434d2123cS[email protected]             return;
180534d2123cS[email protected]         }
1806ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1807ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
180852d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
1809ad83dc6aS[email protected]             return;
1810ad83dc6aS[email protected]         }
181134d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
181234d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
181334d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
18142bd8b7e7S[email protected]             return;
1815afd4e962S[email protected]         }
1816dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1817dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1818dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1819dce78009S[email protected]             return;
1820dce78009S[email protected]         }
1821dbe1a790S[email protected]     }
1822c7e0c5f6Smatthias.ringwald 
18233a9fb326S[email protected]     switch (hci_stack->state){
18243429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
1825bd88fde9S[email protected]             hci_initializing_state_machine();
18263429f56bSmatthias.ringwald             break;
1827c7e0c5f6Smatthias.ringwald 
1828c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1829c7e0c5f6Smatthias.ringwald 
18307b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1831c7e0c5f6Smatthias.ringwald             // close all open connections
18323a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1833c7e0c5f6Smatthias.ringwald             if (connection){
183432ab9390Smatthias.ringwald 
1835c7e0c5f6Smatthias.ringwald                 // send disconnect
1836d94d3cafS[email protected]                 if (!hci_can_send_command_packet_now()) return;
183732ab9390Smatthias.ringwald 
183879bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
18396ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1840c7e0c5f6Smatthias.ringwald 
1841c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1842c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1843c7e0c5f6Smatthias.ringwald                 return;
1844c7e0c5f6Smatthias.ringwald             }
18457b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1846c7e0c5f6Smatthias.ringwald 
184772ea5239Smatthias.ringwald             // switch mode
1848c7e0c5f6Smatthias.ringwald             hci_power_control_off();
18499418f9c9Smatthias.ringwald 
18507b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
185172ea5239Smatthias.ringwald             hci_emit_state();
18527b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
185372ea5239Smatthias.ringwald             break;
1854c7e0c5f6Smatthias.ringwald 
185572ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
18563a9fb326S[email protected]             switch(hci_stack->substate) {
185789db417bSmatthias.ringwald                 case 0:
18587b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
185972ea5239Smatthias.ringwald                     // close all open connections
18603a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
186166da7044Smatthias.ringwald 
186228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
186366da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
186466da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
186566da7044Smatthias.ringwald                         connection = NULL;
186666da7044Smatthias.ringwald                     }
186766da7044Smatthias.ringwald #endif
186872ea5239Smatthias.ringwald                     if (connection){
186932ab9390Smatthias.ringwald 
187072ea5239Smatthias.ringwald                         // send disconnect
1871d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
187232ab9390Smatthias.ringwald 
187379bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
18746ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
187572ea5239Smatthias.ringwald 
187672ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
187772ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
187872ea5239Smatthias.ringwald                         return;
187972ea5239Smatthias.ringwald                     }
188072ea5239Smatthias.ringwald 
188192368cd3S[email protected]                     if (hci_classic_supported()){
188289db417bSmatthias.ringwald                         // disable page and inquiry scan
1883d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
188432ab9390Smatthias.ringwald 
188592368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
18863a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
188789db417bSmatthias.ringwald 
188889db417bSmatthias.ringwald                         // continue in next sub state
18893a9fb326S[email protected]                         hci_stack->substate++;
189089db417bSmatthias.ringwald                         break;
189192368cd3S[email protected]                     }
189292368cd3S[email protected]                     // fall through for ble-only chips
189392368cd3S[email protected] 
189489db417bSmatthias.ringwald                 case 2:
18957b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
189628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
189728171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
189828171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
189928171530Smatthias.ringwald                         // SLEEP MODE reached
19003a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
190128171530Smatthias.ringwald                         hci_emit_state();
190228171530Smatthias.ringwald                         break;
190328171530Smatthias.ringwald                     }
190428171530Smatthias.ringwald #endif
190572ea5239Smatthias.ringwald                     // switch mode
19063a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1907c7e0c5f6Smatthias.ringwald                     hci_emit_state();
190828171530Smatthias.ringwald                     break;
190928171530Smatthias.ringwald 
191089db417bSmatthias.ringwald                 default:
191189db417bSmatthias.ringwald                     break;
191289db417bSmatthias.ringwald             }
1913c7e0c5f6Smatthias.ringwald             break;
1914c7e0c5f6Smatthias.ringwald 
19153429f56bSmatthias.ringwald         default:
19163429f56bSmatthias.ringwald             break;
19171f504dbdSmatthias.ringwald     }
19183429f56bSmatthias.ringwald }
191916833f0aSmatthias.ringwald 
192031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1921c8e4258aSmatthias.ringwald     bd_addr_t addr;
1922c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1923c8e4258aSmatthias.ringwald     // house-keeping
1924c8e4258aSmatthias.ringwald 
1925c8e4258aSmatthias.ringwald     // create_connection?
1926c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1927c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1928339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1929c8e4258aSmatthias.ringwald 
193096a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1931ad83dc6aS[email protected]         if (!conn){
193296a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
193317f1ba2aSmatthias.ringwald             if (!conn){
193417f1ba2aSmatthias.ringwald                 // notify client that alloc failed
193517f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
193617f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
193717f1ba2aSmatthias.ringwald             }
1938ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1939ad83dc6aS[email protected]         }
1940ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1941ad83dc6aS[email protected]         switch (conn->state){
1942ad83dc6aS[email protected]             // if connection active exists
1943ad83dc6aS[email protected]             case OPEN:
194462bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1945ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
194662bda3fbS[email protected]                 return 0;
1947ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1948ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1949ad83dc6aS[email protected]                 break;
1950ad83dc6aS[email protected]             default:
1951ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1952ad83dc6aS[email protected]                 return 0;
1953ad83dc6aS[email protected]         }
1954c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1955c8e4258aSmatthias.ringwald     }
1956c8e4258aSmatthias.ringwald 
19577fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
19587fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
19597fde4af9Smatthias.ringwald     }
19607fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
19617fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
19627fde4af9Smatthias.ringwald     }
19637fde4af9Smatthias.ringwald 
19648ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
19653a9fb326S[email protected]         if (hci_stack->remote_device_db){
19668ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
19673a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
19688ef73945Smatthias.ringwald         }
19698ef73945Smatthias.ringwald     }
1970c8e4258aSmatthias.ringwald 
19716724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
19726724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
19736724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
197496a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
19756724cd9eS[email protected]         if (conn){
19766724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
19776724cd9eS[email protected]         }
19786724cd9eS[email protected]     }
19796724cd9eS[email protected] 
19806724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
19816724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
19826724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
19836724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
19846724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
198596a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
19866724cd9eS[email protected]         if (conn){
19876724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
19886724cd9eS[email protected]         }
19896724cd9eS[email protected]     }
19906724cd9eS[email protected] 
199169a97523S[email protected] #ifdef HAVE_BLE
199269a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
19933a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
199469a97523S[email protected]     }
199569a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
19963a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
199769a97523S[email protected]     }
199869a97523S[email protected] #endif
199969a97523S[email protected] 
20003a9fb326S[email protected]     hci_stack->num_cmd_packets--;
20016b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
20026b4af23dS[email protected] 
20036b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
2004c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
20056b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
20066b4af23dS[email protected]     }
20076b4af23dS[email protected] 
20086b4af23dS[email protected]     return err;
200931452debSmatthias.ringwald }
20108adf0ddaSmatthias.ringwald 
20112bd8b7e7S[email protected] // disconnect because of security block
20122bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
20132bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
20142bd8b7e7S[email protected]     if (!connection) return;
20152bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
20162bd8b7e7S[email protected] }
20172bd8b7e7S[email protected] 
20182bd8b7e7S[email protected] 
2019dbe1a790S[email protected] // Configure Secure Simple Pairing
2020dbe1a790S[email protected] 
2021dbe1a790S[email protected] // enable will enable SSP during init
2022dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
20233a9fb326S[email protected]     hci_stack->ssp_enable = enable;
2024dbe1a790S[email protected] }
2025dbe1a790S[email protected] 
20262bd8b7e7S[email protected] int hci_local_ssp_activated(){
20273a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
20282bd8b7e7S[email protected] }
20292bd8b7e7S[email protected] 
2030dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
2031dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
20323a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
2033dbe1a790S[email protected] }
2034dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
20353a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
2036dbe1a790S[email protected] }
2037dbe1a790S[email protected] 
2038dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
2039dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
20403a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
2041dbe1a790S[email protected] }
2042dbe1a790S[email protected] 
20431cd208adSmatthias.ringwald /**
20441cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
20451cd208adSmatthias.ringwald  */
2046fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
20475127cc62S[email protected] 
2048d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()){
20499d14b626S[email protected]         log_error("hci_send_cmd called but cannot send packet now");
20509d14b626S[email protected]         return 0;
20519d14b626S[email protected]     }
20529d14b626S[email protected] 
20535127cc62S[email protected]     // for HCI INITIALIZATION
20545127cc62S[email protected]     // printf("hci_send_cmd: opcode %04x\n", cmd->opcode);
20555127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
20565127cc62S[email protected] 
20579d14b626S[email protected]     hci_reserve_packet_buffer();
20589d14b626S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
20599d14b626S[email protected] 
20601cd208adSmatthias.ringwald     va_list argptr;
20611cd208adSmatthias.ringwald     va_start(argptr, cmd);
20629d14b626S[email protected]     uint16_t size = hci_create_cmd_internal(packet, cmd, argptr);
20631cd208adSmatthias.ringwald     va_end(argptr);
20649d14b626S[email protected] 
20659d14b626S[email protected]     return hci_send_cmd_packet(packet, size);
206693b8dc03Smatthias.ringwald }
2067c8e4258aSmatthias.ringwald 
2068ee091cf1Smatthias.ringwald // Create various non-HCI events.
2069ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
2070ee091cf1Smatthias.ringwald 
2071c8e4258aSmatthias.ringwald void hci_emit_state(){
20723a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
2073425d1371Smatthias.ringwald     uint8_t event[3];
207480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
2075425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
20763a9fb326S[email protected]     event[2] = hci_stack->state;
2077425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20783a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2079c8e4258aSmatthias.ringwald }
2080c8e4258aSmatthias.ringwald 
208117f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
2082425d1371Smatthias.ringwald     uint8_t event[13];
2083c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
2084425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
208517f1ba2aSmatthias.ringwald     event[2] = status;
2086c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
2087c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
2088c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
2089c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
2090425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20913a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2092c8e4258aSmatthias.ringwald }
2093c8e4258aSmatthias.ringwald 
20944f3229d8S[email protected] void hci_emit_le_connection_complete(hci_connection_t *conn, uint8_t status){
20954f3229d8S[email protected]     uint8_t event[21];
20964f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
20974f3229d8S[email protected]     event[1] = sizeof(event) - 2;
20984f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
20994f3229d8S[email protected]     event[3] = status;
21004f3229d8S[email protected]     bt_store_16(event, 4, conn->con_handle);
21014f3229d8S[email protected]     event[6] = 0; // TODO: role
21024f3229d8S[email protected]     event[7] = conn->address_type;
21034f3229d8S[email protected]     bt_flip_addr(&event[8], conn->address);
21044f3229d8S[email protected]     bt_store_16(event, 14, 0); // interval
21054f3229d8S[email protected]     bt_store_16(event, 16, 0); // latency
21064f3229d8S[email protected]     bt_store_16(event, 18, 0); // supervision timeout
21074f3229d8S[email protected]     event[20] = 0; // master clock accuracy
21084f3229d8S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
21094f3229d8S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
21104f3229d8S[email protected] }
21114f3229d8S[email protected] 
21123c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
2113425d1371Smatthias.ringwald     uint8_t event[6];
21143c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
2115e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
21163c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
21173c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
21183c4d4b90Smatthias.ringwald     event[5] = reason;
2119425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
21203a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
21213c4d4b90Smatthias.ringwald }
21223c4d4b90Smatthias.ringwald 
2123ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
212466fb9560S[email protected]     if (disable_l2cap_timeouts) return;
2125e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
2126425d1371Smatthias.ringwald     uint8_t event[4];
212780d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
2128425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2129ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
2130425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
21313a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2132ee091cf1Smatthias.ringwald }
213343bfb1bdSmatthias.ringwald 
213443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
2135e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
2136425d1371Smatthias.ringwald     uint8_t event[3];
213780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
2138425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
213943bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
2140425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21413a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
214243bfb1bdSmatthias.ringwald }
2143038bc64cSmatthias.ringwald 
2144038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
2145e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
2146425d1371Smatthias.ringwald     uint8_t event[2];
214780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
2148425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2149425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21503a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2151038bc64cSmatthias.ringwald }
21521b0e3922Smatthias.ringwald 
215309ba8edeSmatthias.ringwald #ifndef EMBEDDED
21541b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
2155e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
2156425d1371Smatthias.ringwald     uint8_t event[6];
21571b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
2158425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2159425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
2160425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
2161425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
2162425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21633a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
21641b0e3922Smatthias.ringwald }
216509ba8edeSmatthias.ringwald #endif
21661b0e3922Smatthias.ringwald 
21672ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
2168e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
2169425d1371Smatthias.ringwald     uint8_t event[3];
21702ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
2171425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
21722ed6235cSmatthias.ringwald     event[2] = enabled;
2173425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21743a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
21752ed6235cSmatthias.ringwald }
2176627c2f45Smatthias.ringwald 
2177627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
2178e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
2179627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
2180e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
2181f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
21822f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
2183f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
2184e0abb8e7S[email protected] 
2185e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
2186e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
2187e0abb8e7S[email protected] 
2188e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
21893a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
2190627c2f45Smatthias.ringwald }
2191381fbed8Smatthias.ringwald 
2192381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
2193e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
2194425d1371Smatthias.ringwald     uint8_t event[3];
2195381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
2196425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2197381fbed8Smatthias.ringwald     event[2] = enabled;
2198425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21993a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2200381fbed8Smatthias.ringwald }
2201458bf4e8S[email protected] 
2202a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
2203df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
2204a00031e2S[email protected]     uint8_t event[5];
2205e00caf9cS[email protected]     int pos = 0;
2206a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
2207e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
2208a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
2209e00caf9cS[email protected]     pos += 2;
2210e00caf9cS[email protected]     event[pos++] = level;
2211e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22123a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2213e00caf9cS[email protected] }
2214e00caf9cS[email protected] 
2215ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
2216ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
2217ad83dc6aS[email protected]     uint8_t event[9];
2218ad83dc6aS[email protected]     int pos = 0;
2219ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
2220ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
2221ad83dc6aS[email protected]     event[pos++] = status;
2222ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
2223ad83dc6aS[email protected]     pos += 6;
2224ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22253a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2226ad83dc6aS[email protected] }
2227ad83dc6aS[email protected] 
22282bd8b7e7S[email protected] // query if remote side supports SSP
22292bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
22302bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
22312bd8b7e7S[email protected]     if (!connection) return 0;
22322bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
22332bd8b7e7S[email protected] }
22342bd8b7e7S[email protected] 
2235df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
2236df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
2237df3354fcS[email protected] }
2238df3354fcS[email protected] 
2239458bf4e8S[email protected] // GAP API
2240458bf4e8S[email protected] /**
2241458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
2242458bf4e8S[email protected]  * @praram enabled
2243458bf4e8S[email protected]  */
22444c57c146S[email protected] void gap_set_bondable_mode(int enable){
22453a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
2246458bf4e8S[email protected] }
2247cb230b9dS[email protected] 
2248cb230b9dS[email protected] /**
224934d2123cS[email protected]  * @brief map link keys to security levels
2250cb230b9dS[email protected]  */
225134d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
225234d2123cS[email protected]     switch (link_key_type){
22533c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
22543c68dfa9S[email protected]             return LEVEL_4;
22553c68dfa9S[email protected]         case COMBINATION_KEY:
22563c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
22573c68dfa9S[email protected]             return LEVEL_3;
22583c68dfa9S[email protected]         default:
22593c68dfa9S[email protected]             return LEVEL_2;
22603c68dfa9S[email protected]     }
2261cb230b9dS[email protected] }
2262cb230b9dS[email protected] 
2263a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
226434d2123cS[email protected]     if (!connection) return LEVEL_0;
226534d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
226634d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
226734d2123cS[email protected] }
226834d2123cS[email protected] 
226934d2123cS[email protected] 
2270106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
22715127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
2272106d6d11S[email protected]     return level > LEVEL_2;
2273106d6d11S[email protected] }
2274106d6d11S[email protected] 
227534d2123cS[email protected] /**
227634d2123cS[email protected]  * @brief get current security level
227734d2123cS[email protected]  */
227834d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
227934d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
228034d2123cS[email protected]     if (!connection) return LEVEL_0;
228134d2123cS[email protected]     return gap_security_level_for_connection(connection);
228234d2123cS[email protected] }
228334d2123cS[email protected] 
2284cb230b9dS[email protected] /**
2285cb230b9dS[email protected]  * @brief request connection to device to
2286cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
2287cb230b9dS[email protected]  */
228834d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
228934d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
229034d2123cS[email protected]     if (!connection){
2291a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
229234d2123cS[email protected]         return;
229334d2123cS[email protected]     }
229434d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
229534d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
229634d2123cS[email protected]     if (current_level >= requested_level){
2297a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
229834d2123cS[email protected]         return;
229934d2123cS[email protected]     }
2300a00031e2S[email protected] 
230134d2123cS[email protected]     connection->requested_security_level = requested_level;
2302a00031e2S[email protected] 
2303fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
23043a9fb326S[email protected]     if (hci_stack->remote_device_db){
2305a00031e2S[email protected]         link_key_type_t link_key_type;
2306a00031e2S[email protected]         link_key_t      link_key;
23073a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2308a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2309a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2310a00031e2S[email protected]                 return;
2311a00031e2S[email protected]             }
2312a00031e2S[email protected]         }
2313a00031e2S[email protected]     }
2314a00031e2S[email protected] 
23151eb2563eS[email protected]     // try to authenticate connection
23161eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2317e80b2cf9S[email protected]     hci_run();
2318e00caf9cS[email protected] }
2319ad83dc6aS[email protected] 
2320ad83dc6aS[email protected] /**
2321ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2322ad83dc6aS[email protected]  * @param device
2323ad83dc6aS[email protected]  * @param request MITM protection
2324ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2325ad83dc6aS[email protected]  */
2326ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2327ad83dc6aS[email protected] 
2328ad83dc6aS[email protected]     // create connection state machine
232996a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2330ad83dc6aS[email protected] 
2331ad83dc6aS[email protected]     if (!connection){
2332ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2333ad83dc6aS[email protected]     }
2334ad83dc6aS[email protected] 
2335ad83dc6aS[email protected]     // delete linkn key
2336ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
2337ad83dc6aS[email protected] 
2338ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2339ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2340ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
23415127cc62S[email protected]     log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level);
2342ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2343ad83dc6aS[email protected] 
2344ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2345ad83dc6aS[email protected] 
2346ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2347ad83dc6aS[email protected]     // handle: authentication failure
2348ad83dc6aS[email protected]     // handle: disconnect on done
2349ad83dc6aS[email protected] 
2350ad83dc6aS[email protected]     hci_run();
2351ad83dc6aS[email protected] 
2352ad83dc6aS[email protected]     return 0;
2353ad83dc6aS[email protected] }
23548e618f72S[email protected] 
23558e618f72S[email protected] void gap_set_local_name(const char * local_name){
23568e618f72S[email protected]     hci_stack->local_name = local_name;
23578e618f72S[email protected] }
23588e618f72S[email protected] 
23597bdc6798S[email protected] le_command_status_t le_central_start_scan(){
236036af3e18S[email protected]     if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK;
23617bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
23627bdc6798S[email protected]     hci_run();
23637bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
23647bdc6798S[email protected] }
23658e618f72S[email protected] 
23667bdc6798S[email protected] le_command_status_t le_central_stop_scan(){
236736af3e18S[email protected]     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK;
23687bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
23697bdc6798S[email protected]     hci_run();
23707bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
23717bdc6798S[email protected] }
23724f3229d8S[email protected] 
2373ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
2374ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
2375ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
2376ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
2377ef11999fSmatthias.ringwald     hci_run();
2378ef11999fSmatthias.ringwald }
23794f3229d8S[email protected] 
23804f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){
23814f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
23824f3229d8S[email protected]     if (!conn){
23831f479f8cS[email protected]         log_info("le_central_connect: no connection exists yet, creating context");
23844f3229d8S[email protected]         conn = create_connection_for_bd_addr_and_type(*addr, addr_type);
23854f3229d8S[email protected]         if (!conn){
23864f3229d8S[email protected]             // notify client that alloc failed
23874f3229d8S[email protected]             hci_emit_le_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
23881f479f8cS[email protected]             log_info("le_central_connect: failed to alloc context");
23894f3229d8S[email protected]             return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller
23904f3229d8S[email protected]         }
23914f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
23921f479f8cS[email protected]         log_info("le_central_connect: send create connection next");
2393564fca32S[email protected]         hci_run();
23944f3229d8S[email protected]         return BLE_PERIPHERAL_OK;
23954f3229d8S[email protected]     }
23960bf6344aS[email protected] 
23970bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
23980bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
23990bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
24000bf6344aS[email protected]         hci_emit_le_connection_complete(conn, ERROR_CODE_COMMAND_DISALLOWED);
24011f479f8cS[email protected]         log_error("le_central_connect: classic connection or connect is already being created");
24020bf6344aS[email protected]         return BLE_PERIPHERAL_IN_WRONG_STATE;
24030bf6344aS[email protected]     }
24040bf6344aS[email protected] 
24051f479f8cS[email protected]     log_info("le_central_connect: context exists with state %u", conn->state);
24064f3229d8S[email protected]     hci_emit_le_connection_complete(conn, 0);
24074f3229d8S[email protected]     hci_run();
24084f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
24094f3229d8S[email protected] }
24104f3229d8S[email protected] 
24117851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
24127851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){
24130bf6344aS[email protected]     linked_item_t *it;
24140bf6344aS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
24150bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
24160bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
24170bf6344aS[email protected]         switch (conn->state){
2418a6725849S[email protected]             case SEND_CREATE_CONNECTION:
24197851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
24207851196eSmatthias.ringwald                 return conn;
24217851196eSmatthias.ringwald             default:
24227851196eSmatthias.ringwald                 break;
24237851196eSmatthias.ringwald         };
24247851196eSmatthias.ringwald     }
24257851196eSmatthias.ringwald     return NULL;
24267851196eSmatthias.ringwald }
24277851196eSmatthias.ringwald 
24287851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){
24297851196eSmatthias.ringwald     hci_connection_t * conn = le_central_get_outgoing_connection();
24307851196eSmatthias.ringwald     switch (conn->state){
24317851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
24327851196eSmatthias.ringwald             // skip sending create connection and emit event instead
24330bf6344aS[email protected]             hci_emit_le_connection_complete(conn, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
24347851196eSmatthias.ringwald             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
24357851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
24360bf6344aS[email protected]             break;
2437a6725849S[email protected]         case SENT_CREATE_CONNECTION:
24387851196eSmatthias.ringwald             // request to send cancel connection
24390bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
24400bf6344aS[email protected]             hci_run();
24410bf6344aS[email protected]             break;
24420bf6344aS[email protected]         default:
24430bf6344aS[email protected]             break;
24440bf6344aS[email protected]     }
2445e31f89a7S[email protected]     return BLE_PERIPHERAL_OK;
2446e31f89a7S[email protected] }
24474f3229d8S[email protected] 
24485917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){
24495917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
24505917a5c5S[email protected]     if (!conn){
24517851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
24525917a5c5S[email protected]         return BLE_PERIPHERAL_OK;
24535917a5c5S[email protected]     }
24545917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
24555917a5c5S[email protected]     hci_run();
24564f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
24574f3229d8S[email protected] }
245804a6ef8cSmatthias.ringwald 
245904a6ef8cSmatthias.ringwald void hci_disconnect_all(){
246004a6ef8cSmatthias.ringwald     linked_list_iterator_t it;
246104a6ef8cSmatthias.ringwald     linked_list_iterator_init(&it, &hci_stack->connections);
246204a6ef8cSmatthias.ringwald     while (linked_list_iterator_has_next(&it)){
246304a6ef8cSmatthias.ringwald         hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it);
246404a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
246504a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
246604a6ef8cSmatthias.ringwald     }
2467d31fba26S[email protected]     hci_run();
246804a6ef8cSmatthias.ringwald }
2469