xref: /btstack/src/hci.c (revision f9a30166ed546697680011978bae92390d0e37db)
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 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
147     hci_connection_t * connection = connection_for_handle(handle);
148     if (!connection) {
149         log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
150         return 0;
151     }
152     return connection->num_acl_packets_sent;
153 }
154 
155 uint8_t hci_number_free_acl_slots(){
156     uint8_t free_slots = hci_stack.total_num_acl_packets;
157     linked_item_t *it;
158     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
159         hci_connection_t * connection = (hci_connection_t *) it;
160         if (free_slots < connection->num_acl_packets_sent) {
161             log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
162             return 0;
163         }
164         free_slots -= connection->num_acl_packets_sent;
165     }
166     return free_slots;
167 }
168 
169 int hci_ready_to_send(hci_con_handle_t handle){
170     return hci_number_free_acl_slots() && hci_number_outgoing_packets(handle) < 2;
171 }
172 
173 int hci_send_acl_packet(uint8_t *packet, int size){
174 
175     // check for free places on BT module
176     if (!hci_number_free_acl_slots()) return -1;
177 
178     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
179     hci_connection_t *connection = connection_for_handle( con_handle);
180     if (!connection) return 0;
181     hci_connection_timestamp(connection);
182 
183     // count packet
184     connection->num_acl_packets_sent++;
185     // log_dbg("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
186 
187     // send packet - ignore errors
188     hci_stack.hci_transport->send_acl_packet(packet, size);
189 
190     return 0;
191 }
192 
193 static void acl_handler(uint8_t *packet, int size){
194 
195     // get info
196     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
197     hci_connection_t *conn      = connection_for_handle(con_handle);
198     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
199     uint16_t acl_length         = READ_ACL_LENGTH(packet);
200 
201     // ignore non-registered handle
202     if (!conn){
203         log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
204         return;
205     }
206 
207     // update idle timestamp
208     hci_connection_timestamp(conn);
209 
210     // handle different packet types
211     switch (acl_flags & 0x03) {
212 
213         case 0x01: // continuation fragment
214 
215             // sanity check
216             if (conn->acl_recombination_pos == 0) {
217                 log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
218                 return;
219             }
220 
221             // append fragment payload (header already stored)
222             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
223             conn->acl_recombination_pos += acl_length;
224 
225             // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n",
226             //        acl_length, connection->acl_recombination_pos, connection->acl_recombination_length);
227 
228             // forward complete L2CAP packet if complete.
229             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
230 
231                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
232                 // reset recombination buffer
233                 conn->acl_recombination_length = 0;
234                 conn->acl_recombination_pos = 0;
235             }
236             break;
237 
238         case 0x02: { // first fragment
239 
240             // sanity check
241             if (conn->acl_recombination_pos) {
242                 log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
243                 return;
244             }
245 
246             // peek into L2CAP packet!
247             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
248 
249             // compare fragment size to L2CAP packet size
250             if (acl_length >= l2cap_length + 4){
251 
252                 // forward fragment as L2CAP packet
253                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
254 
255             } else {
256                 // store first fragment and tweak acl length for complete package
257                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
258                 conn->acl_recombination_pos    = acl_length + 4;
259                 conn->acl_recombination_length = l2cap_length;
260                 bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4);
261                 // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
262             }
263             break;
264 
265         }
266         default:
267             log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
268             return;
269     }
270 
271     // execute main loop
272     hci_run();
273 }
274 
275 static void event_handler(uint8_t *packet, int size){
276     bd_addr_t addr;
277     hci_con_handle_t handle;
278     hci_connection_t * conn;
279     int i;
280 
281     // get num_cmd_packets
282     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
283         // Get Num_HCI_Command_Packets
284         hci_stack.num_cmd_packets = packet[2];
285     }
286 
287     switch (packet[0]) {
288 
289         case HCI_EVENT_COMMAND_COMPLETE:
290             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
291                 // from offset 5
292                 // status
293                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
294                 // ignore: SCO data packet len (8)
295                 hci_stack.total_num_acl_packets  = packet[9];
296                 // ignore: total num SCO packets
297                 if (hci_stack.state == HCI_STATE_INITIALIZING){
298                     log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
299                 }
300             }
301             break;
302 
303         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
304             for (i=0; i<packet[2];i++){
305                 handle = READ_BT_16(packet, 3 + 2*i);
306                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
307                 conn = connection_for_handle(handle);
308                 if (!conn){
309                     log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
310                     continue;
311                 }
312                 conn->num_acl_packets_sent -= num_packets;
313                 // log_dbg("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
314             }
315             break;
316 
317         case HCI_EVENT_CONNECTION_REQUEST:
318             bt_flip_addr(addr, &packet[2]);
319             // TODO: eval COD 8-10
320             uint8_t link_type = packet[11];
321             log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type);
322             if (link_type == 1) { // ACL
323                 conn = connection_for_address(addr);
324                 if (!conn) {
325                     conn = create_connection_for_addr(addr);
326                 }
327                 // TODO: check for malloc failure
328                 conn->state = ACCEPTED_CONNECTION_REQUEST;
329                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
330             } else {
331                 // TODO: decline request
332             }
333             break;
334 
335         case HCI_EVENT_CONNECTION_COMPLETE:
336             // Connection management
337             bt_flip_addr(addr, &packet[5]);
338             log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n");
339             conn = connection_for_address(addr);
340             if (conn) {
341                 if (!packet[2]){
342                     conn->state = OPEN;
343                     conn->con_handle = READ_BT_16(packet, 3);
344                     conn->flags = 0;
345 
346                     gettimeofday(&conn->timestamp, NULL);
347                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
348                     run_loop_add_timer(&conn->timeout);
349 
350                     log_dbg("New connection: handle %u, ", conn->con_handle);
351                     print_bd_addr( conn->address );
352                     log_dbg("\n");
353 
354                     hci_emit_nr_connections_changed();
355                 } else {
356                     // connection failed, remove entry
357                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
358                     free( conn );
359                 }
360             }
361             break;
362 
363         case HCI_EVENT_DISCONNECTION_COMPLETE:
364             if (!packet[2]){
365                 handle = READ_BT_16(packet, 3);
366                 hci_connection_t * conn = connection_for_handle(handle);
367                 if (conn) {
368                     log_dbg("Connection closed: handle %u, ", conn->con_handle);
369                     print_bd_addr( conn->address );
370                     log_dbg("\n");
371                     run_loop_remove_timer(&conn->timeout);
372                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
373                     free( conn );
374                     hci_emit_nr_connections_changed();
375                 }
376             }
377             break;
378 
379         default:
380             break;
381     }
382 
383     // handle BT initialization
384     if (hci_stack.state == HCI_STATE_INITIALIZING){
385         // handle H4 synchronization loss on restart
386         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
387         //    hci_stack.substate = 0;
388         // }
389         // handle normal init sequence
390         if (hci_stack.substate % 2){
391             // odd: waiting for event
392             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
393                 hci_stack.substate++;
394             }
395         }
396     }
397 
398     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
399 
400 	// execute main loop
401 	hci_run();
402 }
403 
404 void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
405     switch (packet_type) {
406         case HCI_EVENT_PACKET:
407             event_handler(packet, size);
408             break;
409         case HCI_ACL_DATA_PACKET:
410             acl_handler(packet, size);
411             break;
412         default:
413             break;
414     }
415 }
416 
417 /** Register HCI packet handlers */
418 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
419     hci_stack.packet_handler = handler;
420 }
421 
422 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
423 
424     // reference to use transport layer implementation
425     hci_stack.hci_transport = transport;
426 
427     // references to used control implementation
428     hci_stack.control = control;
429 
430     // reference to used config
431     hci_stack.config = config;
432 
433     // no connections yet
434     hci_stack.connections = NULL;
435 
436     // empty cmd buffer
437     hci_stack.hci_cmd_buffer = malloc(3+255);
438 
439     // higher level handler
440     hci_stack.packet_handler = dummy_handler;
441 
442     // register packet handlers with transport
443     transport->register_packet_handler(&packet_handler);
444 }
445 
446 int hci_power_control(HCI_POWER_MODE power_mode){
447     if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
448 
449         // power on
450         int err = 0;
451         if (hci_stack.control && hci_stack.control->on){
452             err = (*hci_stack.control->on)(hci_stack.config);
453         }
454         if (err){
455             log_err( "POWER_ON failed\n");
456             hci_emit_hci_open_failed();
457             return err;
458         }
459 
460         // open low-level device
461         err = hci_stack.hci_transport->open(hci_stack.config);
462         if (err){
463             log_err( "HCI_INIT failed, turning Bluetooth off again\n");
464             if (hci_stack.control && hci_stack.control->off){
465                 (*hci_stack.control->off)(hci_stack.config);
466             }
467             hci_emit_hci_open_failed();
468             return err;
469         }
470 
471         // set up state machine
472         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
473         hci_stack.state = HCI_STATE_INITIALIZING;
474         hci_stack.substate = 0;
475 
476     } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
477 
478         // close low-level device
479         hci_stack.hci_transport->close(hci_stack.config);
480 
481         // power off
482         if (hci_stack.control && hci_stack.control->off){
483             (*hci_stack.control->off)(hci_stack.config);
484         }
485 
486         // we're off now
487         hci_stack.state = HCI_STATE_OFF;
488     }
489 
490     // create internal event
491 	hci_emit_state();
492 
493 	// trigger next/first action
494 	hci_run();
495 
496     return 0;
497 }
498 
499 void hci_run(){
500     switch (hci_stack.state){
501         case HCI_STATE_INITIALIZING:
502             if (hci_stack.substate % 2) {
503                 // odd: waiting for command completion
504                 return;
505             }
506             if (hci_stack.num_cmd_packets == 0) {
507                 // cannot send command yet
508                 return;
509             }
510             switch (hci_stack.substate >> 1){
511                 case 0:
512                     hci_send_cmd(&hci_reset);
513                     break;
514                 case 1:
515                     // custom initialization
516                     if (hci_stack.control && hci_stack.control->next_command){
517                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
518                         if (cmd) {
519                             int size = 3 + cmd[2];
520                             hci_stack.hci_transport->send_cmd_packet(cmd, size);
521                             break;
522                         }
523                     }
524                     // @NOTE fall through, if no command to send anymore
525                     hci_stack.substate += 2;
526 				case 2:
527 					hci_send_cmd(&hci_read_bd_addr);
528 					break;
529 				case 3:
530 					hci_send_cmd(&hci_read_buffer_size);
531 					break;
532                 case 4:
533                     // ca. 15 sec
534                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
535                     break;
536 				case 5:
537 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
538 					break;
539                 case 6:
540                     // done.
541                     hci_stack.state = HCI_STATE_WORKING;
542                     hci_emit_state();
543                     break;
544                 default:
545                     break;
546             }
547             hci_stack.substate++;
548             break;
549         default:
550             break;
551     }
552 }
553 
554 int hci_send_cmd_packet(uint8_t *packet, int size){
555     bd_addr_t addr;
556     hci_connection_t * conn;
557     // house-keeping
558 
559     // create_connection?
560     if (IS_COMMAND(packet, hci_create_connection)){
561         bt_flip_addr(addr, &packet[3]);
562         log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n");
563         conn = connection_for_address(addr);
564         if (conn) {
565             // if connection exists
566             if (conn->state == OPEN) {
567                 // if OPEN, emit connection complete command
568                 hci_emit_connection_complete(conn);
569             }
570             //    otherwise, just ignore
571             return 0; // don't sent packet to controller
572 
573         } else{
574             conn = create_connection_for_addr(addr);
575             if (conn){
576                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
577                 conn->state = SENT_CREATE_CONNECTION;
578             }
579         }
580     }
581 
582     // accept connection
583 
584     // reject connection
585 
586     // close_connection?
587       // set state = SENT_DISCONNECT
588 
589     hci_stack.num_cmd_packets--;
590     return hci_stack.hci_transport->send_cmd_packet(packet, size);
591 }
592 
593 /**
594  * pre: numcmds >= 0 - it's allowed to send a command to the controller
595  */
596 int hci_send_cmd(hci_cmd_t *cmd, ...){
597     va_list argptr;
598     va_start(argptr, cmd);
599     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
600     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
601     va_end(argptr);
602     return hci_send_cmd_packet(hci_cmd_buffer, size);
603 }
604 
605 // Create various non-HCI events.
606 // TODO: generalize, use table similar to hci_create_command
607 
608 void hci_emit_state(){
609     uint8_t len = 3;
610     uint8_t event[len];
611     event[0] = BTSTACK_EVENT_STATE;
612     event[1] = len - 3;
613     event[2] = hci_stack.state;
614     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
615     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
616 }
617 
618 void hci_emit_connection_complete(hci_connection_t *conn){
619     uint8_t len = 13;
620     uint8_t event[len];
621     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
622     event[1] = len - 3;
623     event[2] = 0; // status = OK
624     bt_store_16(event, 3, conn->con_handle);
625     bt_flip_addr(&event[5], conn->address);
626     event[11] = 1; // ACL connection
627     event[12] = 0; // encryption disabled
628     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
629     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
630 }
631 
632 void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
633     uint8_t len = 4;
634     uint8_t event[len];
635     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
636     event[1] = len - 2;
637     bt_store_16(event, 2, conn->con_handle);
638     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
639     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
640 }
641 
642 void hci_emit_nr_connections_changed(){
643     uint8_t len = 3;
644     uint8_t event[len];
645     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
646     event[1] = len - 2;
647     event[2] = nr_hci_connections();
648     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
649     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
650 }
651 
652 void hci_emit_hci_open_failed(){
653     uint8_t len = 2;
654     uint8_t event[len];
655     event[0] = BTSTACK_EVENT_POWERON_FAILED;
656     event[1] = len - 2;
657     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
658     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
659 }
660 
661 
662 void hci_emit_btstack_version() {
663     uint8_t len = 6;
664     uint8_t event[len];
665     event[0] = BTSTACK_EVENT_VERSION;
666     event[1] = len - 2;
667     event[len++] = BTSTACK_MAJOR;
668     event[len++] = BTSTACK_MINOR;
669     bt_store_16(event, len, BTSTACK_REVISION);
670     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
671     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
672 }
673 
674 void hci_emit_system_bluetooth_enabled(uint8_t enabled){
675     uint8_t len = 3;
676     uint8_t event[len];
677     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
678     event[1] = len - 2;
679     event[2] = enabled;
680     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
681     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
682 }
683