xref: /btstack/src/hci.c (revision 1f7b95a184f89e56f37b0fb84fff5c8fe20e5f38)
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 
52c21e6239Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 5000
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  */
62ee091cf1Smatthias.ringwald static 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;
180*1f7b95a1Smatthias.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 
190*1f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
191*1f7b95a1Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
192*1f7b95a1Smatthias.ringwald             printf("Connection_incoming: "); print_bd_addr(addr); printf("\n");
193*1f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
194*1f7b95a1Smatthias.ringwald             if (!conn) {
195*1f7b95a1Smatthias.ringwald                 conn = create_connection_for_addr(addr);
196*1f7b95a1Smatthias.ringwald             }
197*1f7b95a1Smatthias.ringwald             // TODO: check for malloc failure
198*1f7b95a1Smatthias.ringwald             conn->state = ACCEPTED_CONNECTION_REQUEST;
199*1f7b95a1Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, 1);
200*1f7b95a1Smatthias.ringwald             break;
201*1f7b95a1Smatthias.ringwald 
2026772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
203fe1ed1b8Smatthias.ringwald             // Connection management
204fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
205b448a0e7Smatthias.ringwald             printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n");
206*1f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
207fe1ed1b8Smatthias.ringwald             if (conn) {
208b448a0e7Smatthias.ringwald                 if (!packet[2]){
209c8e4258aSmatthias.ringwald                     conn->state = OPEN;
210fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
211fe1ed1b8Smatthias.ringwald                     conn->flags = 0;
212ee091cf1Smatthias.ringwald 
213ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
214c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
215ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
216ee091cf1Smatthias.ringwald 
217fe1ed1b8Smatthias.ringwald                     printf("New connection: handle %u, ", conn->con_handle);
218fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
219fe1ed1b8Smatthias.ringwald                     printf("\n");
22043bfb1bdSmatthias.ringwald 
22143bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
222b448a0e7Smatthias.ringwald                 } else {
223b448a0e7Smatthias.ringwald                     // connection failed, remove entry
224b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
225b448a0e7Smatthias.ringwald                     free( conn );
226fe1ed1b8Smatthias.ringwald                 }
227fe1ed1b8Smatthias.ringwald             }
2286772a24cSmatthias.ringwald             break;
229fe1ed1b8Smatthias.ringwald 
2306772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
231fe1ed1b8Smatthias.ringwald             if (!packet[2]){
232fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
233fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
234fe1ed1b8Smatthias.ringwald                 if (conn) {
235fe1ed1b8Smatthias.ringwald                     printf("Connection closed: handle %u, ", conn->con_handle);
236fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
237fe1ed1b8Smatthias.ringwald                     printf("\n");
238ee091cf1Smatthias.ringwald                     run_loop_remove_timer(&conn->timeout);
239fe1ed1b8Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
240fe1ed1b8Smatthias.ringwald                     free( conn );
24143bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
242fe1ed1b8Smatthias.ringwald                 }
243fe1ed1b8Smatthias.ringwald             }
2446772a24cSmatthias.ringwald             break;
2456772a24cSmatthias.ringwald 
2466772a24cSmatthias.ringwald         default:
2476772a24cSmatthias.ringwald             break;
248fe1ed1b8Smatthias.ringwald     }
249fe1ed1b8Smatthias.ringwald 
2503429f56bSmatthias.ringwald     // handle BT initialization
2513429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
2527301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
2537301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
2547301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
2557301ad89Smatthias.ringwald         // }
2567301ad89Smatthias.ringwald         // handle normal init sequence
2573429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
2583429f56bSmatthias.ringwald             // odd: waiting for event
2593429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
2603429f56bSmatthias.ringwald                 hci_stack.substate++;
2613429f56bSmatthias.ringwald             }
2623429f56bSmatthias.ringwald         }
26322909952Smatthias.ringwald     }
26422909952Smatthias.ringwald 
26516833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
26694ab26f8Smatthias.ringwald 
26794ab26f8Smatthias.ringwald 	// execute main loop
26894ab26f8Smatthias.ringwald 	hci_run();
26916833f0aSmatthias.ringwald }
27016833f0aSmatthias.ringwald 
271fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
2721cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
27316833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
27416833f0aSmatthias.ringwald }
2751cd208adSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
27616833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
27716833f0aSmatthias.ringwald }
27816833f0aSmatthias.ringwald 
27911e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
280475c8125Smatthias.ringwald 
281475c8125Smatthias.ringwald     // reference to use transport layer implementation
28216833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
283475c8125Smatthias.ringwald 
28411e23e5fSmatthias.ringwald     // references to used control implementation
2857301ad89Smatthias.ringwald     if (control) {
28611e23e5fSmatthias.ringwald         hci_stack.control = control;
2877301ad89Smatthias.ringwald     } else {
2887301ad89Smatthias.ringwald         hci_stack.control = &null_control;
2897301ad89Smatthias.ringwald     }
29011e23e5fSmatthias.ringwald 
29111e23e5fSmatthias.ringwald     // reference to used config
29211e23e5fSmatthias.ringwald     hci_stack.config = config;
29311e23e5fSmatthias.ringwald 
294fe1ed1b8Smatthias.ringwald     // no connections yet
295fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
296fe1ed1b8Smatthias.ringwald 
29702ea9861Smatthias.ringwald     // empty cmd buffer
29816833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
29916833f0aSmatthias.ringwald 
30016833f0aSmatthias.ringwald     // higher level handler
30116833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
30216833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
30316833f0aSmatthias.ringwald 
30416833f0aSmatthias.ringwald     // register packet handlers with transport
30516833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
30616833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
307475c8125Smatthias.ringwald }
308475c8125Smatthias.ringwald 
309475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
310f12adbd6Smatthias.ringwald     if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
3117301ad89Smatthias.ringwald 
312038bc64cSmatthias.ringwald         // power on
313038bc64cSmatthias.ringwald         int err = hci_stack.control->on(hci_stack.config);
314038bc64cSmatthias.ringwald         if (err){
315ac2e07f8Smatthias.ringwald             fprintf(stderr, "POWER_ON failed\n");
316038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
317038bc64cSmatthias.ringwald             return err;
318038bc64cSmatthias.ringwald         }
319038bc64cSmatthias.ringwald 
320038bc64cSmatthias.ringwald         // open low-level device
321038bc64cSmatthias.ringwald         err = hci_stack.hci_transport->open(hci_stack.config);
322038bc64cSmatthias.ringwald         if (err){
323ac2e07f8Smatthias.ringwald             fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n");
324038bc64cSmatthias.ringwald             hci_stack.control->off(hci_stack.config);
325038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
326038bc64cSmatthias.ringwald             return err;
327038bc64cSmatthias.ringwald         }
328038bc64cSmatthias.ringwald 
3297301ad89Smatthias.ringwald         // set up state machine
3307301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
3317301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
3327301ad89Smatthias.ringwald         hci_stack.substate = 0;
3337301ad89Smatthias.ringwald 
334f12adbd6Smatthias.ringwald     } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
3357301ad89Smatthias.ringwald 
3367301ad89Smatthias.ringwald         // close low-level device
3377301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
3387301ad89Smatthias.ringwald 
3397301ad89Smatthias.ringwald         // power off
34011e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
34143bfb1bdSmatthias.ringwald 
34243bfb1bdSmatthias.ringwald         // we're off now
34343bfb1bdSmatthias.ringwald         hci_stack.state = HCI_STATE_OFF;
34411e23e5fSmatthias.ringwald     }
34568d92d03Smatthias.ringwald 
346038bc64cSmatthias.ringwald     // create internal event
347ee8bf225Smatthias.ringwald 	hci_emit_state();
348ee8bf225Smatthias.ringwald 
34968d92d03Smatthias.ringwald 	// trigger next/first action
35068d92d03Smatthias.ringwald 	hci_run();
35168d92d03Smatthias.ringwald 
352475c8125Smatthias.ringwald     return 0;
353475c8125Smatthias.ringwald }
354475c8125Smatthias.ringwald 
35506b35ec0Smatthias.ringwald void hci_run(){
3563429f56bSmatthias.ringwald     switch (hci_stack.state){
3573429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
3583429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
3593429f56bSmatthias.ringwald                 // odd: waiting for command completion
36006b35ec0Smatthias.ringwald                 return;
3613429f56bSmatthias.ringwald             }
3623429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
3633429f56bSmatthias.ringwald                 // cannot send command yet
36406b35ec0Smatthias.ringwald                 return;
3653429f56bSmatthias.ringwald             }
3663429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
3673429f56bSmatthias.ringwald                 case 0:
36822909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
3693429f56bSmatthias.ringwald                     break;
3703429f56bSmatthias.ringwald 				case 1:
371f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
372f432a6ddSmatthias.ringwald 					break;
373f432a6ddSmatthias.ringwald                 case 2:
3743429f56bSmatthias.ringwald                     // ca. 15 sec
3753429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
3763429f56bSmatthias.ringwald                     break;
377f432a6ddSmatthias.ringwald 				case 3:
378bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
379f432a6ddSmatthias.ringwald 					break;
380f432a6ddSmatthias.ringwald                 case 4:
3813429f56bSmatthias.ringwald                     // done.
3823429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
383b360b6adSmatthias.ringwald                     hci_emit_state();
3843429f56bSmatthias.ringwald                     break;
3853429f56bSmatthias.ringwald                 default:
3863429f56bSmatthias.ringwald                     break;
387475c8125Smatthias.ringwald             }
3883429f56bSmatthias.ringwald             hci_stack.substate++;
3893429f56bSmatthias.ringwald             break;
3903429f56bSmatthias.ringwald         default:
3913429f56bSmatthias.ringwald             break;
3921f504dbdSmatthias.ringwald     }
3933429f56bSmatthias.ringwald }
39416833f0aSmatthias.ringwald 
39531452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
396c8e4258aSmatthias.ringwald     bd_addr_t addr;
397c8e4258aSmatthias.ringwald     hci_connection_t * conn;
398c8e4258aSmatthias.ringwald     // house-keeping
399c8e4258aSmatthias.ringwald 
400c8e4258aSmatthias.ringwald     // create_connection?
401c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
402c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
403c8e4258aSmatthias.ringwald         printf("Create_connection to "); print_bd_addr(addr); printf("\n");
404c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
405c8e4258aSmatthias.ringwald         if (conn) {
406c8e4258aSmatthias.ringwald             // if connection exists
407c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
408c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
409c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
410c8e4258aSmatthias.ringwald             }
411c8e4258aSmatthias.ringwald             //    otherwise, just ignore
412c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
413c8e4258aSmatthias.ringwald 
414c8e4258aSmatthias.ringwald         } else{
415c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
416c8e4258aSmatthias.ringwald             if (conn){
417c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
418c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
419c8e4258aSmatthias.ringwald             }
420c8e4258aSmatthias.ringwald         }
421c8e4258aSmatthias.ringwald     }
422c8e4258aSmatthias.ringwald 
423c8e4258aSmatthias.ringwald     // accept connection
424c8e4258aSmatthias.ringwald 
425c8e4258aSmatthias.ringwald     // reject connection
426c8e4258aSmatthias.ringwald 
427c8e4258aSmatthias.ringwald     // close_connection?
428c8e4258aSmatthias.ringwald       // set state = SENT_DISCONNECT
429c8e4258aSmatthias.ringwald 
43031452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
43131452debSmatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(packet, size);
43231452debSmatthias.ringwald }
4338adf0ddaSmatthias.ringwald 
4341cd208adSmatthias.ringwald /**
4351cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
4361cd208adSmatthias.ringwald  */
4371cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
4381cd208adSmatthias.ringwald     va_list argptr;
4391cd208adSmatthias.ringwald     va_start(argptr, cmd);
4401cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
4411cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
4421cd208adSmatthias.ringwald     va_end(argptr);
4431cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
44493b8dc03Smatthias.ringwald }
445c8e4258aSmatthias.ringwald 
446ee091cf1Smatthias.ringwald // Create various non-HCI events.
447ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
448ee091cf1Smatthias.ringwald 
449c8e4258aSmatthias.ringwald void hci_emit_state(){
450c8e4258aSmatthias.ringwald     uint8_t len = 3;
451c8e4258aSmatthias.ringwald     uint8_t event[len];
45280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
4531b0e3922Smatthias.ringwald     event[1] = len - 3;
454c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
455c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
456c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
457c8e4258aSmatthias.ringwald }
458c8e4258aSmatthias.ringwald 
459c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
460c8e4258aSmatthias.ringwald     uint8_t len = 13;
461c8e4258aSmatthias.ringwald     uint8_t event[len];
462c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
4631b0e3922Smatthias.ringwald     event[1] = len - 3;
464c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
465c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
466c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
467c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
468c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
469c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
470c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
471c8e4258aSmatthias.ringwald }
472c8e4258aSmatthias.ringwald 
473ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
474ee091cf1Smatthias.ringwald     uint8_t len = 4;
475ee091cf1Smatthias.ringwald     uint8_t event[len];
47680d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
4771b0e3922Smatthias.ringwald     event[1] = len - 2;
478ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
479ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
480ee091cf1Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
481ee091cf1Smatthias.ringwald }
48243bfb1bdSmatthias.ringwald 
48343bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
48443bfb1bdSmatthias.ringwald     uint8_t len = 3;
48543bfb1bdSmatthias.ringwald     uint8_t event[len];
48680d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
4871b0e3922Smatthias.ringwald     event[1] = len - 2;
48843bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
48943bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
49043bfb1bdSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
49143bfb1bdSmatthias.ringwald }
492038bc64cSmatthias.ringwald 
493038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
4941b0e3922Smatthias.ringwald     uint8_t len = 2;
495038bc64cSmatthias.ringwald     uint8_t event[len];
49680d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
4971b0e3922Smatthias.ringwald     event[1] = len - 2;
498038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
499038bc64cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
500038bc64cSmatthias.ringwald }
5011b0e3922Smatthias.ringwald 
5021b0e3922Smatthias.ringwald 
5031b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
5041b0e3922Smatthias.ringwald     uint8_t len = 6;
5051b0e3922Smatthias.ringwald     uint8_t event[len];
5061b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
5071b0e3922Smatthias.ringwald     event[1] = len - 2;
5081b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
5091b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
5101b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
5111b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
5121b0e3922Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
5131b0e3922Smatthias.ringwald }
5141b0e3922Smatthias.ringwald 
5152ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
5162ed6235cSmatthias.ringwald     uint8_t len = 3;
5172ed6235cSmatthias.ringwald     uint8_t event[len];
5182ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
5192ed6235cSmatthias.ringwald     event[1] = len - 3;
5202ed6235cSmatthias.ringwald     event[2] = enabled;
5212ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
5222ed6235cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
5232ed6235cSmatthias.ringwald }
524