xref: /btstack/src/hci.c (revision d8905019be9ad7442f3ac596d5059ee2e2a49025)
1 /*
2  *  hci.c
3  *
4  *  Created by Matthias Ringwald on 4/29/09.
5  *
6  */
7 
8 #include <unistd.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include "hci.h"
13 #include "hci_dump.h"
14 
15 // the stack is here
16 static hci_stack_t       hci_stack;
17 
18 
19 void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
20     buffer[pos++] = value;
21     buffer[pos++] = value >> 8;
22 }
23 
24 void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
25     buffer[pos++] = value;
26     buffer[pos++] = value >> 8;
27     buffer[pos++] = value >> 16;
28     buffer[pos++] = value >> 24;
29 }
30 
31 void bt_flip_addr(bd_addr_t dest, bd_addr_t src){
32     dest[0] = src[5];
33     dest[1] = src[4];
34     dest[2] = src[3];
35     dest[3] = src[2];
36     dest[4] = src[1];
37     dest[5] = src[0];
38 }
39 
40 void hexdump(void *data, int size){
41     int i;
42     for (i=0; i<size;i++){
43         printf("%02X ", ((uint8_t *)data)[i]);
44     }
45     printf("\n");
46 }
47 
48 /**
49  * Linked link list
50  */
51 
52 /**
53  * get link for given address
54  *
55  * @return connection OR NULL, if not found
56  */
57 #if 0
58 static hci_connection_t *link_for_addr(bd_addr_t addr){
59     return NULL;
60 }
61 #endif
62 
63 /**
64  * Handler called by HCI transport
65  */
66 static void dummy_handler(uint8_t *packet, uint16_t size){
67 }
68 
69 static void acl_handler(uint8_t *packet, int size){
70     hci_stack.acl_packet_handler(packet, size);
71 
72     // execute main loop
73     hci_run();
74 }
75 
76 static void event_handler(uint8_t *packet, int size){
77     bd_addr_t addr;
78 
79     // Get Num_HCI_Command_Packets
80     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
81         packet[0] == HCI_EVENT_COMMAND_STATUS){
82         hci_stack.num_cmd_packets = packet[2];
83     }
84 
85     // handle BT initialization
86     if (hci_stack.state == HCI_STATE_INITIALIZING){
87         // handle H4 synchronization loss on restart
88         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
89         //    hci_stack.substate = 0;
90         // }
91         // handle normal init sequence
92         if (hci_stack.substate % 2){
93             // odd: waiting for event
94             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
95                 hci_stack.substate++;
96             }
97         }
98     }
99 
100     // link key request
101     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
102         bt_flip_addr(addr, &packet[2]);
103         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
104         return;
105     }
106 
107     // pin code request
108     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
109         bt_flip_addr(addr, &packet[2]);
110         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
111     }
112 
113     hci_stack.event_packet_handler(packet, size);
114 
115 	// execute main loop
116 	hci_run();
117 }
118 
119 /** Register L2CAP handlers */
120 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
121     hci_stack.event_packet_handler = handler;
122 }
123 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
124     hci_stack.acl_packet_handler = handler;
125 }
126 
127 static int null_control_function(void *config){
128     return 0;
129 }
130 static const char * null_control_name(void *config){
131     return "Hardware unknown";
132 }
133 
134 static bt_control_t null_control = {
135     null_control_function,
136     null_control_function,
137     null_control_function,
138     null_control_name
139 };
140 
141 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
142 
143     // reference to use transport layer implementation
144     hci_stack.hci_transport = transport;
145 
146     // references to used control implementation
147     if (control) {
148         hci_stack.control = control;
149     } else {
150         hci_stack.control = &null_control;
151     }
152 
153     // reference to used config
154     hci_stack.config = config;
155 
156     // empty cmd buffer
157     hci_stack.hci_cmd_buffer = malloc(3+255);
158 
159     // higher level handler
160     hci_stack.event_packet_handler = dummy_handler;
161     hci_stack.acl_packet_handler = dummy_handler;
162 
163     // register packet handlers with transport
164     transport->register_event_packet_handler( event_handler);
165     transport->register_acl_packet_handler( acl_handler);
166 }
167 
168 int hci_power_control(HCI_POWER_MODE power_mode){
169     if (power_mode == HCI_POWER_ON) {
170 
171         // set up state machine
172         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
173         hci_stack.state = HCI_STATE_INITIALIZING;
174         hci_stack.substate = 0;
175 
176         // power on
177         hci_stack.control->on(hci_stack.config);
178 
179         // open low-level device
180         hci_stack.hci_transport->open(hci_stack.config);
181 
182     } else if (power_mode == HCI_POWER_OFF){
183 
184         // close low-level device
185         hci_stack.hci_transport->close(hci_stack.config);
186 
187         // power off
188         hci_stack.control->off(hci_stack.config);
189     }
190 
191 	// trigger next/first action
192 	hci_run();
193 
194     return 0;
195 }
196 
197 uint32_t hci_run(){
198     uint8_t micro_packet;
199     switch (hci_stack.state){
200         case HCI_STATE_INITIALIZING:
201             if (hci_stack.substate % 2) {
202                 // odd: waiting for command completion
203                 return 0;
204             }
205             if (hci_stack.num_cmd_packets == 0) {
206                 // cannot send command yet
207                 return 0;
208             }
209             switch (hci_stack.substate/2){
210                 case 0:
211                     hci_send_cmd(&hci_reset);
212                     break;
213 				case 1:
214 					hci_send_cmd(&hci_read_bd_addr);
215 					break;
216                 case 2:
217                     // ca. 15 sec
218                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
219                     break;
220 				case 3:
221 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
222 					break;
223                 case 4:
224                     // done.
225                     hci_stack.state = HCI_STATE_WORKING;
226                     micro_packet = HCI_EVENT_BTSTACK_WORKING;
227                     hci_stack.event_packet_handler(&micro_packet, 1);
228                     break;
229                 default:
230                     break;
231             }
232             hci_stack.substate++;
233             break;
234         default:
235             break;
236     }
237 
238     // don't check for timetous yet
239     return 0;
240 }
241 
242 
243 int hci_send_acl_packet(uint8_t *packet, int size){
244     return hci_stack.hci_transport->send_acl_packet(packet, size);
245 }
246 
247 int hci_send_cmd_packet(uint8_t *packet, int size){
248     if (READ_CMD_OGF(packet) != OGF_BTSTACK) {
249         hci_stack.num_cmd_packets--;
250         return hci_stack.hci_transport->send_cmd_packet(packet, size);
251     }
252 
253     hci_dump_packet( HCI_COMMAND_DATA_PACKET, 1, packet, size);
254 
255     // BTstack internal commands
256     uint8_t event[3];
257     switch (READ_CMD_OCF(packet)){
258         case HCI_BTSTACK_GET_STATE:
259             event[0] = HCI_EVENT_BTSTACK_STATE;
260             event[1] = 1;
261             event[2] = hci_stack.state;
262             hci_dump_packet( HCI_EVENT_PACKET, 0, event, 3);
263             hci_stack.event_packet_handler(event, 3);
264             break;
265         default:
266             // TODO log into hci dump as vendor specific"event"
267             printf("Error: command %u not implemented\n:", READ_CMD_OCF(packet));
268             break;
269     }
270     return 0;
271 }
272 
273 
274 /**
275  * pre: numcmds >= 0 - it's allowed to send a command to the controller
276  */
277 int hci_send_cmd(hci_cmd_t *cmd, ...){
278     va_list argptr;
279     va_start(argptr, cmd);
280     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
281     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
282     va_end(argptr);
283     return hci_send_cmd_packet(hci_cmd_buffer, size);
284 }