xref: /btstack/src/hci.c (revision 4f4b43f315d9005411dab34929e7a9e6cad006cc)
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
693d7a3f67S[email protected] #include "../platforms/ios/src/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));
97bb69aaaeS[email protected]     hci_connection_t * conn = 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) {
2389da54300S[email protected]         log_error("hci_number_outgoing_packets: connection for handle %u does not exist!", 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){
2689da54300S[email protected]         log_error("hci_number_free_acl_slots: outgoing classic packets (%u) > total classic packets (%u)", 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){
2769da54300S[email protected]             log_error("hci_number_free_acl_slots: outgoing le packets (%u) > total le packets (%u)", 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){
2839da54300S[email protected]             log_error("hci_number_free_acl_slots: outgoing classic + le packets (%u + %u) > total packets (%u)", 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:
2909da54300S[email protected]             log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", 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:
297cb00d3aaS[email protected]            if (hci_stack->le_acl_packets_total_num){
298ee303eddS[email protected]                return free_slots_le;
299ee303eddS[email protected]            }
300cb00d3aaS[email protected]            return free_slots_classic;
301cb00d3aaS[email protected]     }
302998906cdSmatthias.ringwald }
303998906cdSmatthias.ringwald 
304ac928cc2S[email protected] 
305ac928cc2S[email protected] // @deprecated
306c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
307c24735b1Smatthias.ringwald     switch (packet_type) {
308c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
309ac928cc2S[email protected]             return hci_can_send_prepared_acl_packet_now(0);
310c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
311ac928cc2S[email protected]             return hci_can_send_command_packet_now();
312c24735b1Smatthias.ringwald         default:
313c24735b1Smatthias.ringwald             return 0;
314c24735b1Smatthias.ringwald     }
315c24735b1Smatthias.ringwald }
316c24735b1Smatthias.ringwald 
317ac928cc2S[email protected] // @deprecated
3186b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use
3196b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
320ac928cc2S[email protected] 
3216b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
322ac928cc2S[email protected] 
323ac928cc2S[email protected]     switch (packet_type) {
324ac928cc2S[email protected]         case HCI_ACL_DATA_PACKET:
325ac928cc2S[email protected]             return hci_can_send_acl_packet_now(0);
326ac928cc2S[email protected]         case HCI_COMMAND_DATA_PACKET:
327ac928cc2S[email protected]             return hci_can_send_command_packet_now();
328ac928cc2S[email protected]         default:
329ac928cc2S[email protected]             return 0;
330ac928cc2S[email protected]     }
331ac928cc2S[email protected] }
332ac928cc2S[email protected] 
333ac928cc2S[email protected] 
334ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer]
335ac928cc2S[email protected] int hci_can_send_command_packet_now(void){
336ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
337ac928cc2S[email protected] 
338ac928cc2S[email protected]     // check for async hci transport implementations
339ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
340ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){
341ac928cc2S[email protected]             return 0;
342ac928cc2S[email protected]         }
343ac928cc2S[email protected]     }
344ac928cc2S[email protected] 
345ac928cc2S[email protected]     return hci_stack->num_cmd_packets > 0;
346ac928cc2S[email protected] }
347ac928cc2S[email protected] 
348ac928cc2S[email protected] int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) {
349ac928cc2S[email protected]     // check for async hci transport implementations
350ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
351ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_ACL_DATA_PACKET)){
352ac928cc2S[email protected]             return 0;
353ac928cc2S[email protected]         }
354ac928cc2S[email protected]     }
355e79abdd6S[email protected]     return hci_number_free_acl_slots_for_handle(con_handle) > 0;
356ac928cc2S[email protected] }
357ac928cc2S[email protected] 
358ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){
359ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
360ac928cc2S[email protected]     return hci_can_send_prepared_acl_packet_now(con_handle);
3616b4af23dS[email protected] }
3626b4af23dS[email protected] 
363c8b9416aS[email protected] // used for internal checks in l2cap[-le].c
364c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
365c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
366c8b9416aS[email protected] }
367c8b9416aS[email protected] 
3686b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
3696b4af23dS[email protected] int hci_reserve_packet_buffer(void){
3709d14b626S[email protected]     if (hci_stack->hci_packet_buffer_reserved) {
3719d14b626S[email protected]         log_error("hci_reserve_packet_buffer called but buffer already reserved");
3729d14b626S[email protected]         return 0;
3739d14b626S[email protected]     }
3746b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
3756b4af23dS[email protected]     return 1;
3766b4af23dS[email protected] }
3776b4af23dS[email protected] 
37868a0fcf7S[email protected] void hci_release_packet_buffer(void){
37968a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
38068a0fcf7S[email protected] }
38168a0fcf7S[email protected] 
3826b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
3836b4af23dS[email protected] int hci_transport_synchronous(void){
3846b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
3856b4af23dS[email protected] }
3866b4af23dS[email protected] 
387826f7347S[email protected] // pre: caller has reserved the packet buffer
388826f7347S[email protected] int hci_send_acl_packet_buffer(int size){
3897856c818Smatthias.ringwald 
390826f7347S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
391826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
392826f7347S[email protected]         return 0;
393826f7347S[email protected]     }
394826f7347S[email protected] 
395d713a683S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
396d713a683S[email protected]     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
397d713a683S[email protected] 
398826f7347S[email protected]     // check for free places on Bluetooth module
399d713a683S[email protected]     if (!hci_can_send_prepared_acl_packet_now(con_handle)) {
400826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller");
40197b61c7bS[email protected]         hci_release_packet_buffer();
40297b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
40397b61c7bS[email protected]     }
4046218e6f1Smatthias.ringwald 
4055061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
40697b61c7bS[email protected]     if (!connection) {
4075fa0b7cfS[email protected]         log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle);
40897b61c7bS[email protected]         hci_release_packet_buffer();
40997b61c7bS[email protected]         return 0;
41097b61c7bS[email protected]     }
41156cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
41256cf178bSmatthias.ringwald 
41356cf178bSmatthias.ringwald     // count packet
41456cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
4159da54300S[email protected]     // log_info("hci_send_acl_packet - handle %u, sent %u", connection->con_handle, connection->num_acl_packets_sent);
4167856c818Smatthias.ringwald 
41700d8e42eSmatthias.ringwald     // send packet
4183a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
4196218e6f1Smatthias.ringwald 
4206b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
421826f7347S[email protected]     if (hci_transport_synchronous()){
42297b61c7bS[email protected]         hci_release_packet_buffer();
4236b4af23dS[email protected]     }
4246b4af23dS[email protected] 
42500d8e42eSmatthias.ringwald     return err;
426ee091cf1Smatthias.ringwald }
427ee091cf1Smatthias.ringwald 
42816833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
4297856c818Smatthias.ringwald 
430e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
431e76a89eeS[email protected] 
4327856c818Smatthias.ringwald     // get info
4337856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
4345061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
4357856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
4367856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
4377856c818Smatthias.ringwald 
4387856c818Smatthias.ringwald     // ignore non-registered handle
4397856c818Smatthias.ringwald     if (!conn){
4409da54300S[email protected]         log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle);
4417856c818Smatthias.ringwald         return;
4427856c818Smatthias.ringwald     }
4437856c818Smatthias.ringwald 
444e76a89eeS[email protected]     // assert packet is complete
4459ecc3e17S[email protected]     if (acl_length + 4 != size){
446e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
447e76a89eeS[email protected]         return;
448e76a89eeS[email protected]     }
449e76a89eeS[email protected] 
4507856c818Smatthias.ringwald     // update idle timestamp
4517856c818Smatthias.ringwald     hci_connection_timestamp(conn);
4527856c818Smatthias.ringwald 
4537856c818Smatthias.ringwald     // handle different packet types
4547856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
4557856c818Smatthias.ringwald 
4567856c818Smatthias.ringwald         case 0x01: // continuation fragment
4577856c818Smatthias.ringwald 
4587856c818Smatthias.ringwald             // sanity check
4597856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
4609da54300S[email protected]                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle);
4617856c818Smatthias.ringwald                 return;
4627856c818Smatthias.ringwald             }
4637856c818Smatthias.ringwald 
4647856c818Smatthias.ringwald             // append fragment payload (header already stored)
4657856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
4667856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
4677856c818Smatthias.ringwald 
4689da54300S[email protected]             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length,
469decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
4707856c818Smatthias.ringwald 
4717856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
4727856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
4737856c818Smatthias.ringwald 
4743a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
4757856c818Smatthias.ringwald                 // reset recombination buffer
4767856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
4777856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
4787856c818Smatthias.ringwald             }
4797856c818Smatthias.ringwald             break;
4807856c818Smatthias.ringwald 
4817856c818Smatthias.ringwald         case 0x02: { // first fragment
4827856c818Smatthias.ringwald 
4837856c818Smatthias.ringwald             // sanity check
4847856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
4859da54300S[email protected]                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x", con_handle);
4867856c818Smatthias.ringwald                 return;
4877856c818Smatthias.ringwald             }
4887856c818Smatthias.ringwald 
4897856c818Smatthias.ringwald             // peek into L2CAP packet!
4907856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
4917856c818Smatthias.ringwald 
4929da54300S[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length);
493decc01a8Smatthias.ringwald 
4947856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
4957856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
4967856c818Smatthias.ringwald 
4977856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
4983a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
4997856c818Smatthias.ringwald 
5007856c818Smatthias.ringwald             } else {
5017856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
5027856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
5037856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
5047856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
505decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
5067856c818Smatthias.ringwald             }
5077856c818Smatthias.ringwald             break;
5087856c818Smatthias.ringwald 
5097856c818Smatthias.ringwald         }
5107856c818Smatthias.ringwald         default:
5119da54300S[email protected]             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03);
5127856c818Smatthias.ringwald             return;
5137856c818Smatthias.ringwald     }
51494ab26f8Smatthias.ringwald 
51594ab26f8Smatthias.ringwald     // execute main loop
51694ab26f8Smatthias.ringwald     hci_run();
51716833f0aSmatthias.ringwald }
51822909952Smatthias.ringwald 
51967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
5209da54300S[email protected]     log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address));
5213c4d4b90Smatthias.ringwald 
522c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
523c785ef68Smatthias.ringwald 
5243a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
525a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
5263c4d4b90Smatthias.ringwald 
5273c4d4b90Smatthias.ringwald     // now it's gone
528c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
529c7e0c5f6Smatthias.ringwald }
530c7e0c5f6Smatthias.ringwald 
5310c042179S[email protected] static const uint16_t packet_type_sizes[] = {
5328f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
5338f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
5348f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
5358f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
5368f8108aaSmatthias.ringwald };
53765389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
53865389bfcS[email protected]      0, // 3 slot packets
53965389bfcS[email protected]      1, // 5 slot packets
54065389bfcS[email protected]     25, // EDR 2 mpbs
54165389bfcS[email protected]     26, // EDR 3 mbps
54265389bfcS[email protected]     39, // 3 slot EDR packts
54365389bfcS[email protected]     40, // 5 slot EDR packet
54465389bfcS[email protected] };
54565389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
54665389bfcS[email protected]     0x0f00, // 3 slot packets
54765389bfcS[email protected]     0xf000, // 5 slot packets
54865389bfcS[email protected]     0x1102, // EDR 2 mpbs
54965389bfcS[email protected]     0x2204, // EDR 3 mbps
55065389bfcS[email protected]     0x0300, // 3 slot EDR packts
55165389bfcS[email protected]     0x3000, // 5 slot EDR packet
55265389bfcS[email protected] };
5538f8108aaSmatthias.ringwald 
55465389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
55565389bfcS[email protected]     // enable packet types based on size
5568f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
5578f8108aaSmatthias.ringwald     int i;
5588f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
5598f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
5608f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
5618f8108aaSmatthias.ringwald             packet_types |= 1 << i;
5628f8108aaSmatthias.ringwald         }
5638f8108aaSmatthias.ringwald     }
56465389bfcS[email protected]     // disable packet types due to missing local supported features
56565389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
56665389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
56765389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
56865389bfcS[email protected]         if (feature_set) continue;
56965389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
57065389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
57165389bfcS[email protected]     }
5728f8108aaSmatthias.ringwald     // flip bits for "may not be used"
5738f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
5748f8108aaSmatthias.ringwald     return packet_types;
5758f8108aaSmatthias.ringwald }
5768f8108aaSmatthias.ringwald 
5778f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
5783a9fb326S[email protected]     return hci_stack->packet_types;
5798f8108aaSmatthias.ringwald }
5808f8108aaSmatthias.ringwald 
581facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
5827dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
5833a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
5847dc17943Smatthias.ringwald }
5857dc17943Smatthias.ringwald 
586f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
5873a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
5887dc17943Smatthias.ringwald }
5897dc17943Smatthias.ringwald 
5906ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
5916ac9a97eS[email protected]     // No. 54, byte 6, bit 6
5926ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
5936ac9a97eS[email protected] }
5946ac9a97eS[email protected] 
595f5d8d141S[email protected] int hci_ssp_supported(void){
5966ac9a97eS[email protected]     // No. 51, byte 6, bit 3
5973a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
598f5d8d141S[email protected] }
599f5d8d141S[email protected] 
600f5d8d141S[email protected] int hci_classic_supported(void){
6016ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
6023a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
603f5d8d141S[email protected] }
604f5d8d141S[email protected] 
605f5d8d141S[email protected] int hci_le_supported(void){
606f5d8d141S[email protected] #ifdef HAVE_BLE
6076ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
6083a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
609f5d8d141S[email protected] #else
610f5d8d141S[email protected]     return 0;
611f5d8d141S[email protected] #endif
612f5d8d141S[email protected] }
613f5d8d141S[email protected] 
61469a97523S[email protected] // get addr type and address used in advertisement packets
61518904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
6163a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
6173a9fb326S[email protected]     if (hci_stack->adv_addr_type){
6183a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
61969a97523S[email protected]     } else {
6203a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
62169a97523S[email protected]     }
62269a97523S[email protected] }
62369a97523S[email protected] 
624b2f949feS[email protected] #ifdef HAVE_BLE
625e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){
626d1dc057bS[email protected]     int offset = 3;
627d1dc057bS[email protected]     int num_reports = packet[offset];
628d1dc057bS[email protected]     offset += 1;
629d1dc057bS[email protected] 
63057c9da5bS[email protected]     int i;
63157c9da5bS[email protected]     int total_data_length = 0;
63257c9da5bS[email protected] 
63357c9da5bS[email protected]     for (i=0; i<num_reports;i++){
63457c9da5bS[email protected]         total_data_length += packet[4+num_reports*8+i];
63557c9da5bS[email protected]     }
63657c9da5bS[email protected] 
637*4f4b43f3S[email protected]     log_info("HCI: handle adv report with num reports: %d", num_reports);
63857c9da5bS[email protected]     for (i=0; i<num_reports;i++){
639d1dc057bS[email protected]         uint8_t data_length = packet[offset + 9];
6402b552b23S[email protected]         uint8_t event_size = 10 + data_length;
6412b552b23S[email protected]         uint8_t event[2 + event_size ];
642d1dc057bS[email protected]         int pos = 0;
6432b552b23S[email protected]         event[pos++] = GAP_LE_ADVERTISING_REPORT;
64457c9da5bS[email protected]         event[pos++] = event_size;
645d1dc057bS[email protected]         memcpy(&event[pos], &packet[offset], 1+1+6); // bt address
646d1dc057bS[email protected]         offset += 8;
647d1dc057bS[email protected]         pos += 8;
648d1dc057bS[email protected]         event[pos++] = packet[offset + 1 + data_length]; // rssi
649d1dc057bS[email protected]         event[pos++] = packet[offset++]; //data_length;
650d1dc057bS[email protected]         memcpy(&event[pos], &packet[offset], data_length);
65157c9da5bS[email protected]         pos += data_length;
652d1dc057bS[email protected]         offset += data_length + 1; // rssi
653*4f4b43f3S[email protected]         hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
65457c9da5bS[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
65557c9da5bS[email protected]     }
65657c9da5bS[email protected] }
657b2f949feS[email protected] #endif
65857c9da5bS[email protected] 
6596155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
660a2481739S[email protected]     uint8_t command_completed = 0;
661a2481739S[email protected]     if ((hci_stack->substate % 2) == 0) return;
6626155b3d3S[email protected]     // odd: waiting for event
6636155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
6646155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,3);
6656155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
666a2481739S[email protected]             command_completed = 1;
6676155b3d3S[email protected]             log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate);
6686155b3d3S[email protected]         } else {
6696155b3d3S[email protected]             log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
6706155b3d3S[email protected]         }
6716155b3d3S[email protected]     }
6726155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_STATUS){
6736155b3d3S[email protected]         uint8_t  status = packet[2];
6746155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,4);
6756155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
6766155b3d3S[email protected]             if (status){
677a2481739S[email protected]                 command_completed = 1;
6786155b3d3S[email protected]                 log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate);
6796155b3d3S[email protected]             } else {
6806155b3d3S[email protected]                 log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
6816155b3d3S[email protected]             }
6826155b3d3S[email protected]         } else {
6836155b3d3S[email protected]             log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
6846155b3d3S[email protected]         }
6856155b3d3S[email protected]     }
686a2481739S[email protected] 
687a2481739S[email protected]     if (!command_completed) return;
688a2481739S[email protected] 
689a2481739S[email protected]     switch(hci_stack->substate >> 1){
690a2481739S[email protected]         default:
691a2481739S[email protected]             hci_stack->substate++;
692a2481739S[email protected]             break;
6936155b3d3S[email protected]     }
6946155b3d3S[email protected] }
6956155b3d3S[email protected] 
6966155b3d3S[email protected] static void hci_initializing_state_machine(){
6979da54300S[email protected]         // log_info("hci_init: substate %u", hci_stack->substate);
6986155b3d3S[email protected]     if (hci_stack->substate % 2) {
6996155b3d3S[email protected]         // odd: waiting for command completion
7006155b3d3S[email protected]         return;
7016155b3d3S[email protected]     }
7026155b3d3S[email protected]     switch (hci_stack->substate >> 1){
7036155b3d3S[email protected]         case 0: // RESET
7046155b3d3S[email protected]             hci_state_reset();
7056155b3d3S[email protected] 
7066155b3d3S[email protected]             hci_send_cmd(&hci_reset);
7076155b3d3S[email protected]             if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
7086155b3d3S[email protected]                 // skip baud change
7096155b3d3S[email protected]                 hci_stack->substate = 4; // >> 1 = 2
7106155b3d3S[email protected]             }
7116155b3d3S[email protected]             break;
7126155b3d3S[email protected]         case 1: // SEND BAUD CHANGE
7136155b3d3S[email protected]             hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
71488789c61Smatthias.ringwald             hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
7156155b3d3S[email protected]             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
7166155b3d3S[email protected]             break;
7176155b3d3S[email protected]         case 2: // LOCAL BAUD CHANGE
7186155b3d3S[email protected]             log_info("Local baud rate change");
7196155b3d3S[email protected]             hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
7206155b3d3S[email protected]             hci_stack->substate += 2;
7216155b3d3S[email protected]             // break missing here for fall through
7226155b3d3S[email protected] 
7236155b3d3S[email protected]         case 3:
7246155b3d3S[email protected]             log_info("Custom init");
7256155b3d3S[email protected]             // Custom initialization
7266155b3d3S[email protected]             if (hci_stack->control && hci_stack->control->next_cmd){
7276155b3d3S[email protected]                 int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
7286155b3d3S[email protected]                 if (valid_cmd){
7296155b3d3S[email protected]                     int size = 3 + hci_stack->hci_packet_buffer[2];
7306155b3d3S[email protected]                     hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
7316155b3d3S[email protected]                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
7326155b3d3S[email protected]                     hci_stack->substate = 4; // more init commands
7336155b3d3S[email protected]                     break;
7346155b3d3S[email protected]                 }
7359da54300S[email protected]                 log_info("hci_run: init script done");
7366155b3d3S[email protected]             }
7376155b3d3S[email protected]             // otherwise continue
7386155b3d3S[email protected]             hci_send_cmd(&hci_read_bd_addr);
7396155b3d3S[email protected]             break;
7406155b3d3S[email protected]         case 4:
7416155b3d3S[email protected]             hci_send_cmd(&hci_read_buffer_size);
7426155b3d3S[email protected]             break;
7436155b3d3S[email protected]         case 5:
7446155b3d3S[email protected]             hci_send_cmd(&hci_read_local_supported_features);
7456155b3d3S[email protected]             break;
7466155b3d3S[email protected]         case 6:
7476155b3d3S[email protected]             if (hci_le_supported()){
7486155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
7496155b3d3S[email protected]             } else {
7506155b3d3S[email protected]                 // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
7516155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
7526155b3d3S[email protected]             }
7536155b3d3S[email protected] 
7546155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
7556155b3d3S[email protected]             if (!hci_classic_supported()){
7566155b3d3S[email protected]                 if (hci_le_supported()){
7576155b3d3S[email protected]                     hci_stack->substate = 11 << 1;    // skip all classic command
7586155b3d3S[email protected]                 } else {
7596155b3d3S[email protected]                     log_error("Neither BR/EDR nor LE supported");
7606155b3d3S[email protected]                     hci_stack->substate = 14 << 1;    // skip all
7616155b3d3S[email protected]                 }
7626155b3d3S[email protected]             }
7636155b3d3S[email protected]             break;
7646155b3d3S[email protected]         case 7:
7656155b3d3S[email protected]             if (hci_ssp_supported()){
7666155b3d3S[email protected]                 hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
7676155b3d3S[email protected]                 break;
7686155b3d3S[email protected]             }
7696155b3d3S[email protected]             hci_stack->substate += 2;
7706155b3d3S[email protected]             // break missing here for fall through
7716155b3d3S[email protected] 
7726155b3d3S[email protected]         case 8:
7736155b3d3S[email protected]             // ca. 15 sec
7746155b3d3S[email protected]             hci_send_cmd(&hci_write_page_timeout, 0x6000);
7756155b3d3S[email protected]             break;
7766155b3d3S[email protected]         case 9:
7776155b3d3S[email protected]             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
7786155b3d3S[email protected]             break;
7796155b3d3S[email protected]         case 10:
7806155b3d3S[email protected]             if (hci_stack->local_name){
7816155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
7826155b3d3S[email protected]             } else {
7836155b3d3S[email protected]                 char hostname[30];
7846155b3d3S[email protected] #ifdef EMBEDDED
7856155b3d3S[email protected]                 // BTstack-11:22:33:44:55:66
7866155b3d3S[email protected]                 strcpy(hostname, "BTstack ");
7876155b3d3S[email protected]                 strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
7886155b3d3S[email protected]                 log_info("---> Name %s", hostname);
7896155b3d3S[email protected] #else
7906155b3d3S[email protected]                 // hostname for POSIX systems
7916155b3d3S[email protected]                 gethostname(hostname, 30);
7926155b3d3S[email protected]                 hostname[29] = '\0';
7936155b3d3S[email protected] #endif
7946155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hostname);
7956155b3d3S[email protected]             }
7966155b3d3S[email protected]             break;
7976155b3d3S[email protected]         case 11:
7986155b3d3S[email protected]             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
7996155b3d3S[email protected]             if (!hci_le_supported()){
8006155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
8016155b3d3S[email protected]                 hci_stack->substate = 14 << 1;
8026155b3d3S[email protected]             }
8036155b3d3S[email protected]             break;
8046155b3d3S[email protected] 
8056155b3d3S[email protected] #ifdef HAVE_BLE
8066155b3d3S[email protected]         // LE INIT
8076155b3d3S[email protected]         case 12:
8086155b3d3S[email protected]             hci_send_cmd(&hci_le_read_buffer_size);
8096155b3d3S[email protected]             break;
8106155b3d3S[email protected]         case 13:
8116155b3d3S[email protected]             // LE Supported Host = 1, Simultaneous Host = 0
8126155b3d3S[email protected]             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
8136155b3d3S[email protected]             break;
8146155b3d3S[email protected]         case 14:
8156155b3d3S[email protected]             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
8166155b3d3S[email protected]             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
8176155b3d3S[email protected]             break;
8186155b3d3S[email protected] #endif
8196155b3d3S[email protected] 
8206155b3d3S[email protected]         // DONE
8216155b3d3S[email protected]         case 15:
8226155b3d3S[email protected]             // done.
8236155b3d3S[email protected]             hci_stack->state = HCI_STATE_WORKING;
8246155b3d3S[email protected]             hci_emit_state();
8256155b3d3S[email protected]             break;
8266155b3d3S[email protected]         default:
8276155b3d3S[email protected]             break;
8286155b3d3S[email protected]     }
8296155b3d3S[email protected]     hci_stack->substate++;
8306155b3d3S[email protected] }
8316155b3d3S[email protected] 
83274ec757aSmatthias.ringwald // avoid huge local variables
833223aafc1Smatthias.ringwald #ifndef EMBEDDED
83474ec757aSmatthias.ringwald static device_name_t device_name;
835223aafc1Smatthias.ringwald #endif
83616833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
837e76a89eeS[email protected] 
838e76a89eeS[email protected]     uint16_t event_length = packet[1];
839e76a89eeS[email protected] 
840e76a89eeS[email protected]     // assert packet is complete
841e76a89eeS[email protected]     if (size != event_length + 2){
842e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
843e76a89eeS[email protected]         return;
844e76a89eeS[email protected]     }
845e76a89eeS[email protected] 
8461281a47eSmatthias.ringwald     bd_addr_t addr;
84796a45072S[email protected]     bd_addr_type_t addr_type;
8482d00edd4Smatthias.ringwald     uint8_t link_type;
849fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
8501f7b95a1Smatthias.ringwald     hci_connection_t * conn;
85156cf178bSmatthias.ringwald     int i;
85222909952Smatthias.ringwald 
8539da54300S[email protected]     // log_info("HCI:EVENT:%02x", packet[0]);
8545909f7f2Smatthias.ringwald 
8556772a24cSmatthias.ringwald     switch (packet[0]) {
85622909952Smatthias.ringwald 
8576772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
8587ec5eeaaSmatthias.ringwald             // get num cmd packets
8599da54300S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u", hci_stack->num_cmd_packets, packet[2]);
8603a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
8617ec5eeaaSmatthias.ringwald 
862e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
863e2edc0c3Smatthias.ringwald                 // from offset 5
864e2edc0c3Smatthias.ringwald                 // status
8651d279b20Smatthias.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"
8663a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
86756cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
868ee303eddS[email protected]                 hci_stack->acl_packets_total_num  = packet[9];
86956cf178bSmatthias.ringwald                 // ignore: total num SCO packets
8703a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
871a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
8723a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
8733a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
8748f8108aaSmatthias.ringwald                     }
8759da54300S[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u",
876ee303eddS[email protected]                              hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num);
877e2edc0c3Smatthias.ringwald                 }
87856cf178bSmatthias.ringwald             }
87965a46ef3S[email protected] #ifdef HAVE_BLE
88065a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
881219eea5fS[email protected]                 hci_stack->le_data_packets_length = READ_BT_16(packet, 6);
882ee303eddS[email protected]                 hci_stack->le_acl_packets_total_num  = packet[8];
8839da54300S[email protected]                 log_info("hci_le_read_buffer_size: size %u, count %u", hci_stack->le_data_packets_length, hci_stack->le_acl_packets_total_num);
88465a46ef3S[email protected]             }
88565a46ef3S[email protected] #endif
886188981d2Smatthias.ringwald             // Dump local address
887188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
8883a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
8899da54300S[email protected]                 log_info("Local Address, Status: 0x%02x: Addr: %s",
8903a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
891188981d2Smatthias.ringwald             }
892381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
8933a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
894381fbed8Smatthias.ringwald             }
89565389bfcS[email protected]             // Note: HCI init checks
896559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
8973a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
898559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
8993a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
9003a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
9013a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
9023a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
90365389bfcS[email protected] 
90465389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
9053a9fb326S[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]);
9063a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
907f5d8d141S[email protected] 
908f5d8d141S[email protected]                 // Classic/LE
909f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
910559e517eS[email protected]             }
91156cf178bSmatthias.ringwald             break;
91256cf178bSmatthias.ringwald 
9137ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
9147ec5eeaaSmatthias.ringwald             // get num cmd packets
9159da54300S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u", hci_stack->num_cmd_packets, packet[3]);
9163a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
9177ec5eeaaSmatthias.ringwald             break;
9187ec5eeaaSmatthias.ringwald 
9192e440c8aS[email protected]         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{
9202e440c8aS[email protected]             int offset = 3;
92156cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
9222e440c8aS[email protected]                 handle = READ_BT_16(packet, offset);
9232e440c8aS[email protected]                 offset += 2;
9242e440c8aS[email protected]                 uint16_t num_packets = READ_BT_16(packet, offset);
9252e440c8aS[email protected]                 offset += 2;
9262e440c8aS[email protected] 
9275061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
92856cf178bSmatthias.ringwald                 if (!conn){
9299da54300S[email protected]                     log_error("hci_number_completed_packet lists unused con handle %u", handle);
93056cf178bSmatthias.ringwald                     continue;
93156cf178bSmatthias.ringwald                 }
93223bed257S[email protected] 
93323bed257S[email protected]                 if (conn->num_acl_packets_sent >= num_packets){
93456cf178bSmatthias.ringwald                     conn->num_acl_packets_sent -= num_packets;
93523bed257S[email protected]                 } else {
93623bed257S[email protected]                     log_error("hci_number_completed_packets, more slots freed then sent.");
93723bed257S[email protected]                     conn->num_acl_packets_sent = 0;
93823bed257S[email protected]                 }
9399da54300S[email protected]                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent);
94056cf178bSmatthias.ringwald             }
9416772a24cSmatthias.ringwald             break;
9422e440c8aS[email protected]         }
9431f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
94437eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
94537eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
9462d00edd4Smatthias.ringwald             link_type = packet[11];
9479da54300S[email protected]             log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type);
94837eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
94996a45072S[email protected]                 conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
9501f7b95a1Smatthias.ringwald                 if (!conn) {
95196a45072S[email protected]                     conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
9521f7b95a1Smatthias.ringwald                 }
953ce4c8fabSmatthias.ringwald                 if (!conn) {
954ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
9553a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
9563a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
957ce4c8fabSmatthias.ringwald                     break;
958ce4c8fabSmatthias.ringwald                 }
95932ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
96032ab9390Smatthias.ringwald                 hci_run();
96137eaa4cfSmatthias.ringwald             } else {
962ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
9633a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
9643a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
96537eaa4cfSmatthias.ringwald             }
9661f7b95a1Smatthias.ringwald             break;
9671f7b95a1Smatthias.ringwald 
9686772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
969fe1ed1b8Smatthias.ringwald             // Connection management
970fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
9719da54300S[email protected]             log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
97296a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
97396a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
974fe1ed1b8Smatthias.ringwald             if (conn) {
975b448a0e7Smatthias.ringwald                 if (!packet[2]){
976c8e4258aSmatthias.ringwald                     conn->state = OPEN;
977fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
978ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
979ee091cf1Smatthias.ringwald 
980c785ef68Smatthias.ringwald                     // restart timer
981c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
982ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
983c785ef68Smatthias.ringwald 
9849da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
98543bfb1bdSmatthias.ringwald 
98643bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
987b448a0e7Smatthias.ringwald                 } else {
9881bd5283dS[email protected]                     int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED;
9891bd5283dS[email protected]                     uint8_t status = packet[2];
9901bd5283dS[email protected]                     bd_addr_t bd_address;
9911bd5283dS[email protected]                     memcpy(&bd_address, conn->address, 6);
992ad83dc6aS[email protected] 
993b448a0e7Smatthias.ringwald                     // connection failed, remove entry
9943a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
995a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
996c12e46e7Smatthias.ringwald 
9971bd5283dS[email protected]                     // notify client if dedicated bonding
9981bd5283dS[email protected]                     if (notify_dedicated_bonding_failed){
9991bd5283dS[email protected]                         log_info("hci notify_dedicated_bonding_failed");
10001bd5283dS[email protected]                         hci_emit_dedicated_bonding_result(bd_address, status);
10011bd5283dS[email protected]                     }
10021bd5283dS[email protected] 
1003c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
1004c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
1005c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
1006c12e46e7Smatthias.ringwald                     }
1007fe1ed1b8Smatthias.ringwald                 }
1008fe1ed1b8Smatthias.ringwald             }
10096772a24cSmatthias.ringwald             break;
1010fe1ed1b8Smatthias.ringwald 
1011afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1012afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
1013afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
1014afd4e962S[email protected]             if (!conn) break;
1015afd4e962S[email protected]             if (!packet[2]){
1016afd4e962S[email protected]                 uint8_t * features = &packet[5];
1017afd4e962S[email protected]                 if (features[6] & (1 << 3)){
1018afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
1019afd4e962S[email protected]                 }
1020afd4e962S[email protected]             }
1021afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
1022ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
1023ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1024ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1025ad83dc6aS[email protected]             }
1026afd4e962S[email protected]             break;
1027afd4e962S[email protected] 
10287fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
10299da54300S[email protected]             log_info("HCI_EVENT_LINK_KEY_REQUEST");
10307fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
103174d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
10323a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
103332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
103464472d52Smatthias.ringwald             hci_run();
1035d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
103629d53098Smatthias.ringwald             return;
10377fde4af9Smatthias.ringwald 
10389ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
103929d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
104096a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
10419ab95c90S[email protected]             if (!conn) break;
10429ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
10437bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
10449ab95c90S[email protected]             // Change Connection Encryption keeps link key type
10459ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
10469ab95c90S[email protected]                 conn->link_key_type = link_key_type;
10479ab95c90S[email protected]             }
10483a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
10493a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
105029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
10517fde4af9Smatthias.ringwald             break;
10529ab95c90S[email protected]         }
10537fde4af9Smatthias.ringwald 
10547fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
10556724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
10564c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
10573a9fb326S[email protected]             if (!hci_stack->bondable){
1058899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
1059f8fb5f6eS[email protected]                 hci_run();
1060f8fb5f6eS[email protected]                 return;
10614c57c146S[email protected]             }
1062d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
10633a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
1064d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
10653a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
10667fde4af9Smatthias.ringwald             break;
10677fde4af9Smatthias.ringwald 
10681d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
10691d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
1070dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
1071dbe1a790S[email protected]             break;
1072dbe1a790S[email protected] 
1073dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
10746724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
10753a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1076dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
1077dbe1a790S[email protected]             break;
1078dbe1a790S[email protected] 
1079dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
10806724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
10813a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1082dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
10831d6b20aeS[email protected]             break;
10841d6b20aeS[email protected] 
1085f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
1086f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
1087f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
1088f0944df2S[email protected]             if (!conn) break;
1089ad83dc6aS[email protected]             if (packet[2] == 0) {
1090f0944df2S[email protected]                 if (packet[5]){
1091f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
1092f0944df2S[email protected]                 } else {
1093536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
1094f0944df2S[email protected]                 }
1095ad83dc6aS[email protected]             }
1096a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
1097f0944df2S[email protected]             break;
1098f0944df2S[email protected] 
10991eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
11001eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
11011eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
11021eb2563eS[email protected]             if (!conn) break;
1103ad83dc6aS[email protected] 
1104ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
1105ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1106ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
1107ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
11081bd5283dS[email protected]                 conn->bonding_status = packet[2];
1109ad83dc6aS[email protected]                 break;
1110ad83dc6aS[email protected]             }
1111ad83dc6aS[email protected] 
1112b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
11131eb2563eS[email protected]                 // link key sufficient for requested security
11141eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1115ad83dc6aS[email protected]                 break;
1116ad83dc6aS[email protected]             }
11171eb2563eS[email protected]             // not enough
11181eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
11191eb2563eS[email protected]             break;
112034d2123cS[email protected] 
1121223aafc1Smatthias.ringwald #ifndef EMBEDDED
112274ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
11233a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
112474ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
112574ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
11265a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
11275a06394aSmatthias.ringwald             for (i=0; i<248;i++){
11285a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
11295a06394aSmatthias.ringwald                     packet[9+i] = 0;
11305a06394aSmatthias.ringwald                     break;
1131cdc9101dSmatthias.ringwald                 }
11325a06394aSmatthias.ringwald             }
1133d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
113474ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
11353a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
113674ec757aSmatthias.ringwald             break;
113774ec757aSmatthias.ringwald 
113874ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
1139206a9031S[email protected]         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
11403a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
114174ec757aSmatthias.ringwald             // first send inq result packet
11423a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
114374ec757aSmatthias.ringwald             // then send cached remote names
1144206a9031S[email protected]             int offset = 3;
114574ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
1146206a9031S[email protected]                 bt_flip_addr(addr, &packet[offset]);
1147206a9031S[email protected]                 offset += 14; // 6 + 1 + 1 + 1 + 3 + 2;
11483a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
114974ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
115074ec757aSmatthias.ringwald                 }
115174ec757aSmatthias.ringwald             }
115274ec757aSmatthias.ringwald             return;
1153206a9031S[email protected]         }
1154223aafc1Smatthias.ringwald #endif
115574ec757aSmatthias.ringwald 
115686805605S[email protected]         // HCI_EVENT_DISCONNECTION_COMPLETE
115786805605S[email protected]         // has been moved down, to first notify stack before shutting connection down
11586772a24cSmatthias.ringwald 
1159c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
11603a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
11613a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
11627586ee35S[email protected]             } else {
11637586ee35S[email protected]                 // if no special requests, just reboot stack
11647586ee35S[email protected]                 hci_power_control_off();
11657586ee35S[email protected]                 hci_power_control_on();
1166c68bdf90Smatthias.ringwald             }
1167c68bdf90Smatthias.ringwald             break;
1168c68bdf90Smatthias.ringwald 
11696b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
11706b4af23dS[email protected]             // free packet buffer for asynchronous transport
11716b4af23dS[email protected]             if (hci_transport_synchronous()) break;
11726b4af23dS[email protected]             hci_stack->hci_packet_buffer_reserved = 0;
11736b4af23dS[email protected]             break;
11746b4af23dS[email protected] 
11755909f7f2Smatthias.ringwald #ifdef HAVE_BLE
11765909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
11775909f7f2Smatthias.ringwald             switch (packet[2]){
117857c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
11799da54300S[email protected]                     log_info("advertising report received");
11807bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
118157c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
11827bdc6798S[email protected]                     break;
11835909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
11845909f7f2Smatthias.ringwald                     // Connection management
11855909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
11867bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
11879da54300S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr));
11885909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
118996a45072S[email protected]                     conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
11905909f7f2Smatthias.ringwald                     if (packet[3]){
11915909f7f2Smatthias.ringwald                         if (conn){
11925909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
11933a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
11945909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
11955909f7f2Smatthias.ringwald                         }
11965909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
11975909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
11985909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
11995909f7f2Smatthias.ringwald                         }
12005909f7f2Smatthias.ringwald                         break;
12015909f7f2Smatthias.ringwald                     }
12025909f7f2Smatthias.ringwald                     if (!conn){
120396a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
12045909f7f2Smatthias.ringwald                     }
12055909f7f2Smatthias.ringwald                     if (!conn){
12065909f7f2Smatthias.ringwald                         // no memory
12075909f7f2Smatthias.ringwald                         break;
12085909f7f2Smatthias.ringwald                     }
12095909f7f2Smatthias.ringwald 
12105909f7f2Smatthias.ringwald                     conn->state = OPEN;
12115909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
12125909f7f2Smatthias.ringwald 
12135909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
12145909f7f2Smatthias.ringwald 
12155909f7f2Smatthias.ringwald                     // restart timer
12165909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
12175909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
12185909f7f2Smatthias.ringwald 
12199da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
12205909f7f2Smatthias.ringwald 
12215909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
12225909f7f2Smatthias.ringwald                     break;
12235909f7f2Smatthias.ringwald 
12249da54300S[email protected]             // log_info("LE buffer size: %u, count %u", READ_BT_16(packet,6), packet[8]);
122565a46ef3S[email protected] 
12265909f7f2Smatthias.ringwald                 default:
12275909f7f2Smatthias.ringwald                     break;
12285909f7f2Smatthias.ringwald             }
12295909f7f2Smatthias.ringwald             break;
12305909f7f2Smatthias.ringwald #endif
12316772a24cSmatthias.ringwald         default:
12326772a24cSmatthias.ringwald             break;
1233fe1ed1b8Smatthias.ringwald     }
1234fe1ed1b8Smatthias.ringwald 
12353429f56bSmatthias.ringwald     // handle BT initialization
12363a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
12376155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
1238c9af4d3fS[email protected]     }
123922909952Smatthias.ringwald 
124089db417bSmatthias.ringwald     // help with BT sleep
12413a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
12423a9fb326S[email protected]         && hci_stack->substate == 1
124389db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
12443a9fb326S[email protected]         hci_stack->substate++;
124589db417bSmatthias.ringwald     }
124689db417bSmatthias.ringwald 
124786805605S[email protected]     // notify upper stack
12483a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
124994ab26f8Smatthias.ringwald 
125086805605S[email protected]     // moved here to give upper stack a chance to close down everything with hci_connection_t intact
125186805605S[email protected]     if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE){
125286805605S[email protected]         if (!packet[2]){
125386805605S[email protected]             handle = READ_BT_16(packet, 3);
125486805605S[email protected]             hci_connection_t * conn = hci_connection_for_handle(handle);
125586805605S[email protected]             if (conn) {
125686805605S[email protected]                 uint8_t status = conn->bonding_status;
125786805605S[email protected]                 bd_addr_t bd_address;
125886805605S[email protected]                 memcpy(&bd_address, conn->address, 6);
125986805605S[email protected]                 hci_shutdown_connection(conn);
126086805605S[email protected]                 if (conn->bonding_flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){
126186805605S[email protected]                     hci_emit_dedicated_bonding_result(bd_address, status);
126286805605S[email protected]                 }
126386805605S[email protected]             }
126486805605S[email protected]         }
126586805605S[email protected]     }
126686805605S[email protected] 
126794ab26f8Smatthias.ringwald 	// execute main loop
126894ab26f8Smatthias.ringwald 	hci_run();
126916833f0aSmatthias.ringwald }
127016833f0aSmatthias.ringwald 
12710a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
127210e830c9Smatthias.ringwald     switch (packet_type) {
127310e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
127410e830c9Smatthias.ringwald             event_handler(packet, size);
127510e830c9Smatthias.ringwald             break;
127610e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
127710e830c9Smatthias.ringwald             acl_handler(packet, size);
127810e830c9Smatthias.ringwald             break;
127910e830c9Smatthias.ringwald         default:
128010e830c9Smatthias.ringwald             break;
128110e830c9Smatthias.ringwald     }
128210e830c9Smatthias.ringwald }
128310e830c9Smatthias.ringwald 
1284fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
12852718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
12863a9fb326S[email protected]     hci_stack->packet_handler = handler;
128716833f0aSmatthias.ringwald }
128816833f0aSmatthias.ringwald 
12896155b3d3S[email protected] static void hci_state_reset(){
1290595bdbfbS[email protected]     // no connections yet
1291595bdbfbS[email protected]     hci_stack->connections = NULL;
129274308b23Smatthias.ringwald 
129374308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
129474308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
129574308b23Smatthias.ringwald     // hci_stack->connectable = 0;
129674308b23Smatthias.ringwald     // hci_stack->bondable = 1;
1297595bdbfbS[email protected] 
129844935e40S[email protected]     // buffer is free
129944935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
130044935e40S[email protected] 
1301595bdbfbS[email protected]     // no pending cmds
1302595bdbfbS[email protected]     hci_stack->decline_reason = 0;
1303595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
1304595bdbfbS[email protected] 
1305595bdbfbS[email protected]     // LE
1306595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
1307595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
1308595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
1309e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
1310595bdbfbS[email protected] }
1311595bdbfbS[email protected] 
13124f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
1313475c8125Smatthias.ringwald 
13143a9fb326S[email protected] #ifdef HAVE_MALLOC
13153a9fb326S[email protected]     if (!hci_stack) {
13163a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
13173a9fb326S[email protected]     }
13183a9fb326S[email protected] #else
13193a9fb326S[email protected]     hci_stack = &hci_stack_static;
13203a9fb326S[email protected] #endif
132166fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
13223a9fb326S[email protected] 
1323475c8125Smatthias.ringwald     // reference to use transport layer implementation
13243a9fb326S[email protected]     hci_stack->hci_transport = transport;
1325475c8125Smatthias.ringwald 
132611e23e5fSmatthias.ringwald     // references to used control implementation
13273a9fb326S[email protected]     hci_stack->control = control;
132811e23e5fSmatthias.ringwald 
132911e23e5fSmatthias.ringwald     // reference to used config
13303a9fb326S[email protected]     hci_stack->config = config;
133111e23e5fSmatthias.ringwald 
133216833f0aSmatthias.ringwald     // higher level handler
13333a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
133416833f0aSmatthias.ringwald 
1335404843c1Smatthias.ringwald     // store and open remote device db
13363a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
13373a9fb326S[email protected]     if (hci_stack->remote_device_db) {
13383a9fb326S[email protected]         hci_stack->remote_device_db->open();
1339404843c1Smatthias.ringwald     }
134029d53098Smatthias.ringwald 
13418fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
13423a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
13438fcba05dSmatthias.ringwald 
134416833f0aSmatthias.ringwald     // register packet handlers with transport
134510e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
1346f5454fc6Smatthias.ringwald 
13473a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
1348e2386ba1S[email protected] 
1349e2386ba1S[email protected]     // class of device
13503a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
1351a45d6b9fS[email protected] 
1352f20168b8Smatthias.ringwald     // bondable by default
1353f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
1354f20168b8Smatthias.ringwald 
135563048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
13563a9fb326S[email protected]     hci_stack->ssp_enable = 1;
13573a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
13583a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
13593a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
136069a97523S[email protected] 
1361595bdbfbS[email protected]     hci_state_reset();
1362475c8125Smatthias.ringwald }
1363475c8125Smatthias.ringwald 
1364404843c1Smatthias.ringwald void hci_close(){
1365404843c1Smatthias.ringwald     // close remote device db
13663a9fb326S[email protected]     if (hci_stack->remote_device_db) {
13673a9fb326S[email protected]         hci_stack->remote_device_db->close();
1368404843c1Smatthias.ringwald     }
13693a9fb326S[email protected]     while (hci_stack->connections) {
1370bc68afe2S[email protected]         // cancel all l2cap connections
1371bc68afe2S[email protected]         hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host
13723a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1373f5454fc6Smatthias.ringwald     }
1374f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
13753a9fb326S[email protected] 
13763a9fb326S[email protected] #ifdef HAVE_MALLOC
13773a9fb326S[email protected]     free(hci_stack);
13783a9fb326S[email protected] #endif
13793a9fb326S[email protected]     hci_stack = NULL;
1380404843c1Smatthias.ringwald }
1381404843c1Smatthias.ringwald 
13829e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
13839e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
13849e61646fS[email protected] }
13859e61646fS[email protected] 
138666fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
138766fb9560S[email protected]     disable_l2cap_timeouts = 1;
138866fb9560S[email protected] }
13898d213e1aSmatthias.ringwald // State-Module-Driver overview
13908d213e1aSmatthias.ringwald // state                    module  low-level
13918d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
13928d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
13938d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
13948d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1395d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1396d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1397c7e0c5f6Smatthias.ringwald 
139840d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
13997301ad89Smatthias.ringwald 
1400038bc64cSmatthias.ringwald     // power on
1401f9a30166Smatthias.ringwald     int err = 0;
14023a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
14033a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1404f9a30166Smatthias.ringwald     }
1405038bc64cSmatthias.ringwald     if (err){
14069da54300S[email protected]         log_error( "POWER_ON failed");
1407038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1408038bc64cSmatthias.ringwald         return err;
1409038bc64cSmatthias.ringwald     }
1410038bc64cSmatthias.ringwald 
1411038bc64cSmatthias.ringwald     // open low-level device
14123a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1413038bc64cSmatthias.ringwald     if (err){
14149da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
14153a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
14163a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1417f9a30166Smatthias.ringwald         }
1418038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1419038bc64cSmatthias.ringwald         return err;
1420038bc64cSmatthias.ringwald     }
14218d213e1aSmatthias.ringwald     return 0;
14228d213e1aSmatthias.ringwald }
1423038bc64cSmatthias.ringwald 
142440d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
14258d213e1aSmatthias.ringwald 
14269da54300S[email protected]     log_info("hci_power_control_off");
14279418f9c9Smatthias.ringwald 
14288d213e1aSmatthias.ringwald     // close low-level device
14293a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
14308d213e1aSmatthias.ringwald 
14319da54300S[email protected]     log_info("hci_power_control_off - hci_transport closed");
14329418f9c9Smatthias.ringwald 
14338d213e1aSmatthias.ringwald     // power off
14343a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
14353a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
14368d213e1aSmatthias.ringwald     }
14379418f9c9Smatthias.ringwald 
14389da54300S[email protected]     log_info("hci_power_control_off - control closed");
14399418f9c9Smatthias.ringwald 
14403a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
144172ea5239Smatthias.ringwald }
144272ea5239Smatthias.ringwald 
144340d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
144472ea5239Smatthias.ringwald 
14459da54300S[email protected]     log_info("hci_power_control_sleep");
14463144bce4Smatthias.ringwald 
1447b429b9b7Smatthias.ringwald #if 0
1448b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1449b429b9b7Smatthias.ringwald 
145072ea5239Smatthias.ringwald     // close low-level device
14513a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1452b429b9b7Smatthias.ringwald #endif
145372ea5239Smatthias.ringwald 
145472ea5239Smatthias.ringwald     // sleep mode
14553a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
14563a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
145772ea5239Smatthias.ringwald     }
1458b429b9b7Smatthias.ringwald 
14593a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
14608d213e1aSmatthias.ringwald }
14618d213e1aSmatthias.ringwald 
146240d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1463b429b9b7Smatthias.ringwald 
14649da54300S[email protected]     log_info("hci_power_control_wake");
1465b429b9b7Smatthias.ringwald 
1466b429b9b7Smatthias.ringwald     // wake on
14673a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
14683a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1469b429b9b7Smatthias.ringwald     }
1470b429b9b7Smatthias.ringwald 
1471b429b9b7Smatthias.ringwald #if 0
1472b429b9b7Smatthias.ringwald     // open low-level device
14733a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1474b429b9b7Smatthias.ringwald     if (err){
14759da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
14763a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
14773a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1478b429b9b7Smatthias.ringwald         }
1479b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1480b429b9b7Smatthias.ringwald         return err;
1481b429b9b7Smatthias.ringwald     }
1482b429b9b7Smatthias.ringwald #endif
1483b429b9b7Smatthias.ringwald 
1484b429b9b7Smatthias.ringwald     return 0;
1485b429b9b7Smatthias.ringwald }
1486b429b9b7Smatthias.ringwald 
148744935e40S[email protected] static void hci_power_transition_to_initializing(void){
148844935e40S[email protected]     // set up state machine
148944935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
149044935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
149144935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
149244935e40S[email protected]     hci_stack->substate = 0;
149344935e40S[email protected] }
1494b429b9b7Smatthias.ringwald 
14958d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1496d661ed19Smatthias.ringwald 
14979da54300S[email protected]     log_info("hci_power_control: %u, current mode %u", power_mode, hci_stack->state);
1498d661ed19Smatthias.ringwald 
14998d213e1aSmatthias.ringwald     int err = 0;
15003a9fb326S[email protected]     switch (hci_stack->state){
15018d213e1aSmatthias.ringwald 
15028d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
15038d213e1aSmatthias.ringwald             switch (power_mode){
15048d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
15058d213e1aSmatthias.ringwald                     err = hci_power_control_on();
150697b61c7bS[email protected]                     if (err) {
150797b61c7bS[email protected]                         log_error("hci_power_control_on() error %u", err);
150897b61c7bS[email protected]                         return err;
150997b61c7bS[email protected]                     }
151044935e40S[email protected]                     hci_power_transition_to_initializing();
15118d213e1aSmatthias.ringwald                     break;
15128d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
15138d213e1aSmatthias.ringwald                     // do nothing
15148d213e1aSmatthias.ringwald                     break;
15158d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1516b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
15178d213e1aSmatthias.ringwald                     break;
15188d213e1aSmatthias.ringwald             }
15198d213e1aSmatthias.ringwald             break;
15207301ad89Smatthias.ringwald 
15218d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
15228d213e1aSmatthias.ringwald             switch (power_mode){
15238d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
15248d213e1aSmatthias.ringwald                     // do nothing
15258d213e1aSmatthias.ringwald                     break;
15268d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
15278d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
15288d213e1aSmatthias.ringwald                     hci_power_control_off();
15298d213e1aSmatthias.ringwald                     break;
15308d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1531b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
153272ea5239Smatthias.ringwald                     hci_power_control_sleep();
15338d213e1aSmatthias.ringwald                     break;
15348d213e1aSmatthias.ringwald             }
15358d213e1aSmatthias.ringwald             break;
15367301ad89Smatthias.ringwald 
15378d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
15388d213e1aSmatthias.ringwald             switch (power_mode){
15398d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
15408d213e1aSmatthias.ringwald                     // do nothing
15418d213e1aSmatthias.ringwald                     break;
15428d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1543c7e0c5f6Smatthias.ringwald                     // see hci_run
15443a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
15458d213e1aSmatthias.ringwald                     break;
15468d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1547b546ac54Smatthias.ringwald                     // see hci_run
15483a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
15493a9fb326S[email protected]                     hci_stack->substate = 0;
15508d213e1aSmatthias.ringwald                     break;
15518d213e1aSmatthias.ringwald             }
15528d213e1aSmatthias.ringwald             break;
15537301ad89Smatthias.ringwald 
15548d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
15558d213e1aSmatthias.ringwald             switch (power_mode){
15568d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
155744935e40S[email protected]                     hci_power_transition_to_initializing();
15588d213e1aSmatthias.ringwald                     break;
15598d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
15608d213e1aSmatthias.ringwald                     // do nothing
15618d213e1aSmatthias.ringwald                     break;
15628d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1563b546ac54Smatthias.ringwald                     // see hci_run
15643a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
15653a9fb326S[email protected]                     hci_stack->substate = 0;
15668d213e1aSmatthias.ringwald                     break;
15678d213e1aSmatthias.ringwald             }
15688d213e1aSmatthias.ringwald             break;
15698d213e1aSmatthias.ringwald 
15708d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
15718d213e1aSmatthias.ringwald             switch (power_mode){
15728d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
157328171530Smatthias.ringwald 
157428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
157528171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
157628171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
15773a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
15783a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
157928171530Smatthias.ringwald                         break;
158028171530Smatthias.ringwald                     }
158128171530Smatthias.ringwald #endif
158244935e40S[email protected]                     hci_power_transition_to_initializing();
15838d213e1aSmatthias.ringwald                     break;
15848d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1585b546ac54Smatthias.ringwald                     // see hci_run
15863a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
15878d213e1aSmatthias.ringwald                     break;
15888d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1589b546ac54Smatthias.ringwald                     // do nothing
15908d213e1aSmatthias.ringwald                     break;
15918d213e1aSmatthias.ringwald             }
15928d213e1aSmatthias.ringwald             break;
15938d213e1aSmatthias.ringwald 
15948d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
15958d213e1aSmatthias.ringwald             switch (power_mode){
15968d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
159728171530Smatthias.ringwald 
159828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
159928171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
160028171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
16013a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
16023a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1603758b46ceSmatthias.ringwald                         hci_update_scan_enable();
160428171530Smatthias.ringwald                         break;
160528171530Smatthias.ringwald                     }
160628171530Smatthias.ringwald #endif
16073144bce4Smatthias.ringwald                     err = hci_power_control_wake();
16083144bce4Smatthias.ringwald                     if (err) return err;
160944935e40S[email protected]                     hci_power_transition_to_initializing();
16108d213e1aSmatthias.ringwald                     break;
16118d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
16123a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
16138d213e1aSmatthias.ringwald                     break;
16148d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1615b546ac54Smatthias.ringwald                     // do nothing
16168d213e1aSmatthias.ringwald                     break;
16178d213e1aSmatthias.ringwald             }
16188d213e1aSmatthias.ringwald             break;
161911e23e5fSmatthias.ringwald     }
162068d92d03Smatthias.ringwald 
1621038bc64cSmatthias.ringwald     // create internal event
1622ee8bf225Smatthias.ringwald 	hci_emit_state();
1623ee8bf225Smatthias.ringwald 
162468d92d03Smatthias.ringwald 	// trigger next/first action
162568d92d03Smatthias.ringwald 	hci_run();
162668d92d03Smatthias.ringwald 
1627475c8125Smatthias.ringwald     return 0;
1628475c8125Smatthias.ringwald }
1629475c8125Smatthias.ringwald 
1630758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1631758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
16323a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1633758b46ceSmatthias.ringwald     hci_run();
1634758b46ceSmatthias.ringwald }
1635758b46ceSmatthias.ringwald 
1636381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1637381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1638381fbed8Smatthias.ringwald 
16393a9fb326S[email protected]     if (hci_stack->discoverable == enable){
16403a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1641381fbed8Smatthias.ringwald         return;
1642381fbed8Smatthias.ringwald     }
1643381fbed8Smatthias.ringwald 
16443a9fb326S[email protected]     hci_stack->discoverable = enable;
1645758b46ceSmatthias.ringwald     hci_update_scan_enable();
1646758b46ceSmatthias.ringwald }
1647b031bebbSmatthias.ringwald 
1648758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1649758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1650758b46ceSmatthias.ringwald 
1651758b46ceSmatthias.ringwald     // don't emit event
16523a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1653758b46ceSmatthias.ringwald 
16543a9fb326S[email protected]     hci_stack->connectable = enable;
1655758b46ceSmatthias.ringwald     hci_update_scan_enable();
1656381fbed8Smatthias.ringwald }
1657381fbed8Smatthias.ringwald 
16585061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
16593a9fb326S[email protected]     return &hci_stack->local_bd_addr;
16605061f3afS[email protected] }
16615061f3afS[email protected] 
166206b35ec0Smatthias.ringwald void hci_run(){
16638a485f27Smatthias.ringwald 
166432ab9390Smatthias.ringwald     hci_connection_t * connection;
166532ab9390Smatthias.ringwald     linked_item_t * it;
166632ab9390Smatthias.ringwald 
1667d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()) return;
1668ce4c8fabSmatthias.ringwald 
1669b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1670b031bebbSmatthias.ringwald 
1671b031bebbSmatthias.ringwald     // decline incoming connections
16723a9fb326S[email protected]     if (hci_stack->decline_reason){
16733a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
16743a9fb326S[email protected]         hci_stack->decline_reason = 0;
16753a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1676dbe1a790S[email protected]         return;
1677ce4c8fabSmatthias.ringwald     }
1678ce4c8fabSmatthias.ringwald 
1679b031bebbSmatthias.ringwald     // send scan enable
16803a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
16813a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
16823a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1683dbe1a790S[email protected]         return;
1684b031bebbSmatthias.ringwald     }
1685b031bebbSmatthias.ringwald 
1686b2f949feS[email protected] #ifdef HAVE_BLE
16877bdc6798S[email protected]     // handle le scan
16887bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
16897bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
16907bdc6798S[email protected]             case LE_START_SCAN:
16917bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
16927bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
16937bdc6798S[email protected]                 return;
16947bdc6798S[email protected] 
16957bdc6798S[email protected]             case LE_STOP_SCAN:
16967bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
16977bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
16987bdc6798S[email protected]                 return;
16997bdc6798S[email protected]             default:
17007bdc6798S[email protected]                 break;
17017bdc6798S[email protected]         }
1702e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
1703e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
1704e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
1705e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
1706e2602ea2Smatthias.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);
1707e2602ea2Smatthias.ringwald             return;
1708e2602ea2Smatthias.ringwald         }
17097bdc6798S[email protected]     }
1710b2f949feS[email protected] #endif
17117bdc6798S[email protected] 
171232ab9390Smatthias.ringwald     // send pending HCI commands
17133a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
1714b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
171532ab9390Smatthias.ringwald 
17160bf6344aS[email protected]         switch(connection->state){
17170bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
17184f3229d8S[email protected]                 switch(connection->address_type){
17194f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
17209da54300S[email protected]                         log_info("sending hci_create_connection");
1721ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
17224f3229d8S[email protected]                         break;
17234f3229d8S[email protected]                     default:
1724b2f949feS[email protected] #ifdef HAVE_BLE
17259da54300S[email protected]                         log_info("sending hci_le_create_connection");
17264f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
1727b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
1728b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
17294f3229d8S[email protected]                                      0,         // don't use whitelist
17304f3229d8S[email protected]                                      connection->address_type, // peer address type
17314f3229d8S[email protected]                                      connection->address,      // peer bd addr
1732b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
1733b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
1734b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
17354f3229d8S[email protected]                                      0,         // conn latency
1736b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
1737b2571179Smatthias.ringwald                                      0x0001,    // min ce length
1738b2571179Smatthias.ringwald                                      0x0001     // max ce length
17394f3229d8S[email protected]                                      );
17404f3229d8S[email protected] 
17414f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
1742b2f949feS[email protected] #endif
17434f3229d8S[email protected]                         break;
17444f3229d8S[email protected]                 }
1745ad83dc6aS[email protected]                 return;
1746ad83dc6aS[email protected] 
17470bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
17489da54300S[email protected]                 log_info("sending hci_accept_connection_request");
174932ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
175034d2123cS[email protected]                 hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1751dbe1a790S[email protected]                 return;
17520bf6344aS[email protected] 
1753a6725849S[email protected] #ifdef HAVE_BLE
17540bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
17550bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
17560bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
17570bf6344aS[email protected]                 return;
1758a6725849S[email protected] #endif
17590bf6344aS[email protected]             case SEND_DISCONNECT:
17600bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
17617851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
17620bf6344aS[email protected]                 return;
17630bf6344aS[email protected] 
17640bf6344aS[email protected]             default:
17650bf6344aS[email protected]                 break;
1766c7e0c5f6Smatthias.ringwald         }
1767c7e0c5f6Smatthias.ringwald 
176832ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
17699da54300S[email protected]             log_info("responding to link key request");
177034d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
177132ab9390Smatthias.ringwald             link_key_t link_key;
1772c77e8838S[email protected]             link_key_type_t link_key_type;
17733a9fb326S[email protected]             if ( hci_stack->remote_device_db
17743a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
177534d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
17769ab95c90S[email protected]                connection->link_key_type = link_key_type;
177732ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
177832ab9390Smatthias.ringwald             } else {
177932ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
178032ab9390Smatthias.ringwald             }
1781dbe1a790S[email protected]             return;
178232ab9390Smatthias.ringwald         }
17831d6b20aeS[email protected] 
1784899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
17859da54300S[email protected]             log_info("denying to pin request");
1786899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
178734d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
17884c57c146S[email protected]             return;
17894c57c146S[email protected]         }
17904c57c146S[email protected] 
1791dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
179234d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
179382d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
179482d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1795106d6d11S[email protected]                 // tweak authentication requirements
17963a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1797106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
17989faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
17999faad3abS[email protected]                 }
18009faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
18019faad3abS[email protected]                     authreq |= 1;
1802106d6d11S[email protected]                 }
18033a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1804f8fb5f6eS[email protected]             } else {
1805f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1806f8fb5f6eS[email protected]             }
1807dbe1a790S[email protected]             return;
180832ab9390Smatthias.ringwald         }
180932ab9390Smatthias.ringwald 
1810dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1811dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
181234d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1813dbe1a790S[email protected]             return;
1814dbe1a790S[email protected]         }
1815dbe1a790S[email protected] 
1816dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1817dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
181834d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1819dbe1a790S[email protected]             return;
1820dbe1a790S[email protected]         }
1821afd4e962S[email protected] 
1822afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1823afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
182434d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
18252bd8b7e7S[email protected]             return;
18262bd8b7e7S[email protected]         }
18272bd8b7e7S[email protected] 
18282bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
18292bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
183034d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
183134d2123cS[email protected]             return;
183234d2123cS[email protected]         }
1833ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1834ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
18351bd5283dS[email protected]             connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT;
183652d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
1837ad83dc6aS[email protected]             return;
1838ad83dc6aS[email protected]         }
183934d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
184034d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
184134d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
18422bd8b7e7S[email protected]             return;
1843afd4e962S[email protected]         }
1844dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1845dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1846dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1847dce78009S[email protected]             return;
1848dce78009S[email protected]         }
1849c37a3166S[email protected] #ifdef HAVE_BLE
1850c37a3166S[email protected]         if (connection->le_conn_interval_min){
1851c37a3166S[email protected]             uint16_t connection_interval_min = connection->le_conn_interval_min;
1852c37a3166S[email protected]             connection->le_conn_interval_min = 0;
1853c37a3166S[email protected]             hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min,
1854c37a3166S[email protected]                 connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout,
1855c37a3166S[email protected]                 0x0000, 0xffff);
1856c37a3166S[email protected]         }
1857c37a3166S[email protected] #endif
1858dbe1a790S[email protected]     }
1859c7e0c5f6Smatthias.ringwald 
18603a9fb326S[email protected]     switch (hci_stack->state){
18613429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
1862bd88fde9S[email protected]             hci_initializing_state_machine();
18633429f56bSmatthias.ringwald             break;
1864c7e0c5f6Smatthias.ringwald 
1865c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1866c7e0c5f6Smatthias.ringwald 
18679da54300S[email protected]             log_info("HCI_STATE_HALTING");
1868c7e0c5f6Smatthias.ringwald             // close all open connections
18693a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1870c7e0c5f6Smatthias.ringwald             if (connection){
187132ab9390Smatthias.ringwald 
1872c7e0c5f6Smatthias.ringwald                 // send disconnect
1873d94d3cafS[email protected]                 if (!hci_can_send_command_packet_now()) return;
187432ab9390Smatthias.ringwald 
18759da54300S[email protected]                 log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, (uint16_t)connection->con_handle);
18766ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1877c7e0c5f6Smatthias.ringwald 
1878c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1879c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1880c7e0c5f6Smatthias.ringwald                 return;
1881c7e0c5f6Smatthias.ringwald             }
18829da54300S[email protected]             log_info("HCI_STATE_HALTING, calling off");
1883c7e0c5f6Smatthias.ringwald 
188472ea5239Smatthias.ringwald             // switch mode
1885c7e0c5f6Smatthias.ringwald             hci_power_control_off();
18869418f9c9Smatthias.ringwald 
18879da54300S[email protected]             log_info("HCI_STATE_HALTING, emitting state");
188872ea5239Smatthias.ringwald             hci_emit_state();
18899da54300S[email protected]             log_info("HCI_STATE_HALTING, done");
189072ea5239Smatthias.ringwald             break;
1891c7e0c5f6Smatthias.ringwald 
189272ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
18933a9fb326S[email protected]             switch(hci_stack->substate) {
189489db417bSmatthias.ringwald                 case 0:
18959da54300S[email protected]                     log_info("HCI_STATE_FALLING_ASLEEP");
189672ea5239Smatthias.ringwald                     // close all open connections
18973a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
189866da7044Smatthias.ringwald 
189928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
190066da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
190166da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
190266da7044Smatthias.ringwald                         connection = NULL;
190366da7044Smatthias.ringwald                     }
190466da7044Smatthias.ringwald #endif
190572ea5239Smatthias.ringwald                     if (connection){
190632ab9390Smatthias.ringwald 
190772ea5239Smatthias.ringwald                         // send disconnect
1908d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
190932ab9390Smatthias.ringwald 
19109da54300S[email protected]                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle);
19116ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
191272ea5239Smatthias.ringwald 
191372ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
191472ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
191572ea5239Smatthias.ringwald                         return;
191672ea5239Smatthias.ringwald                     }
191772ea5239Smatthias.ringwald 
191892368cd3S[email protected]                     if (hci_classic_supported()){
191989db417bSmatthias.ringwald                         // disable page and inquiry scan
1920d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
192132ab9390Smatthias.ringwald 
19229da54300S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans");
19233a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
192489db417bSmatthias.ringwald 
192589db417bSmatthias.ringwald                         // continue in next sub state
19263a9fb326S[email protected]                         hci_stack->substate++;
192789db417bSmatthias.ringwald                         break;
192892368cd3S[email protected]                     }
192992368cd3S[email protected]                     // fall through for ble-only chips
193092368cd3S[email protected] 
193189db417bSmatthias.ringwald                 case 2:
19329da54300S[email protected]                     log_info("HCI_STATE_HALTING, calling sleep");
193328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
193428171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
193528171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
193628171530Smatthias.ringwald                         // SLEEP MODE reached
19373a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
193828171530Smatthias.ringwald                         hci_emit_state();
193928171530Smatthias.ringwald                         break;
194028171530Smatthias.ringwald                     }
194128171530Smatthias.ringwald #endif
194272ea5239Smatthias.ringwald                     // switch mode
19433a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1944c7e0c5f6Smatthias.ringwald                     hci_emit_state();
194528171530Smatthias.ringwald                     break;
194628171530Smatthias.ringwald 
194789db417bSmatthias.ringwald                 default:
194889db417bSmatthias.ringwald                     break;
194989db417bSmatthias.ringwald             }
1950c7e0c5f6Smatthias.ringwald             break;
1951c7e0c5f6Smatthias.ringwald 
19523429f56bSmatthias.ringwald         default:
19533429f56bSmatthias.ringwald             break;
19541f504dbdSmatthias.ringwald     }
19553429f56bSmatthias.ringwald }
195616833f0aSmatthias.ringwald 
195731452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1958c8e4258aSmatthias.ringwald     bd_addr_t addr;
1959c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1960c8e4258aSmatthias.ringwald     // house-keeping
1961c8e4258aSmatthias.ringwald 
1962c8e4258aSmatthias.ringwald     // create_connection?
1963c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1964c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
19659da54300S[email protected]         log_info("Create_connection to %s", bd_addr_to_str(addr));
1966c8e4258aSmatthias.ringwald 
196796a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1968ad83dc6aS[email protected]         if (!conn){
196996a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
197017f1ba2aSmatthias.ringwald             if (!conn){
197117f1ba2aSmatthias.ringwald                 // notify client that alloc failed
197217f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
197317f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
197417f1ba2aSmatthias.ringwald             }
1975ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1976ad83dc6aS[email protected]         }
1977ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1978ad83dc6aS[email protected]         switch (conn->state){
1979ad83dc6aS[email protected]             // if connection active exists
1980ad83dc6aS[email protected]             case OPEN:
198162bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1982ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
198362bda3fbS[email protected]                 return 0;
1984ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1985ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1986ad83dc6aS[email protected]                 break;
1987ad83dc6aS[email protected]             default:
1988ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1989ad83dc6aS[email protected]                 return 0;
1990ad83dc6aS[email protected]         }
1991c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1992c8e4258aSmatthias.ringwald     }
1993c8e4258aSmatthias.ringwald 
19947fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
19957fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
19967fde4af9Smatthias.ringwald     }
19977fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
19987fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
19997fde4af9Smatthias.ringwald     }
20007fde4af9Smatthias.ringwald 
20018ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
20023a9fb326S[email protected]         if (hci_stack->remote_device_db){
20038ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
20043a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
20058ef73945Smatthias.ringwald         }
20068ef73945Smatthias.ringwald     }
2007c8e4258aSmatthias.ringwald 
20086724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
20096724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
20106724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
201196a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
20126724cd9eS[email protected]         if (conn){
20136724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
20146724cd9eS[email protected]         }
20156724cd9eS[email protected]     }
20166724cd9eS[email protected] 
20176724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
20186724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
20196724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
20206724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
20216724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
202296a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
20236724cd9eS[email protected]         if (conn){
20246724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
20256724cd9eS[email protected]         }
20266724cd9eS[email protected]     }
20276724cd9eS[email protected] 
202869a97523S[email protected] #ifdef HAVE_BLE
202969a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
20303a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
203169a97523S[email protected]     }
203269a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
20333a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
203469a97523S[email protected]     }
203569a97523S[email protected] #endif
203669a97523S[email protected] 
20373a9fb326S[email protected]     hci_stack->num_cmd_packets--;
20386b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
20396b4af23dS[email protected] 
20406b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
2041c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
20426b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
20436b4af23dS[email protected]     }
20446b4af23dS[email protected] 
20456b4af23dS[email protected]     return err;
204631452debSmatthias.ringwald }
20478adf0ddaSmatthias.ringwald 
20482bd8b7e7S[email protected] // disconnect because of security block
20492bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
20502bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
20512bd8b7e7S[email protected]     if (!connection) return;
20522bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
20532bd8b7e7S[email protected] }
20542bd8b7e7S[email protected] 
20552bd8b7e7S[email protected] 
2056dbe1a790S[email protected] // Configure Secure Simple Pairing
2057dbe1a790S[email protected] 
2058dbe1a790S[email protected] // enable will enable SSP during init
2059dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
20603a9fb326S[email protected]     hci_stack->ssp_enable = enable;
2061dbe1a790S[email protected] }
2062dbe1a790S[email protected] 
20632bd8b7e7S[email protected] int hci_local_ssp_activated(){
20643a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
20652bd8b7e7S[email protected] }
20662bd8b7e7S[email protected] 
2067dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
2068dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
20693a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
2070dbe1a790S[email protected] }
2071dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
20723a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
2073dbe1a790S[email protected] }
2074dbe1a790S[email protected] 
2075dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
2076dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
20773a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
2078dbe1a790S[email protected] }
2079dbe1a790S[email protected] 
20801cd208adSmatthias.ringwald /**
20811cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
20821cd208adSmatthias.ringwald  */
2083fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
20845127cc62S[email protected] 
2085d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()){
20869d14b626S[email protected]         log_error("hci_send_cmd called but cannot send packet now");
20879d14b626S[email protected]         return 0;
20889d14b626S[email protected]     }
20899d14b626S[email protected] 
20905127cc62S[email protected]     // for HCI INITIALIZATION
20919da54300S[email protected]     // log_info("hci_send_cmd: opcode %04x", cmd->opcode);
20925127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
20935127cc62S[email protected] 
20949d14b626S[email protected]     hci_reserve_packet_buffer();
20959d14b626S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
20969d14b626S[email protected] 
20971cd208adSmatthias.ringwald     va_list argptr;
20981cd208adSmatthias.ringwald     va_start(argptr, cmd);
20999d14b626S[email protected]     uint16_t size = hci_create_cmd_internal(packet, cmd, argptr);
21001cd208adSmatthias.ringwald     va_end(argptr);
21019d14b626S[email protected] 
21029d14b626S[email protected]     return hci_send_cmd_packet(packet, size);
210393b8dc03Smatthias.ringwald }
2104c8e4258aSmatthias.ringwald 
2105ee091cf1Smatthias.ringwald // Create various non-HCI events.
2106ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
2107ee091cf1Smatthias.ringwald 
2108c8e4258aSmatthias.ringwald void hci_emit_state(){
21093a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
2110425d1371Smatthias.ringwald     uint8_t event[3];
211180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
2112425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
21133a9fb326S[email protected]     event[2] = hci_stack->state;
2114425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21153a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2116c8e4258aSmatthias.ringwald }
2117c8e4258aSmatthias.ringwald 
211817f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
2119425d1371Smatthias.ringwald     uint8_t event[13];
2120c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
2121425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
212217f1ba2aSmatthias.ringwald     event[2] = status;
2123c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
2124c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
2125c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
2126c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
2127425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
21283a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2129c8e4258aSmatthias.ringwald }
2130c8e4258aSmatthias.ringwald 
21314f3229d8S[email protected] void hci_emit_le_connection_complete(hci_connection_t *conn, uint8_t status){
21324f3229d8S[email protected]     uint8_t event[21];
21334f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
21344f3229d8S[email protected]     event[1] = sizeof(event) - 2;
21354f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
21364f3229d8S[email protected]     event[3] = status;
21374f3229d8S[email protected]     bt_store_16(event, 4, conn->con_handle);
21384f3229d8S[email protected]     event[6] = 0; // TODO: role
21394f3229d8S[email protected]     event[7] = conn->address_type;
21404f3229d8S[email protected]     bt_flip_addr(&event[8], conn->address);
21414f3229d8S[email protected]     bt_store_16(event, 14, 0); // interval
21424f3229d8S[email protected]     bt_store_16(event, 16, 0); // latency
21434f3229d8S[email protected]     bt_store_16(event, 18, 0); // supervision timeout
21444f3229d8S[email protected]     event[20] = 0; // master clock accuracy
21454f3229d8S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
21464f3229d8S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
21474f3229d8S[email protected] }
21484f3229d8S[email protected] 
21493c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
2150425d1371Smatthias.ringwald     uint8_t event[6];
21513c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
2152e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
21533c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
21543c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
21553c4d4b90Smatthias.ringwald     event[5] = reason;
2156425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
21573a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
21583c4d4b90Smatthias.ringwald }
21593c4d4b90Smatthias.ringwald 
2160ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
216166fb9560S[email protected]     if (disable_l2cap_timeouts) return;
2162e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
2163425d1371Smatthias.ringwald     uint8_t event[4];
216480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
2165425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2166ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
2167425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
21683a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2169ee091cf1Smatthias.ringwald }
217043bfb1bdSmatthias.ringwald 
217143bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
2172e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
2173425d1371Smatthias.ringwald     uint8_t event[3];
217480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
2175425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
217643bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
2177425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21783a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
217943bfb1bdSmatthias.ringwald }
2180038bc64cSmatthias.ringwald 
2181038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
2182e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
2183425d1371Smatthias.ringwald     uint8_t event[2];
218480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
2185425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2186425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21873a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2188038bc64cSmatthias.ringwald }
21891b0e3922Smatthias.ringwald 
219009ba8edeSmatthias.ringwald #ifndef EMBEDDED
21911b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
2192e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
2193425d1371Smatthias.ringwald     uint8_t event[6];
21941b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
2195425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2196425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
2197425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
2198425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
2199425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22003a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
22011b0e3922Smatthias.ringwald }
220209ba8edeSmatthias.ringwald #endif
22031b0e3922Smatthias.ringwald 
22042ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
2205e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
2206425d1371Smatthias.ringwald     uint8_t event[3];
22072ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
2208425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
22092ed6235cSmatthias.ringwald     event[2] = enabled;
2210425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22113a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
22122ed6235cSmatthias.ringwald }
2213627c2f45Smatthias.ringwald 
2214627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
2215e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
2216627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
2217e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
2218f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
22192f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
2220f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
2221e0abb8e7S[email protected] 
2222e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
2223e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
2224e0abb8e7S[email protected] 
2225e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
22263a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
2227627c2f45Smatthias.ringwald }
2228381fbed8Smatthias.ringwald 
2229381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
2230e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
2231425d1371Smatthias.ringwald     uint8_t event[3];
2232381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
2233425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2234381fbed8Smatthias.ringwald     event[2] = enabled;
2235425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22363a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2237381fbed8Smatthias.ringwald }
2238458bf4e8S[email protected] 
2239a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
2240df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
2241a00031e2S[email protected]     uint8_t event[5];
2242e00caf9cS[email protected]     int pos = 0;
2243a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
2244e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
2245a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
2246e00caf9cS[email protected]     pos += 2;
2247e00caf9cS[email protected]     event[pos++] = level;
2248e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22493a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2250e00caf9cS[email protected] }
2251e00caf9cS[email protected] 
22521bd5283dS[email protected] void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){
2253ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
2254ad83dc6aS[email protected]     uint8_t event[9];
2255ad83dc6aS[email protected]     int pos = 0;
2256ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
2257ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
2258ad83dc6aS[email protected]     event[pos++] = status;
22591bd5283dS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], address);
2260ad83dc6aS[email protected]     pos += 6;
2261ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22623a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2263ad83dc6aS[email protected] }
2264ad83dc6aS[email protected] 
22652bd8b7e7S[email protected] // query if remote side supports SSP
22662bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
22672bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
22682bd8b7e7S[email protected]     if (!connection) return 0;
22692bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
22702bd8b7e7S[email protected] }
22712bd8b7e7S[email protected] 
2272df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
2273df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
2274df3354fcS[email protected] }
2275df3354fcS[email protected] 
2276458bf4e8S[email protected] // GAP API
2277458bf4e8S[email protected] /**
2278458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
2279458bf4e8S[email protected]  * @praram enabled
2280458bf4e8S[email protected]  */
22814c57c146S[email protected] void gap_set_bondable_mode(int enable){
22823a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
2283458bf4e8S[email protected] }
2284cb230b9dS[email protected] 
2285cb230b9dS[email protected] /**
228634d2123cS[email protected]  * @brief map link keys to security levels
2287cb230b9dS[email protected]  */
228834d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
228934d2123cS[email protected]     switch (link_key_type){
22903c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
22913c68dfa9S[email protected]             return LEVEL_4;
22923c68dfa9S[email protected]         case COMBINATION_KEY:
22933c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
22943c68dfa9S[email protected]             return LEVEL_3;
22953c68dfa9S[email protected]         default:
22963c68dfa9S[email protected]             return LEVEL_2;
22973c68dfa9S[email protected]     }
2298cb230b9dS[email protected] }
2299cb230b9dS[email protected] 
2300a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
230134d2123cS[email protected]     if (!connection) return LEVEL_0;
230234d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
230334d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
230434d2123cS[email protected] }
230534d2123cS[email protected] 
230634d2123cS[email protected] 
2307106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
23085127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
2309106d6d11S[email protected]     return level > LEVEL_2;
2310106d6d11S[email protected] }
2311106d6d11S[email protected] 
231234d2123cS[email protected] /**
231334d2123cS[email protected]  * @brief get current security level
231434d2123cS[email protected]  */
231534d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
231634d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
231734d2123cS[email protected]     if (!connection) return LEVEL_0;
231834d2123cS[email protected]     return gap_security_level_for_connection(connection);
231934d2123cS[email protected] }
232034d2123cS[email protected] 
2321cb230b9dS[email protected] /**
2322cb230b9dS[email protected]  * @brief request connection to device to
2323cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
2324cb230b9dS[email protected]  */
232534d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
232634d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
232734d2123cS[email protected]     if (!connection){
2328a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
232934d2123cS[email protected]         return;
233034d2123cS[email protected]     }
233134d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
233234d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
233334d2123cS[email protected]     if (current_level >= requested_level){
2334a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
233534d2123cS[email protected]         return;
233634d2123cS[email protected]     }
2337a00031e2S[email protected] 
233834d2123cS[email protected]     connection->requested_security_level = requested_level;
2339a00031e2S[email protected] 
2340fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
23413a9fb326S[email protected]     if (hci_stack->remote_device_db){
2342a00031e2S[email protected]         link_key_type_t link_key_type;
2343a00031e2S[email protected]         link_key_t      link_key;
23443a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2345a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2346a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2347a00031e2S[email protected]                 return;
2348a00031e2S[email protected]             }
2349a00031e2S[email protected]         }
2350a00031e2S[email protected]     }
2351a00031e2S[email protected] 
23521eb2563eS[email protected]     // try to authenticate connection
23531eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2354e80b2cf9S[email protected]     hci_run();
2355e00caf9cS[email protected] }
2356ad83dc6aS[email protected] 
2357ad83dc6aS[email protected] /**
2358ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2359ad83dc6aS[email protected]  * @param device
2360ad83dc6aS[email protected]  * @param request MITM protection
2361ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2362ad83dc6aS[email protected]  */
2363ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2364ad83dc6aS[email protected] 
2365ad83dc6aS[email protected]     // create connection state machine
236696a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2367ad83dc6aS[email protected] 
2368ad83dc6aS[email protected]     if (!connection){
2369ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2370ad83dc6aS[email protected]     }
2371ad83dc6aS[email protected] 
2372ad83dc6aS[email protected]     // delete linkn key
2373ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
2374ad83dc6aS[email protected] 
2375ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2376ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2377ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
23785127cc62S[email protected]     log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level);
2379ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2380ad83dc6aS[email protected] 
2381ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2382ad83dc6aS[email protected] 
2383ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2384ad83dc6aS[email protected]     // handle: authentication failure
2385ad83dc6aS[email protected]     // handle: disconnect on done
2386ad83dc6aS[email protected] 
2387ad83dc6aS[email protected]     hci_run();
2388ad83dc6aS[email protected] 
2389ad83dc6aS[email protected]     return 0;
2390ad83dc6aS[email protected] }
23918e618f72S[email protected] 
23928e618f72S[email protected] void gap_set_local_name(const char * local_name){
23938e618f72S[email protected]     hci_stack->local_name = local_name;
23948e618f72S[email protected] }
23958e618f72S[email protected] 
23967bdc6798S[email protected] le_command_status_t le_central_start_scan(){
239736af3e18S[email protected]     if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK;
23987bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
23997bdc6798S[email protected]     hci_run();
24007bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
24017bdc6798S[email protected] }
24028e618f72S[email protected] 
24037bdc6798S[email protected] le_command_status_t le_central_stop_scan(){
240436af3e18S[email protected]     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK;
24057bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
24067bdc6798S[email protected]     hci_run();
24077bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
24087bdc6798S[email protected] }
24094f3229d8S[email protected] 
2410ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
2411ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
2412ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
2413ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
2414ef11999fSmatthias.ringwald     hci_run();
2415ef11999fSmatthias.ringwald }
24164f3229d8S[email protected] 
24174f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){
24184f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
24194f3229d8S[email protected]     if (!conn){
24201f479f8cS[email protected]         log_info("le_central_connect: no connection exists yet, creating context");
24214f3229d8S[email protected]         conn = create_connection_for_bd_addr_and_type(*addr, addr_type);
24224f3229d8S[email protected]         if (!conn){
24234f3229d8S[email protected]             // notify client that alloc failed
24244f3229d8S[email protected]             hci_emit_le_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
24251f479f8cS[email protected]             log_info("le_central_connect: failed to alloc context");
24264f3229d8S[email protected]             return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller
24274f3229d8S[email protected]         }
24284f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
24291f479f8cS[email protected]         log_info("le_central_connect: send create connection next");
2430564fca32S[email protected]         hci_run();
24314f3229d8S[email protected]         return BLE_PERIPHERAL_OK;
24324f3229d8S[email protected]     }
24330bf6344aS[email protected] 
24340bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
24350bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
24360bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
24370bf6344aS[email protected]         hci_emit_le_connection_complete(conn, ERROR_CODE_COMMAND_DISALLOWED);
24381f479f8cS[email protected]         log_error("le_central_connect: classic connection or connect is already being created");
24390bf6344aS[email protected]         return BLE_PERIPHERAL_IN_WRONG_STATE;
24400bf6344aS[email protected]     }
24410bf6344aS[email protected] 
24421f479f8cS[email protected]     log_info("le_central_connect: context exists with state %u", conn->state);
24434f3229d8S[email protected]     hci_emit_le_connection_complete(conn, 0);
24444f3229d8S[email protected]     hci_run();
24454f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
24464f3229d8S[email protected] }
24474f3229d8S[email protected] 
24487851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
24497851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){
24500bf6344aS[email protected]     linked_item_t *it;
24510bf6344aS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
24520bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
24530bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
24540bf6344aS[email protected]         switch (conn->state){
2455a6725849S[email protected]             case SEND_CREATE_CONNECTION:
24567851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
24577851196eSmatthias.ringwald                 return conn;
24587851196eSmatthias.ringwald             default:
24597851196eSmatthias.ringwald                 break;
24607851196eSmatthias.ringwald         };
24617851196eSmatthias.ringwald     }
24627851196eSmatthias.ringwald     return NULL;
24637851196eSmatthias.ringwald }
24647851196eSmatthias.ringwald 
24657851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){
24667851196eSmatthias.ringwald     hci_connection_t * conn = le_central_get_outgoing_connection();
24677851196eSmatthias.ringwald     switch (conn->state){
24687851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
24697851196eSmatthias.ringwald             // skip sending create connection and emit event instead
24700bf6344aS[email protected]             hci_emit_le_connection_complete(conn, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
24717851196eSmatthias.ringwald             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
24727851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
24730bf6344aS[email protected]             break;
2474a6725849S[email protected]         case SENT_CREATE_CONNECTION:
24757851196eSmatthias.ringwald             // request to send cancel connection
24760bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
24770bf6344aS[email protected]             hci_run();
24780bf6344aS[email protected]             break;
24790bf6344aS[email protected]         default:
24800bf6344aS[email protected]             break;
24810bf6344aS[email protected]     }
2482e31f89a7S[email protected]     return BLE_PERIPHERAL_OK;
2483e31f89a7S[email protected] }
24844f3229d8S[email protected] 
2485c37a3166S[email protected] /**
2486c37a3166S[email protected]  * @brief Updates the connection parameters for a given LE connection
2487c37a3166S[email protected]  * @param handle
2488c37a3166S[email protected]  * @param conn_interval_min (unit: 1.25ms)
2489c37a3166S[email protected]  * @param conn_interval_max (unit: 1.25ms)
2490c37a3166S[email protected]  * @param conn_latency
2491c37a3166S[email protected]  * @param supervision_timeout (unit: 10ms)
2492c37a3166S[email protected]  * @returns 0 if ok
2493c37a3166S[email protected]  */
2494c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min,
2495c37a3166S[email protected]     uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
2496c37a3166S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
2497c37a3166S[email protected]     if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2498c37a3166S[email protected]     connection->le_conn_interval_min = conn_interval_min;
2499c37a3166S[email protected]     connection->le_conn_interval_max = conn_interval_max;
2500c37a3166S[email protected]     connection->le_conn_latency = conn_latency;
2501c37a3166S[email protected]     connection->le_supervision_timeout = supervision_timeout;
2502c37a3166S[email protected]     return 0;
2503c37a3166S[email protected] }
2504c37a3166S[email protected] 
25055917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){
25065917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
25075917a5c5S[email protected]     if (!conn){
25087851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
25095917a5c5S[email protected]         return BLE_PERIPHERAL_OK;
25105917a5c5S[email protected]     }
25115917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
25125917a5c5S[email protected]     hci_run();
25134f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
25144f3229d8S[email protected] }
251504a6ef8cSmatthias.ringwald 
251604a6ef8cSmatthias.ringwald void hci_disconnect_all(){
251704a6ef8cSmatthias.ringwald     linked_list_iterator_t it;
251804a6ef8cSmatthias.ringwald     linked_list_iterator_init(&it, &hci_stack->connections);
251904a6ef8cSmatthias.ringwald     while (linked_list_iterator_has_next(&it)){
252004a6ef8cSmatthias.ringwald         hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it);
252104a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
252204a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
252304a6ef8cSmatthias.ringwald     }
2524d31fba26S[email protected]     hci_run();
252504a6ef8cSmatthias.ringwald }
2526