xref: /btstack/src/hci.c (revision fcadd0ca34dea9fd124dbf7316e77852015c9ef9)
11f504dbdSmatthias.ringwald /*
21f504dbdSmatthias.ringwald  *  hci.c
31f504dbdSmatthias.ringwald  *
41f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
51f504dbdSmatthias.ringwald  *
61f504dbdSmatthias.ringwald  */
71f504dbdSmatthias.ringwald 
8475c8125Smatthias.ringwald #include <unistd.h>
993b8dc03Smatthias.ringwald #include <stdarg.h>
1093b8dc03Smatthias.ringwald #include <string.h>
1156fe0872Smatthias.ringwald #include <stdio.h>
121f504dbdSmatthias.ringwald #include "hci.h"
13d8905019Smatthias.ringwald #include "hci_dump.h"
1493b8dc03Smatthias.ringwald 
151e6aba47Smatthias.ringwald // temp
161e6aba47Smatthias.ringwald #include "l2cap.h"
171e6aba47Smatthias.ringwald 
1806b35ec0Smatthias.ringwald // the STACK is here
1916833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
2016833f0aSmatthias.ringwald 
2197addcc5Smatthias.ringwald /**
2206b35ec0Smatthias.ringwald  * get connection for given address
2397addcc5Smatthias.ringwald  *
2497addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
2597addcc5Smatthias.ringwald  */
26fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
2706b35ec0Smatthias.ringwald     linked_item_t *it;
2806b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
2906b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
3006b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
3106b35ec0Smatthias.ringwald         }
3206b35ec0Smatthias.ringwald     }
3306b35ec0Smatthias.ringwald     return NULL;
3406b35ec0Smatthias.ringwald }
3506b35ec0Smatthias.ringwald 
3606b35ec0Smatthias.ringwald /**
3706b35ec0Smatthias.ringwald  * get connection for a given handle
3806b35ec0Smatthias.ringwald  *
3906b35ec0Smatthias.ringwald  * @return connection OR NULL, if not found
4006b35ec0Smatthias.ringwald  */
41fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
4206b35ec0Smatthias.ringwald     linked_item_t *it;
4306b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
4406b35ec0Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
4506b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
4606b35ec0Smatthias.ringwald         }
4706b35ec0Smatthias.ringwald     }
4897addcc5Smatthias.ringwald     return NULL;
4997addcc5Smatthias.ringwald }
5097addcc5Smatthias.ringwald 
5197addcc5Smatthias.ringwald /**
52ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
5316833f0aSmatthias.ringwald  */
541cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){
5516833f0aSmatthias.ringwald }
5616833f0aSmatthias.ringwald 
57ba681a6cSmatthias.ringwald /**
58ba681a6cSmatthias.ringwald  * Dummy control handler
59ba681a6cSmatthias.ringwald  */
60ba681a6cSmatthias.ringwald static int null_control_function(void *config){
61ba681a6cSmatthias.ringwald     return 0;
62ba681a6cSmatthias.ringwald }
63ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){
64ba681a6cSmatthias.ringwald     return "Hardware unknown";
65ba681a6cSmatthias.ringwald }
66ba681a6cSmatthias.ringwald static bt_control_t null_control = {
67ba681a6cSmatthias.ringwald     null_control_function,
68ba681a6cSmatthias.ringwald     null_control_function,
69ba681a6cSmatthias.ringwald     null_control_function,
70ba681a6cSmatthias.ringwald     null_control_name
71ba681a6cSmatthias.ringwald };
72ba681a6cSmatthias.ringwald 
7316833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
7416833f0aSmatthias.ringwald     hci_stack.acl_packet_handler(packet, size);
7594ab26f8Smatthias.ringwald 
7694ab26f8Smatthias.ringwald     // execute main loop
7794ab26f8Smatthias.ringwald     hci_run();
7816833f0aSmatthias.ringwald }
7922909952Smatthias.ringwald 
8016833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
811281a47eSmatthias.ringwald     bd_addr_t addr;
82fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
8322909952Smatthias.ringwald 
843429f56bSmatthias.ringwald     // Get Num_HCI_Command_Packets
853429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
863429f56bSmatthias.ringwald         packet[0] == HCI_EVENT_COMMAND_STATUS){
873429f56bSmatthias.ringwald         hci_stack.num_cmd_packets = packet[2];
8822909952Smatthias.ringwald     }
8922909952Smatthias.ringwald 
90fe1ed1b8Smatthias.ringwald     // Connection management
91fe1ed1b8Smatthias.ringwald     if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) {
92fe1ed1b8Smatthias.ringwald         if (!packet[2]){
93fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
94fe1ed1b8Smatthias.ringwald             hci_connection_t * conn = connection_for_address(addr);
95fe1ed1b8Smatthias.ringwald             if (!conn) {
96fe1ed1b8Smatthias.ringwald                 conn = malloc( sizeof(hci_connection_t) );
97fe1ed1b8Smatthias.ringwald                 if (conn) {
98fe1ed1b8Smatthias.ringwald                     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
99fe1ed1b8Smatthias.ringwald                 }
100fe1ed1b8Smatthias.ringwald             }
101fe1ed1b8Smatthias.ringwald             if (conn) {
102fe1ed1b8Smatthias.ringwald                 BD_ADDR_COPY(conn->address, addr);
103fe1ed1b8Smatthias.ringwald                 conn->con_handle = READ_BT_16(packet, 3);
104fe1ed1b8Smatthias.ringwald                 conn->flags = 0;
105fe1ed1b8Smatthias.ringwald                 printf("New connection: handle %u, ", conn->con_handle);
106fe1ed1b8Smatthias.ringwald                 print_bd_addr( conn->address );
107fe1ed1b8Smatthias.ringwald                 printf("\n");
108fe1ed1b8Smatthias.ringwald             }
109fe1ed1b8Smatthias.ringwald         }
110fe1ed1b8Smatthias.ringwald     }
111fe1ed1b8Smatthias.ringwald 
112fe1ed1b8Smatthias.ringwald     if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) {
113fe1ed1b8Smatthias.ringwald         if (!packet[2]){
114fe1ed1b8Smatthias.ringwald             handle = READ_BT_16(packet, 3);
115fe1ed1b8Smatthias.ringwald             hci_connection_t * conn = connection_for_handle(handle);
116fe1ed1b8Smatthias.ringwald             if (conn) {
117fe1ed1b8Smatthias.ringwald                 printf("Connection closed: handle %u, ", conn->con_handle);
118fe1ed1b8Smatthias.ringwald                 print_bd_addr( conn->address );
119fe1ed1b8Smatthias.ringwald                 printf("\n");
120fe1ed1b8Smatthias.ringwald                 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
121fe1ed1b8Smatthias.ringwald                 free( conn );
122fe1ed1b8Smatthias.ringwald             }
123fe1ed1b8Smatthias.ringwald         }
124fe1ed1b8Smatthias.ringwald     }
125fe1ed1b8Smatthias.ringwald 
1263429f56bSmatthias.ringwald     // handle BT initialization
1273429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
1287301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
1297301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
1307301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
1317301ad89Smatthias.ringwald         // }
1327301ad89Smatthias.ringwald         // handle normal init sequence
1333429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
1343429f56bSmatthias.ringwald             // odd: waiting for event
1353429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
1363429f56bSmatthias.ringwald                 hci_stack.substate++;
1373429f56bSmatthias.ringwald             }
1383429f56bSmatthias.ringwald         }
13922909952Smatthias.ringwald     }
14022909952Smatthias.ringwald 
1411281a47eSmatthias.ringwald     // link key request
1423429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
1431281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
1441281a47eSmatthias.ringwald         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
1451281a47eSmatthias.ringwald         return;
1461281a47eSmatthias.ringwald     }
1471281a47eSmatthias.ringwald 
1481281a47eSmatthias.ringwald     // pin code request
1493429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
1501281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
1511281a47eSmatthias.ringwald         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
1521281a47eSmatthias.ringwald     }
1531281a47eSmatthias.ringwald 
15416833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
15594ab26f8Smatthias.ringwald 
15694ab26f8Smatthias.ringwald 	// execute main loop
15794ab26f8Smatthias.ringwald 	hci_run();
15816833f0aSmatthias.ringwald }
15916833f0aSmatthias.ringwald 
160*fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
1611cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
16216833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
16316833f0aSmatthias.ringwald }
1641cd208adSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
16516833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
16616833f0aSmatthias.ringwald }
16716833f0aSmatthias.ringwald 
16811e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
169475c8125Smatthias.ringwald 
170475c8125Smatthias.ringwald     // reference to use transport layer implementation
17116833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
172475c8125Smatthias.ringwald 
17311e23e5fSmatthias.ringwald     // references to used control implementation
1747301ad89Smatthias.ringwald     if (control) {
17511e23e5fSmatthias.ringwald         hci_stack.control = control;
1767301ad89Smatthias.ringwald     } else {
1777301ad89Smatthias.ringwald         hci_stack.control = &null_control;
1787301ad89Smatthias.ringwald     }
17911e23e5fSmatthias.ringwald 
18011e23e5fSmatthias.ringwald     // reference to used config
18111e23e5fSmatthias.ringwald     hci_stack.config = config;
18211e23e5fSmatthias.ringwald 
183fe1ed1b8Smatthias.ringwald     // no connections yet
184fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
185fe1ed1b8Smatthias.ringwald 
18602ea9861Smatthias.ringwald     // empty cmd buffer
18716833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
18816833f0aSmatthias.ringwald 
18916833f0aSmatthias.ringwald     // higher level handler
19016833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
19116833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
19216833f0aSmatthias.ringwald 
19316833f0aSmatthias.ringwald     // register packet handlers with transport
19416833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
19516833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
196475c8125Smatthias.ringwald }
197475c8125Smatthias.ringwald 
1981e6aba47Smatthias.ringwald void hci_emit_state(){
199ee8bf225Smatthias.ringwald     uint8_t event[3];
200ee8bf225Smatthias.ringwald     event[0] = HCI_EVENT_BTSTACK_STATE;
201ee8bf225Smatthias.ringwald     event[1] = 1;
202ee8bf225Smatthias.ringwald     event[2] = hci_stack.state;
203ee8bf225Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, 3);
204ee8bf225Smatthias.ringwald     hci_stack.event_packet_handler(event, 3);
205ee8bf225Smatthias.ringwald }
206ee8bf225Smatthias.ringwald 
207475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
20811e23e5fSmatthias.ringwald     if (power_mode == HCI_POWER_ON) {
2097301ad89Smatthias.ringwald 
2107301ad89Smatthias.ringwald         // set up state machine
2117301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
2127301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
2137301ad89Smatthias.ringwald         hci_stack.substate = 0;
2147301ad89Smatthias.ringwald 
2157301ad89Smatthias.ringwald         // power on
21611e23e5fSmatthias.ringwald         hci_stack.control->on(hci_stack.config);
2177301ad89Smatthias.ringwald 
2187301ad89Smatthias.ringwald         // open low-level device
2197301ad89Smatthias.ringwald         hci_stack.hci_transport->open(hci_stack.config);
2207301ad89Smatthias.ringwald 
221ee8bf225Smatthias.ringwald         // create internal event
222ee8bf225Smatthias.ringwald 
22311e23e5fSmatthias.ringwald     } else if (power_mode == HCI_POWER_OFF){
2247301ad89Smatthias.ringwald 
2257301ad89Smatthias.ringwald         // close low-level device
2267301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
2277301ad89Smatthias.ringwald 
2287301ad89Smatthias.ringwald         // power off
22911e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
23011e23e5fSmatthias.ringwald     }
23168d92d03Smatthias.ringwald 
232ee8bf225Smatthias.ringwald 	hci_emit_state();
233ee8bf225Smatthias.ringwald 
23468d92d03Smatthias.ringwald 	// trigger next/first action
23568d92d03Smatthias.ringwald 	hci_run();
23668d92d03Smatthias.ringwald 
237475c8125Smatthias.ringwald     return 0;
238475c8125Smatthias.ringwald }
239475c8125Smatthias.ringwald 
24006b35ec0Smatthias.ringwald void hci_run(){
2413429f56bSmatthias.ringwald     uint8_t micro_packet;
2423429f56bSmatthias.ringwald     switch (hci_stack.state){
2433429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
2443429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
2453429f56bSmatthias.ringwald                 // odd: waiting for command completion
24606b35ec0Smatthias.ringwald                 return;
2473429f56bSmatthias.ringwald             }
2483429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
2493429f56bSmatthias.ringwald                 // cannot send command yet
25006b35ec0Smatthias.ringwald                 return;
2513429f56bSmatthias.ringwald             }
2523429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
2533429f56bSmatthias.ringwald                 case 0:
25422909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
2553429f56bSmatthias.ringwald                     break;
2563429f56bSmatthias.ringwald 				case 1:
257f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
258f432a6ddSmatthias.ringwald 					break;
259f432a6ddSmatthias.ringwald                 case 2:
2603429f56bSmatthias.ringwald                     // ca. 15 sec
2613429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
2623429f56bSmatthias.ringwald                     break;
263f432a6ddSmatthias.ringwald 				case 3:
264bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
265f432a6ddSmatthias.ringwald 					break;
266f432a6ddSmatthias.ringwald                 case 4:
2673429f56bSmatthias.ringwald                     // done.
2683429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
2698adf0ddaSmatthias.ringwald                     micro_packet = HCI_EVENT_BTSTACK_WORKING;
2703429f56bSmatthias.ringwald                     hci_stack.event_packet_handler(&micro_packet, 1);
2713429f56bSmatthias.ringwald                     break;
2723429f56bSmatthias.ringwald                 default:
2733429f56bSmatthias.ringwald                     break;
274475c8125Smatthias.ringwald             }
2753429f56bSmatthias.ringwald             hci_stack.substate++;
2763429f56bSmatthias.ringwald             break;
2773429f56bSmatthias.ringwald         default:
2783429f56bSmatthias.ringwald             break;
2791f504dbdSmatthias.ringwald     }
2803429f56bSmatthias.ringwald }
28116833f0aSmatthias.ringwald 
28216833f0aSmatthias.ringwald 
28343625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
28416833f0aSmatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
28543625864Smatthias.ringwald }
28643625864Smatthias.ringwald 
28731452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
28831452debSmatthias.ringwald     if (READ_CMD_OGF(packet) != OGF_BTSTACK) {
28931452debSmatthias.ringwald         hci_stack.num_cmd_packets--;
29031452debSmatthias.ringwald         return hci_stack.hci_transport->send_cmd_packet(packet, size);
29131452debSmatthias.ringwald     }
2928adf0ddaSmatthias.ringwald     return 0;
2938adf0ddaSmatthias.ringwald }
2948adf0ddaSmatthias.ringwald 
2951cd208adSmatthias.ringwald /**
2961cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
2971cd208adSmatthias.ringwald  */
2981cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
2991cd208adSmatthias.ringwald     va_list argptr;
3001cd208adSmatthias.ringwald     va_start(argptr, cmd);
3011cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
3021cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
3031cd208adSmatthias.ringwald     va_end(argptr);
3041cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
30593b8dc03Smatthias.ringwald }