xref: /btstack/src/hci.c (revision d55db49ea217780f20828e0b4a3d0074d6111b4e)
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 
52169f8b28Smatthias.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);
112*d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
113c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
114c8e4258aSmatthias.ringwald     return conn;
115c8e4258aSmatthias.ringwald }
116c8e4258aSmatthias.ringwald 
117c8e4258aSmatthias.ringwald /**
11806b35ec0Smatthias.ringwald  * get connection for given address
11997addcc5Smatthias.ringwald  *
12097addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
12197addcc5Smatthias.ringwald  */
122fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
12306b35ec0Smatthias.ringwald     linked_item_t *it;
12406b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
12506b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
12606b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
12706b35ec0Smatthias.ringwald         }
12806b35ec0Smatthias.ringwald     }
12906b35ec0Smatthias.ringwald     return NULL;
13006b35ec0Smatthias.ringwald }
13106b35ec0Smatthias.ringwald 
13243bfb1bdSmatthias.ringwald /**
13343bfb1bdSmatthias.ringwald  * count connections
13443bfb1bdSmatthias.ringwald  */
13543bfb1bdSmatthias.ringwald static int nr_hci_connections(){
13656c253c9Smatthias.ringwald     int count = 0;
13743bfb1bdSmatthias.ringwald     linked_item_t *it;
13843bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
13943bfb1bdSmatthias.ringwald     return count;
14043bfb1bdSmatthias.ringwald }
141c8e4258aSmatthias.ringwald 
14297addcc5Smatthias.ringwald /**
143ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
14416833f0aSmatthias.ringwald  */
1451cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){
14616833f0aSmatthias.ringwald }
14716833f0aSmatthias.ringwald 
148ba681a6cSmatthias.ringwald /**
149ba681a6cSmatthias.ringwald  * Dummy control handler
150ba681a6cSmatthias.ringwald  */
151ba681a6cSmatthias.ringwald static int null_control_function(void *config){
152ba681a6cSmatthias.ringwald     return 0;
153ba681a6cSmatthias.ringwald }
154ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){
155ba681a6cSmatthias.ringwald     return "Hardware unknown";
156ba681a6cSmatthias.ringwald }
157ba681a6cSmatthias.ringwald static bt_control_t null_control = {
158ba681a6cSmatthias.ringwald     null_control_function,
159ba681a6cSmatthias.ringwald     null_control_function,
160ba681a6cSmatthias.ringwald     null_control_function,
161ba681a6cSmatthias.ringwald     null_control_name
162ba681a6cSmatthias.ringwald };
163ba681a6cSmatthias.ringwald 
164c8e4258aSmatthias.ringwald 
165ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
166ee091cf1Smatthias.ringwald     hci_connection_update_timestamp_for_acl(packet);
167ee091cf1Smatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
168ee091cf1Smatthias.ringwald }
169ee091cf1Smatthias.ringwald 
17016833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
171ee091cf1Smatthias.ringwald     hci_connection_update_timestamp_for_acl(packet);
17216833f0aSmatthias.ringwald     hci_stack.acl_packet_handler(packet, size);
17394ab26f8Smatthias.ringwald 
17494ab26f8Smatthias.ringwald     // execute main loop
17594ab26f8Smatthias.ringwald     hci_run();
17616833f0aSmatthias.ringwald }
17722909952Smatthias.ringwald 
17816833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1791281a47eSmatthias.ringwald     bd_addr_t addr;
180fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
1811f7b95a1Smatthias.ringwald     hci_connection_t * conn;
18222909952Smatthias.ringwald 
1836772a24cSmatthias.ringwald     switch (packet[0]) {
18422909952Smatthias.ringwald 
1856772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1866772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
1876772a24cSmatthias.ringwald             // Get Num_HCI_Command_Packets
1886772a24cSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
1896772a24cSmatthias.ringwald             break;
1906772a24cSmatthias.ringwald 
1911f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
19237eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
19337eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
19437eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
19537eaa4cfSmatthias.ringwald             printf("Connection_incoming: "); print_bd_addr(addr); printf(", type %u\n", link_type);
19637eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
1971f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
1981f7b95a1Smatthias.ringwald                 if (!conn) {
1991f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
2001f7b95a1Smatthias.ringwald                 }
2011f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
2021f7b95a1Smatthias.ringwald                 conn->state = ACCEPTED_CONNECTION_REQUEST;
20337eaa4cfSmatthias.ringwald                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
20437eaa4cfSmatthias.ringwald             } else {
20537eaa4cfSmatthias.ringwald                 // TODO: decline request
20637eaa4cfSmatthias.ringwald             }
2071f7b95a1Smatthias.ringwald             break;
2081f7b95a1Smatthias.ringwald 
2096772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
210fe1ed1b8Smatthias.ringwald             // Connection management
211fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
212b448a0e7Smatthias.ringwald             printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n");
2131f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
214fe1ed1b8Smatthias.ringwald             if (conn) {
215b448a0e7Smatthias.ringwald                 if (!packet[2]){
216c8e4258aSmatthias.ringwald                     conn->state = OPEN;
217fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
218fe1ed1b8Smatthias.ringwald                     conn->flags = 0;
219ee091cf1Smatthias.ringwald 
220ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
221c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
222ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
223ee091cf1Smatthias.ringwald 
224fe1ed1b8Smatthias.ringwald                     printf("New connection: handle %u, ", conn->con_handle);
225fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
226fe1ed1b8Smatthias.ringwald                     printf("\n");
22743bfb1bdSmatthias.ringwald 
22843bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
229b448a0e7Smatthias.ringwald                 } else {
230b448a0e7Smatthias.ringwald                     // connection failed, remove entry
231b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
232b448a0e7Smatthias.ringwald                     free( conn );
233fe1ed1b8Smatthias.ringwald                 }
234fe1ed1b8Smatthias.ringwald             }
2356772a24cSmatthias.ringwald             break;
236fe1ed1b8Smatthias.ringwald 
2376772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
238fe1ed1b8Smatthias.ringwald             if (!packet[2]){
239fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
240fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
241fe1ed1b8Smatthias.ringwald                 if (conn) {
242fe1ed1b8Smatthias.ringwald                     printf("Connection closed: handle %u, ", conn->con_handle);
243fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
244fe1ed1b8Smatthias.ringwald                     printf("\n");
245ee091cf1Smatthias.ringwald                     run_loop_remove_timer(&conn->timeout);
246fe1ed1b8Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
247fe1ed1b8Smatthias.ringwald                     free( conn );
24843bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
249fe1ed1b8Smatthias.ringwald                 }
250fe1ed1b8Smatthias.ringwald             }
2516772a24cSmatthias.ringwald             break;
2526772a24cSmatthias.ringwald 
2536772a24cSmatthias.ringwald         default:
2546772a24cSmatthias.ringwald             break;
255fe1ed1b8Smatthias.ringwald     }
256fe1ed1b8Smatthias.ringwald 
2573429f56bSmatthias.ringwald     // handle BT initialization
2583429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
2597301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
2607301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
2617301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
2627301ad89Smatthias.ringwald         // }
2637301ad89Smatthias.ringwald         // handle normal init sequence
2643429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
2653429f56bSmatthias.ringwald             // odd: waiting for event
2663429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
2673429f56bSmatthias.ringwald                 hci_stack.substate++;
2683429f56bSmatthias.ringwald             }
2693429f56bSmatthias.ringwald         }
27022909952Smatthias.ringwald     }
27122909952Smatthias.ringwald 
27216833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
27394ab26f8Smatthias.ringwald 
27494ab26f8Smatthias.ringwald 	// execute main loop
27594ab26f8Smatthias.ringwald 	hci_run();
27616833f0aSmatthias.ringwald }
27716833f0aSmatthias.ringwald 
278fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
2791cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
28016833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
28116833f0aSmatthias.ringwald }
2821cd208adSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
28316833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
28416833f0aSmatthias.ringwald }
28516833f0aSmatthias.ringwald 
28611e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
287475c8125Smatthias.ringwald 
288475c8125Smatthias.ringwald     // reference to use transport layer implementation
28916833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
290475c8125Smatthias.ringwald 
29111e23e5fSmatthias.ringwald     // references to used control implementation
2927301ad89Smatthias.ringwald     if (control) {
29311e23e5fSmatthias.ringwald         hci_stack.control = control;
2947301ad89Smatthias.ringwald     } else {
2957301ad89Smatthias.ringwald         hci_stack.control = &null_control;
2967301ad89Smatthias.ringwald     }
29711e23e5fSmatthias.ringwald 
29811e23e5fSmatthias.ringwald     // reference to used config
29911e23e5fSmatthias.ringwald     hci_stack.config = config;
30011e23e5fSmatthias.ringwald 
301fe1ed1b8Smatthias.ringwald     // no connections yet
302fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
303fe1ed1b8Smatthias.ringwald 
30402ea9861Smatthias.ringwald     // empty cmd buffer
30516833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
30616833f0aSmatthias.ringwald 
30716833f0aSmatthias.ringwald     // higher level handler
30816833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
30916833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
31016833f0aSmatthias.ringwald 
31116833f0aSmatthias.ringwald     // register packet handlers with transport
31216833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
31316833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
314475c8125Smatthias.ringwald }
315475c8125Smatthias.ringwald 
316475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
317f12adbd6Smatthias.ringwald     if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
3187301ad89Smatthias.ringwald 
319038bc64cSmatthias.ringwald         // power on
320038bc64cSmatthias.ringwald         int err = hci_stack.control->on(hci_stack.config);
321038bc64cSmatthias.ringwald         if (err){
322ac2e07f8Smatthias.ringwald             fprintf(stderr, "POWER_ON failed\n");
323038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
324038bc64cSmatthias.ringwald             return err;
325038bc64cSmatthias.ringwald         }
326038bc64cSmatthias.ringwald 
327038bc64cSmatthias.ringwald         // open low-level device
328038bc64cSmatthias.ringwald         err = hci_stack.hci_transport->open(hci_stack.config);
329038bc64cSmatthias.ringwald         if (err){
330ac2e07f8Smatthias.ringwald             fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n");
331038bc64cSmatthias.ringwald             hci_stack.control->off(hci_stack.config);
332038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
333038bc64cSmatthias.ringwald             return err;
334038bc64cSmatthias.ringwald         }
335038bc64cSmatthias.ringwald 
3367301ad89Smatthias.ringwald         // set up state machine
3377301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
3387301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
3397301ad89Smatthias.ringwald         hci_stack.substate = 0;
3407301ad89Smatthias.ringwald 
341f12adbd6Smatthias.ringwald     } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
3427301ad89Smatthias.ringwald 
3437301ad89Smatthias.ringwald         // close low-level device
3447301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
3457301ad89Smatthias.ringwald 
3467301ad89Smatthias.ringwald         // power off
34711e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
34843bfb1bdSmatthias.ringwald 
34943bfb1bdSmatthias.ringwald         // we're off now
35043bfb1bdSmatthias.ringwald         hci_stack.state = HCI_STATE_OFF;
35111e23e5fSmatthias.ringwald     }
35268d92d03Smatthias.ringwald 
353038bc64cSmatthias.ringwald     // create internal event
354ee8bf225Smatthias.ringwald 	hci_emit_state();
355ee8bf225Smatthias.ringwald 
35668d92d03Smatthias.ringwald 	// trigger next/first action
35768d92d03Smatthias.ringwald 	hci_run();
35868d92d03Smatthias.ringwald 
359475c8125Smatthias.ringwald     return 0;
360475c8125Smatthias.ringwald }
361475c8125Smatthias.ringwald 
36206b35ec0Smatthias.ringwald void hci_run(){
3633429f56bSmatthias.ringwald     switch (hci_stack.state){
3643429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
3653429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
3663429f56bSmatthias.ringwald                 // odd: waiting for command completion
36706b35ec0Smatthias.ringwald                 return;
3683429f56bSmatthias.ringwald             }
3693429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
3703429f56bSmatthias.ringwald                 // cannot send command yet
37106b35ec0Smatthias.ringwald                 return;
3723429f56bSmatthias.ringwald             }
3733429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
3743429f56bSmatthias.ringwald                 case 0:
37522909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
3763429f56bSmatthias.ringwald                     break;
3773429f56bSmatthias.ringwald 				case 1:
378f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
379f432a6ddSmatthias.ringwald 					break;
380f432a6ddSmatthias.ringwald                 case 2:
3813429f56bSmatthias.ringwald                     // ca. 15 sec
3823429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
3833429f56bSmatthias.ringwald                     break;
384f432a6ddSmatthias.ringwald 				case 3:
385bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
386f432a6ddSmatthias.ringwald 					break;
387f432a6ddSmatthias.ringwald                 case 4:
3883429f56bSmatthias.ringwald                     // done.
3893429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
390b360b6adSmatthias.ringwald                     hci_emit_state();
3913429f56bSmatthias.ringwald                     break;
3923429f56bSmatthias.ringwald                 default:
3933429f56bSmatthias.ringwald                     break;
394475c8125Smatthias.ringwald             }
3953429f56bSmatthias.ringwald             hci_stack.substate++;
3963429f56bSmatthias.ringwald             break;
3973429f56bSmatthias.ringwald         default:
3983429f56bSmatthias.ringwald             break;
3991f504dbdSmatthias.ringwald     }
4003429f56bSmatthias.ringwald }
40116833f0aSmatthias.ringwald 
40231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
403c8e4258aSmatthias.ringwald     bd_addr_t addr;
404c8e4258aSmatthias.ringwald     hci_connection_t * conn;
405c8e4258aSmatthias.ringwald     // house-keeping
406c8e4258aSmatthias.ringwald 
407c8e4258aSmatthias.ringwald     // create_connection?
408c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
409c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
410c8e4258aSmatthias.ringwald         printf("Create_connection to "); print_bd_addr(addr); printf("\n");
411c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
412c8e4258aSmatthias.ringwald         if (conn) {
413c8e4258aSmatthias.ringwald             // if connection exists
414c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
415c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
416c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
417c8e4258aSmatthias.ringwald             }
418c8e4258aSmatthias.ringwald             //    otherwise, just ignore
419c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
420c8e4258aSmatthias.ringwald 
421c8e4258aSmatthias.ringwald         } else{
422c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
423c8e4258aSmatthias.ringwald             if (conn){
424c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
425c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
426c8e4258aSmatthias.ringwald             }
427c8e4258aSmatthias.ringwald         }
428c8e4258aSmatthias.ringwald     }
429c8e4258aSmatthias.ringwald 
430c8e4258aSmatthias.ringwald     // accept connection
431c8e4258aSmatthias.ringwald 
432c8e4258aSmatthias.ringwald     // reject connection
433c8e4258aSmatthias.ringwald 
434c8e4258aSmatthias.ringwald     // close_connection?
435c8e4258aSmatthias.ringwald       // set state = SENT_DISCONNECT
436c8e4258aSmatthias.ringwald 
43731452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
43831452debSmatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(packet, size);
43931452debSmatthias.ringwald }
4408adf0ddaSmatthias.ringwald 
4411cd208adSmatthias.ringwald /**
4421cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
4431cd208adSmatthias.ringwald  */
4441cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
4451cd208adSmatthias.ringwald     va_list argptr;
4461cd208adSmatthias.ringwald     va_start(argptr, cmd);
4471cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
4481cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
4491cd208adSmatthias.ringwald     va_end(argptr);
4501cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
45193b8dc03Smatthias.ringwald }
452c8e4258aSmatthias.ringwald 
453ee091cf1Smatthias.ringwald // Create various non-HCI events.
454ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
455ee091cf1Smatthias.ringwald 
456c8e4258aSmatthias.ringwald void hci_emit_state(){
457c8e4258aSmatthias.ringwald     uint8_t len = 3;
458c8e4258aSmatthias.ringwald     uint8_t event[len];
45980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
4601b0e3922Smatthias.ringwald     event[1] = len - 3;
461c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
462c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
463c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
464c8e4258aSmatthias.ringwald }
465c8e4258aSmatthias.ringwald 
466c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
467c8e4258aSmatthias.ringwald     uint8_t len = 13;
468c8e4258aSmatthias.ringwald     uint8_t event[len];
469c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
4701b0e3922Smatthias.ringwald     event[1] = len - 3;
471c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
472c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
473c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
474c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
475c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
476c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
477c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
478c8e4258aSmatthias.ringwald }
479c8e4258aSmatthias.ringwald 
480ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
481ee091cf1Smatthias.ringwald     uint8_t len = 4;
482ee091cf1Smatthias.ringwald     uint8_t event[len];
48380d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
4841b0e3922Smatthias.ringwald     event[1] = len - 2;
485ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
486ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
487ee091cf1Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
488ee091cf1Smatthias.ringwald }
48943bfb1bdSmatthias.ringwald 
49043bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
49143bfb1bdSmatthias.ringwald     uint8_t len = 3;
49243bfb1bdSmatthias.ringwald     uint8_t event[len];
49380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
4941b0e3922Smatthias.ringwald     event[1] = len - 2;
49543bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
49643bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
49743bfb1bdSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
49843bfb1bdSmatthias.ringwald }
499038bc64cSmatthias.ringwald 
500038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
5011b0e3922Smatthias.ringwald     uint8_t len = 2;
502038bc64cSmatthias.ringwald     uint8_t event[len];
50380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
5041b0e3922Smatthias.ringwald     event[1] = len - 2;
505038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
506038bc64cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
507038bc64cSmatthias.ringwald }
5081b0e3922Smatthias.ringwald 
5091b0e3922Smatthias.ringwald 
5101b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
5111b0e3922Smatthias.ringwald     uint8_t len = 6;
5121b0e3922Smatthias.ringwald     uint8_t event[len];
5131b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
5141b0e3922Smatthias.ringwald     event[1] = len - 2;
5151b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
5161b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
5171b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
5181b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
5191b0e3922Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
5201b0e3922Smatthias.ringwald }
5211b0e3922Smatthias.ringwald 
5222ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
5232ed6235cSmatthias.ringwald     uint8_t len = 3;
5242ed6235cSmatthias.ringwald     uint8_t event[len];
5252ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
5262ed6235cSmatthias.ringwald     event[1] = len - 3;
5272ed6235cSmatthias.ringwald     event[2] = enabled;
5282ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
5292ed6235cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
5302ed6235cSmatthias.ringwald }
531