xref: /btstack/src/hci.c (revision 998906cd833194d65c34b7191899156f7a3d61c8)
1 /*
2  * Copyright (C) 2009 by Matthias Ringwald
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 /*
33  *  hci.c
34  *
35  *  Created by Matthias Ringwald on 4/29/09.
36  *
37  */
38 
39 #include "hci.h"
40 
41 #include <unistd.h>
42 #include <stdarg.h>
43 #include <string.h>
44 #include <stdio.h>
45 
46 #include "debug.h"
47 #include "hci_dump.h"
48 
49 #include "../include/btstack/hci_cmds.h"
50 #include "../include/btstack/version.h"
51 
52 // temp
53 #include "l2cap.h"
54 
55 #define HCI_CONNECTION_TIMEOUT_MS 10000
56 
57 // the STACK is here
58 static hci_stack_t       hci_stack;
59 
60 /**
61  * get connection for a given handle
62  *
63  * @return connection OR NULL, if not found
64  */
65 hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
66     linked_item_t *it;
67     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
68         if ( ((hci_connection_t *) it)->con_handle == con_handle){
69             return (hci_connection_t *) it;
70         }
71     }
72     return NULL;
73 }
74 
75 static void hci_connection_timeout_handler(timer_source_t *timer){
76     hci_connection_t * connection = linked_item_get_user(&timer->item);
77     struct timeval tv;
78     gettimeofday(&tv, NULL);
79     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
80         // connections might be timed out
81         hci_emit_l2cap_check_timeout(connection);
82         run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
83     } else {
84         // next timeout check at
85         timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
86     }
87     run_loop_add_timer(timer);
88 }
89 
90 static void hci_connection_timestamp(hci_connection_t *connection){
91     gettimeofday(&connection->timestamp, NULL);
92 }
93 
94 /**
95  * create connection for given address
96  *
97  * @return connection OR NULL, if not found
98  */
99 static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
100     hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
101     if (!conn) return NULL;
102     BD_ADDR_COPY(conn->address, addr);
103     conn->con_handle = 0xffff;
104     conn->flags = 0;
105     linked_item_set_user(&conn->timeout.item, conn);
106     conn->timeout.process = hci_connection_timeout_handler;
107     hci_connection_timestamp(conn);
108     conn->acl_recombination_length = 0;
109     conn->acl_recombination_pos = 0;
110     conn->num_acl_packets_sent = 0;
111     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
112     return conn;
113 }
114 
115 /**
116  * get connection for given address
117  *
118  * @return connection OR NULL, if not found
119  */
120 static hci_connection_t * connection_for_address(bd_addr_t address){
121     linked_item_t *it;
122     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
123         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
124             return (hci_connection_t *) it;
125         }
126     }
127     return NULL;
128 }
129 
130 /**
131  * count connections
132  */
133 static int nr_hci_connections(){
134     int count = 0;
135     linked_item_t *it;
136     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
137     return count;
138 }
139 
140 /**
141  * Dummy handler called by HCI
142  */
143 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
144 }
145 
146 /**
147  * Dummy control handler
148  */
149 static int null_control_function(void *config){
150     return 0;
151 }
152 static const char * null_control_name(void *config){
153     return "Hardware unknown";
154 }
155 static bt_control_t null_control = {
156     null_control_function,
157     null_control_function,
158     null_control_function,
159     null_control_name
160 };
161 
162 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
163     hci_connection_t * connection = connection_for_handle(handle);
164     if (!connection) {
165         log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
166         return 0;
167     }
168     return connection->num_acl_packets_sent;
169 }
170 
171 uint8_t hci_number_free_acl_slots(){
172     uint8_t free_slots = hci_stack.total_num_acl_packets;
173     linked_item_t *it;
174     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
175         hci_connection_t * connection = (hci_connection_t *) it;
176         if (free_slots < connection->num_acl_packets_sent) {
177             log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
178             return 0;
179         }
180         free_slots -= connection->num_acl_packets_sent;
181     }
182     return free_slots;
183 }
184 
185 int hci_ready_to_send(hci_con_handle_t handle){
186     return hci_number_free_acl_slots() && hci_number_outgoing_packets(handle) < 2;
187 }
188 
189 int hci_send_acl_packet(uint8_t *packet, int size){
190 
191     // update idle timestamp
192     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
193     hci_connection_t *connection = connection_for_handle( con_handle);
194     if (!connection) return 0;
195     hci_connection_timestamp(connection);
196 
197     // count packet
198     connection->num_acl_packets_sent++;
199     log_dbg("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
200 
201     // send packet
202     return hci_stack.hci_transport->send_acl_packet(packet, size);
203 }
204 
205 static void acl_handler(uint8_t *packet, int size){
206 
207     // get info
208     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
209     hci_connection_t *conn      = connection_for_handle(con_handle);
210     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
211     uint16_t acl_length         = READ_ACL_LENGTH(packet);
212 
213     // ignore non-registered handle
214     if (!conn){
215         log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
216         return;
217     }
218 
219     // update idle timestamp
220     hci_connection_timestamp(conn);
221 
222     // handle different packet types
223     switch (acl_flags & 0x03) {
224 
225         case 0x01: // continuation fragment
226 
227             // sanity check
228             if (conn->acl_recombination_pos == 0) {
229                 log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
230                 return;
231             }
232 
233             // append fragment payload (header already stored)
234             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
235             conn->acl_recombination_pos += acl_length;
236 
237             // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n",
238             //        acl_length, connection->acl_recombination_pos, connection->acl_recombination_length);
239 
240             // forward complete L2CAP packet if complete.
241             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
242 
243                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
244                 // reset recombination buffer
245                 conn->acl_recombination_length = 0;
246                 conn->acl_recombination_pos = 0;
247             }
248             break;
249 
250         case 0x02: { // first fragment
251 
252             // sanity check
253             if (conn->acl_recombination_pos) {
254                 log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
255                 return;
256             }
257 
258             // peek into L2CAP packet!
259             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
260 
261             // compare fragment size to L2CAP packet size
262             if (acl_length >= l2cap_length + 4){
263 
264                 // forward fragment as L2CAP packet
265                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
266 
267             } else {
268                 // store first fragment and tweak acl length for complete package
269                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
270                 conn->acl_recombination_pos    = acl_length + 4;
271                 conn->acl_recombination_length = l2cap_length;
272                 bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4);
273                 // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
274             }
275             break;
276 
277         }
278         default:
279             log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
280             return;
281     }
282 
283     // execute main loop
284     hci_run();
285 }
286 
287 static void event_handler(uint8_t *packet, int size){
288     bd_addr_t addr;
289     hci_con_handle_t handle;
290     hci_connection_t * conn;
291     int i;
292 
293     // get num_cmd_packets
294     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
295         // Get Num_HCI_Command_Packets
296         hci_stack.num_cmd_packets = packet[2];
297     }
298 
299     switch (packet[0]) {
300 
301         case HCI_EVENT_COMMAND_COMPLETE:
302             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
303                 // from offset 5
304                 // status
305                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
306                 // ignore: SCO data packet len (8)
307                 hci_stack.total_num_acl_packets  = packet[9];
308                 // ignore: total num SCO packets
309                 if (hci_stack.state == HCI_STATE_INITIALIZING){
310                     log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
311                 }
312             }
313             break;
314 
315         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
316             for (i=0; i<packet[2];i++){
317                 handle = READ_BT_16(packet, 3 + 2*i);
318                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
319                 conn = connection_for_handle(handle);
320                 if (!conn){
321                     log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
322                     continue;
323                 }
324                 conn->num_acl_packets_sent -= num_packets;
325                 log_dbg("hci_number_completed_packet (hci) %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
326             }
327             break;
328 
329         case HCI_EVENT_CONNECTION_REQUEST:
330             bt_flip_addr(addr, &packet[2]);
331             // TODO: eval COD 8-10
332             uint8_t link_type = packet[11];
333             log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type);
334             if (link_type == 1) { // ACL
335                 conn = connection_for_address(addr);
336                 if (!conn) {
337                     conn = create_connection_for_addr(addr);
338                 }
339                 // TODO: check for malloc failure
340                 conn->state = ACCEPTED_CONNECTION_REQUEST;
341                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
342             } else {
343                 // TODO: decline request
344             }
345             break;
346 
347         case HCI_EVENT_CONNECTION_COMPLETE:
348             // Connection management
349             bt_flip_addr(addr, &packet[5]);
350             log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n");
351             conn = connection_for_address(addr);
352             if (conn) {
353                 if (!packet[2]){
354                     conn->state = OPEN;
355                     conn->con_handle = READ_BT_16(packet, 3);
356                     conn->flags = 0;
357 
358                     gettimeofday(&conn->timestamp, NULL);
359                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
360                     run_loop_add_timer(&conn->timeout);
361 
362                     log_dbg("New connection: handle %u, ", conn->con_handle);
363                     print_bd_addr( conn->address );
364                     log_dbg("\n");
365 
366                     hci_emit_nr_connections_changed();
367                 } else {
368                     // connection failed, remove entry
369                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
370                     free( conn );
371                 }
372             }
373             break;
374 
375         case HCI_EVENT_DISCONNECTION_COMPLETE:
376             if (!packet[2]){
377                 handle = READ_BT_16(packet, 3);
378                 hci_connection_t * conn = connection_for_handle(handle);
379                 if (conn) {
380                     log_dbg("Connection closed: handle %u, ", conn->con_handle);
381                     print_bd_addr( conn->address );
382                     log_dbg("\n");
383                     run_loop_remove_timer(&conn->timeout);
384                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
385                     free( conn );
386                     hci_emit_nr_connections_changed();
387                 }
388             }
389             break;
390 
391         default:
392             break;
393     }
394 
395     // handle BT initialization
396     if (hci_stack.state == HCI_STATE_INITIALIZING){
397         // handle H4 synchronization loss on restart
398         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
399         //    hci_stack.substate = 0;
400         // }
401         // handle normal init sequence
402         if (hci_stack.substate % 2){
403             // odd: waiting for event
404             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
405                 hci_stack.substate++;
406             }
407         }
408     }
409 
410     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
411 
412 	// execute main loop
413 	hci_run();
414 }
415 
416 void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
417     switch (packet_type) {
418         case HCI_EVENT_PACKET:
419             event_handler(packet, size);
420             break;
421         case HCI_ACL_DATA_PACKET:
422             acl_handler(packet, size);
423             break;
424         default:
425             break;
426     }
427 }
428 
429 /** Register HCI packet handlers */
430 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
431     hci_stack.packet_handler = handler;
432 }
433 
434 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
435 
436     // reference to use transport layer implementation
437     hci_stack.hci_transport = transport;
438 
439     // references to used control implementation
440     if (control) {
441         hci_stack.control = control;
442     } else {
443         hci_stack.control = &null_control;
444     }
445 
446     // reference to used config
447     hci_stack.config = config;
448 
449     // no connections yet
450     hci_stack.connections = NULL;
451 
452     // empty cmd buffer
453     hci_stack.hci_cmd_buffer = malloc(3+255);
454 
455     // higher level handler
456     hci_stack.packet_handler = dummy_handler;
457 
458     // register packet handlers with transport
459     transport->register_packet_handler(&packet_handler);
460 }
461 
462 int hci_power_control(HCI_POWER_MODE power_mode){
463     if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
464 
465         // power on
466         int err = hci_stack.control->on(hci_stack.config);
467         if (err){
468             log_err( "POWER_ON failed\n");
469             hci_emit_hci_open_failed();
470             return err;
471         }
472 
473         // open low-level device
474         err = hci_stack.hci_transport->open(hci_stack.config);
475         if (err){
476             log_err( "HCI_INIT failed, turning Bluetooth off again\n");
477             hci_stack.control->off(hci_stack.config);
478             hci_emit_hci_open_failed();
479             return err;
480         }
481 
482         // set up state machine
483         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
484         hci_stack.state = HCI_STATE_INITIALIZING;
485         hci_stack.substate = 0;
486 
487     } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
488 
489         // close low-level device
490         hci_stack.hci_transport->close(hci_stack.config);
491 
492         // power off
493         hci_stack.control->off(hci_stack.config);
494 
495         // we're off now
496         hci_stack.state = HCI_STATE_OFF;
497     }
498 
499     // create internal event
500 	hci_emit_state();
501 
502 	// trigger next/first action
503 	hci_run();
504 
505     return 0;
506 }
507 
508 void hci_run(){
509     switch (hci_stack.state){
510         case HCI_STATE_INITIALIZING:
511             if (hci_stack.substate % 2) {
512                 // odd: waiting for command completion
513                 return;
514             }
515             if (hci_stack.num_cmd_packets == 0) {
516                 // cannot send command yet
517                 return;
518             }
519             switch (hci_stack.substate/2){
520                 case 0:
521                     hci_send_cmd(&hci_reset);
522                     break;
523 				case 1:
524 					hci_send_cmd(&hci_read_bd_addr);
525 					break;
526 				case 2:
527 					hci_send_cmd(&hci_read_buffer_size);
528 					break;
529                 case 3:
530                     // ca. 15 sec
531                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
532                     break;
533 				case 4:
534 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
535 					break;
536                 case 5:
537                     // done.
538                     hci_stack.state = HCI_STATE_WORKING;
539                     hci_emit_state();
540                     break;
541                 default:
542                     break;
543             }
544             hci_stack.substate++;
545             break;
546         default:
547             break;
548     }
549 }
550 
551 int hci_send_cmd_packet(uint8_t *packet, int size){
552     bd_addr_t addr;
553     hci_connection_t * conn;
554     // house-keeping
555 
556     // create_connection?
557     if (IS_COMMAND(packet, hci_create_connection)){
558         bt_flip_addr(addr, &packet[3]);
559         log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n");
560         conn = connection_for_address(addr);
561         if (conn) {
562             // if connection exists
563             if (conn->state == OPEN) {
564                 // if OPEN, emit connection complete command
565                 hci_emit_connection_complete(conn);
566             }
567             //    otherwise, just ignore
568             return 0; // don't sent packet to controller
569 
570         } else{
571             conn = create_connection_for_addr(addr);
572             if (conn){
573                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
574                 conn->state = SENT_CREATE_CONNECTION;
575             }
576         }
577     }
578 
579     // accept connection
580 
581     // reject connection
582 
583     // close_connection?
584       // set state = SENT_DISCONNECT
585 
586     hci_stack.num_cmd_packets--;
587     return hci_stack.hci_transport->send_cmd_packet(packet, size);
588 }
589 
590 /**
591  * pre: numcmds >= 0 - it's allowed to send a command to the controller
592  */
593 int hci_send_cmd(hci_cmd_t *cmd, ...){
594     va_list argptr;
595     va_start(argptr, cmd);
596     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
597     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
598     va_end(argptr);
599     return hci_send_cmd_packet(hci_cmd_buffer, size);
600 }
601 
602 // Create various non-HCI events.
603 // TODO: generalize, use table similar to hci_create_command
604 
605 void hci_emit_state(){
606     uint8_t len = 3;
607     uint8_t event[len];
608     event[0] = BTSTACK_EVENT_STATE;
609     event[1] = len - 3;
610     event[2] = hci_stack.state;
611     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
612     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
613 }
614 
615 void hci_emit_connection_complete(hci_connection_t *conn){
616     uint8_t len = 13;
617     uint8_t event[len];
618     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
619     event[1] = len - 3;
620     event[2] = 0; // status = OK
621     bt_store_16(event, 3, conn->con_handle);
622     bt_flip_addr(&event[5], conn->address);
623     event[11] = 1; // ACL connection
624     event[12] = 0; // encryption disabled
625     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
626     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
627 }
628 
629 void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
630     uint8_t len = 4;
631     uint8_t event[len];
632     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
633     event[1] = len - 2;
634     bt_store_16(event, 2, conn->con_handle);
635     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
636     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
637 }
638 
639 void hci_emit_nr_connections_changed(){
640     uint8_t len = 3;
641     uint8_t event[len];
642     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
643     event[1] = len - 2;
644     event[2] = nr_hci_connections();
645     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
646     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
647 }
648 
649 void hci_emit_hci_open_failed(){
650     uint8_t len = 2;
651     uint8_t event[len];
652     event[0] = BTSTACK_EVENT_POWERON_FAILED;
653     event[1] = len - 2;
654     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
655     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
656 }
657 
658 
659 void hci_emit_btstack_version() {
660     uint8_t len = 6;
661     uint8_t event[len];
662     event[0] = BTSTACK_EVENT_VERSION;
663     event[1] = len - 2;
664     event[len++] = BTSTACK_MAJOR;
665     event[len++] = BTSTACK_MINOR;
666     bt_store_16(event, len, BTSTACK_REVISION);
667     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
668     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
669 }
670 
671 void hci_emit_system_bluetooth_enabled(uint8_t enabled){
672     uint8_t len = 3;
673     uint8_t event[len];
674     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
675     event[1] = len - 2;
676     event[2] = enabled;
677     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
678     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
679 }
680