xref: /btstack/src/hci.c (revision 169f8b289d38a277601a09d9fe9698c85a297ac1)
11f504dbdSmatthias.ringwald /*
21713bceaSmatthias.ringwald  * Copyright (C) 2009 by Matthias Ringwald
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
161713bceaSmatthias.ringwald  *
171713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
181713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
211713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
221713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
231713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
241713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
251713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
271713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281713bceaSmatthias.ringwald  * SUCH DAMAGE.
291713bceaSmatthias.ringwald  *
301713bceaSmatthias.ringwald  */
311713bceaSmatthias.ringwald 
321713bceaSmatthias.ringwald /*
331f504dbdSmatthias.ringwald  *  hci.c
341f504dbdSmatthias.ringwald  *
351f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
361f504dbdSmatthias.ringwald  *
371f504dbdSmatthias.ringwald  */
381f504dbdSmatthias.ringwald 
39475c8125Smatthias.ringwald #include <unistd.h>
4093b8dc03Smatthias.ringwald #include <stdarg.h>
4193b8dc03Smatthias.ringwald #include <string.h>
4256fe0872Smatthias.ringwald #include <stdio.h>
431f504dbdSmatthias.ringwald #include "hci.h"
44d8905019Smatthias.ringwald #include "hci_dump.h"
4593b8dc03Smatthias.ringwald 
462ed6235cSmatthias.ringwald #include "../include/btstack/hci_cmds.h"
472ed6235cSmatthias.ringwald #include "../include/btstack/version.h"
481b0e3922Smatthias.ringwald 
491e6aba47Smatthias.ringwald // temp
501e6aba47Smatthias.ringwald #include "l2cap.h"
511e6aba47Smatthias.ringwald 
52*169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
53ee091cf1Smatthias.ringwald 
5406b35ec0Smatthias.ringwald // the STACK is here
5516833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
5616833f0aSmatthias.ringwald 
5797addcc5Smatthias.ringwald /**
58ee091cf1Smatthias.ringwald  * get connection for a given handle
59ee091cf1Smatthias.ringwald  *
60ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
61ee091cf1Smatthias.ringwald  */
62645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
63ee091cf1Smatthias.ringwald     linked_item_t *it;
64ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
65ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
66ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
67ee091cf1Smatthias.ringwald         }
68ee091cf1Smatthias.ringwald     }
69ee091cf1Smatthias.ringwald     return NULL;
70ee091cf1Smatthias.ringwald }
71ee091cf1Smatthias.ringwald 
72981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
73ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
74ee091cf1Smatthias.ringwald     struct timeval tv;
75ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
76c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
77ee091cf1Smatthias.ringwald         // connections might be timed out
78ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
79c21e6239Smatthias.ringwald         run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
80ee091cf1Smatthias.ringwald     } else {
81ee091cf1Smatthias.ringwald         // next timeout check at
82c21e6239Smatthias.ringwald         timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
83ee091cf1Smatthias.ringwald     }
84ee091cf1Smatthias.ringwald     run_loop_add_timer(timer);
85ee091cf1Smatthias.ringwald }
86ee091cf1Smatthias.ringwald 
87ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
88ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
89ee091cf1Smatthias.ringwald }
90ee091cf1Smatthias.ringwald 
91ee091cf1Smatthias.ringwald static void hci_connection_update_timestamp_for_acl(uint8_t *packet) {
92ee091cf1Smatthias.ringwald     // update timestamp
93ee091cf1Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
94ee091cf1Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
95ee091cf1Smatthias.ringwald     if (connection) hci_connection_timestamp(connection);
96ee091cf1Smatthias.ringwald }
97ee091cf1Smatthias.ringwald 
98ee091cf1Smatthias.ringwald /**
99c8e4258aSmatthias.ringwald  * create connection for given address
100c8e4258aSmatthias.ringwald  *
101c8e4258aSmatthias.ringwald  * @return connection OR NULL, if not found
102c8e4258aSmatthias.ringwald  */
103c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
104c8e4258aSmatthias.ringwald     hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
105c8e4258aSmatthias.ringwald     if (!conn) return NULL;
106c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
107c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
108c8e4258aSmatthias.ringwald     conn->flags = 0;
109ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
110ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
111ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
112c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
113c8e4258aSmatthias.ringwald     return conn;
114c8e4258aSmatthias.ringwald }
115c8e4258aSmatthias.ringwald 
116c8e4258aSmatthias.ringwald /**
11706b35ec0Smatthias.ringwald  * get connection for given address
11897addcc5Smatthias.ringwald  *
11997addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
12097addcc5Smatthias.ringwald  */
121fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
12206b35ec0Smatthias.ringwald     linked_item_t *it;
12306b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
12406b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
12506b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
12606b35ec0Smatthias.ringwald         }
12706b35ec0Smatthias.ringwald     }
12806b35ec0Smatthias.ringwald     return NULL;
12906b35ec0Smatthias.ringwald }
13006b35ec0Smatthias.ringwald 
13143bfb1bdSmatthias.ringwald /**
13243bfb1bdSmatthias.ringwald  * count connections
13343bfb1bdSmatthias.ringwald  */
13443bfb1bdSmatthias.ringwald static int nr_hci_connections(){
13556c253c9Smatthias.ringwald     int count = 0;
13643bfb1bdSmatthias.ringwald     linked_item_t *it;
13743bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
13843bfb1bdSmatthias.ringwald     return count;
13943bfb1bdSmatthias.ringwald }
140c8e4258aSmatthias.ringwald 
14197addcc5Smatthias.ringwald /**
142ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
14316833f0aSmatthias.ringwald  */
1441cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){
14516833f0aSmatthias.ringwald }
14616833f0aSmatthias.ringwald 
147ba681a6cSmatthias.ringwald /**
148ba681a6cSmatthias.ringwald  * Dummy control handler
149ba681a6cSmatthias.ringwald  */
150ba681a6cSmatthias.ringwald static int null_control_function(void *config){
151ba681a6cSmatthias.ringwald     return 0;
152ba681a6cSmatthias.ringwald }
153ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){
154ba681a6cSmatthias.ringwald     return "Hardware unknown";
155ba681a6cSmatthias.ringwald }
156ba681a6cSmatthias.ringwald static bt_control_t null_control = {
157ba681a6cSmatthias.ringwald     null_control_function,
158ba681a6cSmatthias.ringwald     null_control_function,
159ba681a6cSmatthias.ringwald     null_control_function,
160ba681a6cSmatthias.ringwald     null_control_name
161ba681a6cSmatthias.ringwald };
162ba681a6cSmatthias.ringwald 
163c8e4258aSmatthias.ringwald 
164ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
165ee091cf1Smatthias.ringwald     hci_connection_update_timestamp_for_acl(packet);
166ee091cf1Smatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
167ee091cf1Smatthias.ringwald }
168ee091cf1Smatthias.ringwald 
16916833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
170ee091cf1Smatthias.ringwald     hci_connection_update_timestamp_for_acl(packet);
17116833f0aSmatthias.ringwald     hci_stack.acl_packet_handler(packet, size);
17294ab26f8Smatthias.ringwald 
17394ab26f8Smatthias.ringwald     // execute main loop
17494ab26f8Smatthias.ringwald     hci_run();
17516833f0aSmatthias.ringwald }
17622909952Smatthias.ringwald 
17716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1781281a47eSmatthias.ringwald     bd_addr_t addr;
179fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
1801f7b95a1Smatthias.ringwald     hci_connection_t * conn;
18122909952Smatthias.ringwald 
1826772a24cSmatthias.ringwald     switch (packet[0]) {
18322909952Smatthias.ringwald 
1846772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1856772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
1866772a24cSmatthias.ringwald             // Get Num_HCI_Command_Packets
1876772a24cSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
1886772a24cSmatthias.ringwald             break;
1896772a24cSmatthias.ringwald 
1901f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
19137eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
19237eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
19337eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
19437eaa4cfSmatthias.ringwald             printf("Connection_incoming: "); print_bd_addr(addr); printf(", type %u\n", link_type);
19537eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
1961f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
1971f7b95a1Smatthias.ringwald                 if (!conn) {
1981f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
1991f7b95a1Smatthias.ringwald                 }
2001f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
2011f7b95a1Smatthias.ringwald                 conn->state = ACCEPTED_CONNECTION_REQUEST;
20237eaa4cfSmatthias.ringwald                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
20337eaa4cfSmatthias.ringwald             } else {
20437eaa4cfSmatthias.ringwald                 // TODO: decline request
20537eaa4cfSmatthias.ringwald             }
2061f7b95a1Smatthias.ringwald             break;
2071f7b95a1Smatthias.ringwald 
2086772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
209fe1ed1b8Smatthias.ringwald             // Connection management
210fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
211b448a0e7Smatthias.ringwald             printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n");
2121f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
213fe1ed1b8Smatthias.ringwald             if (conn) {
214b448a0e7Smatthias.ringwald                 if (!packet[2]){
215c8e4258aSmatthias.ringwald                     conn->state = OPEN;
216fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
217fe1ed1b8Smatthias.ringwald                     conn->flags = 0;
218ee091cf1Smatthias.ringwald 
219ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
220c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
221ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
222ee091cf1Smatthias.ringwald 
223fe1ed1b8Smatthias.ringwald                     printf("New connection: handle %u, ", conn->con_handle);
224fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
225fe1ed1b8Smatthias.ringwald                     printf("\n");
22643bfb1bdSmatthias.ringwald 
22743bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
228b448a0e7Smatthias.ringwald                 } else {
229b448a0e7Smatthias.ringwald                     // connection failed, remove entry
230b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
231b448a0e7Smatthias.ringwald                     free( conn );
232fe1ed1b8Smatthias.ringwald                 }
233fe1ed1b8Smatthias.ringwald             }
2346772a24cSmatthias.ringwald             break;
235fe1ed1b8Smatthias.ringwald 
2366772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
237fe1ed1b8Smatthias.ringwald             if (!packet[2]){
238fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
239fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
240fe1ed1b8Smatthias.ringwald                 if (conn) {
241fe1ed1b8Smatthias.ringwald                     printf("Connection closed: handle %u, ", conn->con_handle);
242fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
243fe1ed1b8Smatthias.ringwald                     printf("\n");
244ee091cf1Smatthias.ringwald                     run_loop_remove_timer(&conn->timeout);
245fe1ed1b8Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
246fe1ed1b8Smatthias.ringwald                     free( conn );
24743bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
248fe1ed1b8Smatthias.ringwald                 }
249fe1ed1b8Smatthias.ringwald             }
2506772a24cSmatthias.ringwald             break;
2516772a24cSmatthias.ringwald 
2526772a24cSmatthias.ringwald         default:
2536772a24cSmatthias.ringwald             break;
254fe1ed1b8Smatthias.ringwald     }
255fe1ed1b8Smatthias.ringwald 
2563429f56bSmatthias.ringwald     // handle BT initialization
2573429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
2587301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
2597301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
2607301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
2617301ad89Smatthias.ringwald         // }
2627301ad89Smatthias.ringwald         // handle normal init sequence
2633429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
2643429f56bSmatthias.ringwald             // odd: waiting for event
2653429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
2663429f56bSmatthias.ringwald                 hci_stack.substate++;
2673429f56bSmatthias.ringwald             }
2683429f56bSmatthias.ringwald         }
26922909952Smatthias.ringwald     }
27022909952Smatthias.ringwald 
27116833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
27294ab26f8Smatthias.ringwald 
27394ab26f8Smatthias.ringwald 	// execute main loop
27494ab26f8Smatthias.ringwald 	hci_run();
27516833f0aSmatthias.ringwald }
27616833f0aSmatthias.ringwald 
277fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
2781cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
27916833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
28016833f0aSmatthias.ringwald }
2811cd208adSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
28216833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
28316833f0aSmatthias.ringwald }
28416833f0aSmatthias.ringwald 
28511e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
286475c8125Smatthias.ringwald 
287475c8125Smatthias.ringwald     // reference to use transport layer implementation
28816833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
289475c8125Smatthias.ringwald 
29011e23e5fSmatthias.ringwald     // references to used control implementation
2917301ad89Smatthias.ringwald     if (control) {
29211e23e5fSmatthias.ringwald         hci_stack.control = control;
2937301ad89Smatthias.ringwald     } else {
2947301ad89Smatthias.ringwald         hci_stack.control = &null_control;
2957301ad89Smatthias.ringwald     }
29611e23e5fSmatthias.ringwald 
29711e23e5fSmatthias.ringwald     // reference to used config
29811e23e5fSmatthias.ringwald     hci_stack.config = config;
29911e23e5fSmatthias.ringwald 
300fe1ed1b8Smatthias.ringwald     // no connections yet
301fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
302fe1ed1b8Smatthias.ringwald 
30302ea9861Smatthias.ringwald     // empty cmd buffer
30416833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
30516833f0aSmatthias.ringwald 
30616833f0aSmatthias.ringwald     // higher level handler
30716833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
30816833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
30916833f0aSmatthias.ringwald 
31016833f0aSmatthias.ringwald     // register packet handlers with transport
31116833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
31216833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
313475c8125Smatthias.ringwald }
314475c8125Smatthias.ringwald 
315475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
316f12adbd6Smatthias.ringwald     if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
3177301ad89Smatthias.ringwald 
318038bc64cSmatthias.ringwald         // power on
319038bc64cSmatthias.ringwald         int err = hci_stack.control->on(hci_stack.config);
320038bc64cSmatthias.ringwald         if (err){
321ac2e07f8Smatthias.ringwald             fprintf(stderr, "POWER_ON failed\n");
322038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
323038bc64cSmatthias.ringwald             return err;
324038bc64cSmatthias.ringwald         }
325038bc64cSmatthias.ringwald 
326038bc64cSmatthias.ringwald         // open low-level device
327038bc64cSmatthias.ringwald         err = hci_stack.hci_transport->open(hci_stack.config);
328038bc64cSmatthias.ringwald         if (err){
329ac2e07f8Smatthias.ringwald             fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n");
330038bc64cSmatthias.ringwald             hci_stack.control->off(hci_stack.config);
331038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
332038bc64cSmatthias.ringwald             return err;
333038bc64cSmatthias.ringwald         }
334038bc64cSmatthias.ringwald 
3357301ad89Smatthias.ringwald         // set up state machine
3367301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
3377301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
3387301ad89Smatthias.ringwald         hci_stack.substate = 0;
3397301ad89Smatthias.ringwald 
340f12adbd6Smatthias.ringwald     } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
3417301ad89Smatthias.ringwald 
3427301ad89Smatthias.ringwald         // close low-level device
3437301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
3447301ad89Smatthias.ringwald 
3457301ad89Smatthias.ringwald         // power off
34611e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
34743bfb1bdSmatthias.ringwald 
34843bfb1bdSmatthias.ringwald         // we're off now
34943bfb1bdSmatthias.ringwald         hci_stack.state = HCI_STATE_OFF;
35011e23e5fSmatthias.ringwald     }
35168d92d03Smatthias.ringwald 
352038bc64cSmatthias.ringwald     // create internal event
353ee8bf225Smatthias.ringwald 	hci_emit_state();
354ee8bf225Smatthias.ringwald 
35568d92d03Smatthias.ringwald 	// trigger next/first action
35668d92d03Smatthias.ringwald 	hci_run();
35768d92d03Smatthias.ringwald 
358475c8125Smatthias.ringwald     return 0;
359475c8125Smatthias.ringwald }
360475c8125Smatthias.ringwald 
36106b35ec0Smatthias.ringwald void hci_run(){
3623429f56bSmatthias.ringwald     switch (hci_stack.state){
3633429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
3643429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
3653429f56bSmatthias.ringwald                 // odd: waiting for command completion
36606b35ec0Smatthias.ringwald                 return;
3673429f56bSmatthias.ringwald             }
3683429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
3693429f56bSmatthias.ringwald                 // cannot send command yet
37006b35ec0Smatthias.ringwald                 return;
3713429f56bSmatthias.ringwald             }
3723429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
3733429f56bSmatthias.ringwald                 case 0:
37422909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
3753429f56bSmatthias.ringwald                     break;
3763429f56bSmatthias.ringwald 				case 1:
377f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
378f432a6ddSmatthias.ringwald 					break;
379f432a6ddSmatthias.ringwald                 case 2:
3803429f56bSmatthias.ringwald                     // ca. 15 sec
3813429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
3823429f56bSmatthias.ringwald                     break;
383f432a6ddSmatthias.ringwald 				case 3:
384bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
385f432a6ddSmatthias.ringwald 					break;
386f432a6ddSmatthias.ringwald                 case 4:
3873429f56bSmatthias.ringwald                     // done.
3883429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
389b360b6adSmatthias.ringwald                     hci_emit_state();
3903429f56bSmatthias.ringwald                     break;
3913429f56bSmatthias.ringwald                 default:
3923429f56bSmatthias.ringwald                     break;
393475c8125Smatthias.ringwald             }
3943429f56bSmatthias.ringwald             hci_stack.substate++;
3953429f56bSmatthias.ringwald             break;
3963429f56bSmatthias.ringwald         default:
3973429f56bSmatthias.ringwald             break;
3981f504dbdSmatthias.ringwald     }
3993429f56bSmatthias.ringwald }
40016833f0aSmatthias.ringwald 
40131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
402c8e4258aSmatthias.ringwald     bd_addr_t addr;
403c8e4258aSmatthias.ringwald     hci_connection_t * conn;
404c8e4258aSmatthias.ringwald     // house-keeping
405c8e4258aSmatthias.ringwald 
406c8e4258aSmatthias.ringwald     // create_connection?
407c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
408c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
409c8e4258aSmatthias.ringwald         printf("Create_connection to "); print_bd_addr(addr); printf("\n");
410c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
411c8e4258aSmatthias.ringwald         if (conn) {
412c8e4258aSmatthias.ringwald             // if connection exists
413c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
414c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
415c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
416c8e4258aSmatthias.ringwald             }
417c8e4258aSmatthias.ringwald             //    otherwise, just ignore
418c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
419c8e4258aSmatthias.ringwald 
420c8e4258aSmatthias.ringwald         } else{
421c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
422c8e4258aSmatthias.ringwald             if (conn){
423c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
424c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
425c8e4258aSmatthias.ringwald             }
426c8e4258aSmatthias.ringwald         }
427c8e4258aSmatthias.ringwald     }
428c8e4258aSmatthias.ringwald 
429c8e4258aSmatthias.ringwald     // accept connection
430c8e4258aSmatthias.ringwald 
431c8e4258aSmatthias.ringwald     // reject connection
432c8e4258aSmatthias.ringwald 
433c8e4258aSmatthias.ringwald     // close_connection?
434c8e4258aSmatthias.ringwald       // set state = SENT_DISCONNECT
435c8e4258aSmatthias.ringwald 
43631452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
43731452debSmatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(packet, size);
43831452debSmatthias.ringwald }
4398adf0ddaSmatthias.ringwald 
4401cd208adSmatthias.ringwald /**
4411cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
4421cd208adSmatthias.ringwald  */
4431cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
4441cd208adSmatthias.ringwald     va_list argptr;
4451cd208adSmatthias.ringwald     va_start(argptr, cmd);
4461cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
4471cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
4481cd208adSmatthias.ringwald     va_end(argptr);
4491cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
45093b8dc03Smatthias.ringwald }
451c8e4258aSmatthias.ringwald 
452ee091cf1Smatthias.ringwald // Create various non-HCI events.
453ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
454ee091cf1Smatthias.ringwald 
455c8e4258aSmatthias.ringwald void hci_emit_state(){
456c8e4258aSmatthias.ringwald     uint8_t len = 3;
457c8e4258aSmatthias.ringwald     uint8_t event[len];
45880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
4591b0e3922Smatthias.ringwald     event[1] = len - 3;
460c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
461c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
462c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
463c8e4258aSmatthias.ringwald }
464c8e4258aSmatthias.ringwald 
465c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
466c8e4258aSmatthias.ringwald     uint8_t len = 13;
467c8e4258aSmatthias.ringwald     uint8_t event[len];
468c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
4691b0e3922Smatthias.ringwald     event[1] = len - 3;
470c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
471c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
472c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
473c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
474c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
475c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
476c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
477c8e4258aSmatthias.ringwald }
478c8e4258aSmatthias.ringwald 
479ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
480ee091cf1Smatthias.ringwald     uint8_t len = 4;
481ee091cf1Smatthias.ringwald     uint8_t event[len];
48280d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
4831b0e3922Smatthias.ringwald     event[1] = len - 2;
484ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
485ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
486ee091cf1Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
487ee091cf1Smatthias.ringwald }
48843bfb1bdSmatthias.ringwald 
48943bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
49043bfb1bdSmatthias.ringwald     uint8_t len = 3;
49143bfb1bdSmatthias.ringwald     uint8_t event[len];
49280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
4931b0e3922Smatthias.ringwald     event[1] = len - 2;
49443bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
49543bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
49643bfb1bdSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
49743bfb1bdSmatthias.ringwald }
498038bc64cSmatthias.ringwald 
499038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
5001b0e3922Smatthias.ringwald     uint8_t len = 2;
501038bc64cSmatthias.ringwald     uint8_t event[len];
50280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
5031b0e3922Smatthias.ringwald     event[1] = len - 2;
504038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
505038bc64cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
506038bc64cSmatthias.ringwald }
5071b0e3922Smatthias.ringwald 
5081b0e3922Smatthias.ringwald 
5091b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
5101b0e3922Smatthias.ringwald     uint8_t len = 6;
5111b0e3922Smatthias.ringwald     uint8_t event[len];
5121b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
5131b0e3922Smatthias.ringwald     event[1] = len - 2;
5141b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
5151b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
5161b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
5171b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
5181b0e3922Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
5191b0e3922Smatthias.ringwald }
5201b0e3922Smatthias.ringwald 
5212ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
5222ed6235cSmatthias.ringwald     uint8_t len = 3;
5232ed6235cSmatthias.ringwald     uint8_t event[len];
5242ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
5252ed6235cSmatthias.ringwald     event[1] = len - 3;
5262ed6235cSmatthias.ringwald     event[2] = enabled;
5272ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
5282ed6235cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
5292ed6235cSmatthias.ringwald }
530