xref: /btstack/src/hci.c (revision 97addcc5762d3c90512a11d6a9e1acc7c889b896)
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 
14 // calculate combined ogf/ocf value
15 #define OPCODE(ogf, ocf) (ocf | ogf << 10)
16 #define OGF_LINK_CONTROL 0x01
17 #define OGF_CONTROLLER_BASEBAND 0x03
18 
19 hci_cmd_t hci_inquiry = {
20     OPCODE(OGF_LINK_CONTROL, 0x01), "311"
21     // LAP, Inquiry length, Num_responses
22 };
23 
24 hci_cmd_t hci_link_key_request_negative_reply = {
25     OPCODE(OGF_LINK_CONTROL, 0x0c), "B"
26 };
27 
28 hci_cmd_t hci_pin_code_request_reply = {
29     OPCODE(OGF_LINK_CONTROL, 0x0d), "B1P"
30     // BD_ADDR, pin length, PIN: c-string
31 };
32 
33 hci_cmd_t hci_reset = {
34     OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), ""
35 };
36 
37 hci_cmd_t hci_create_connection = {
38     OPCODE(OGF_LINK_CONTROL, 0x05), "B21121"
39     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
40 };
41 
42 hci_cmd_t hci_write_page_timeout = {
43     OPCODE(OGF_CONTROLLER_BASEBAND, 0x18), "2"
44     // Page_Timeout * 0.625 ms
45 };
46 
47 hci_cmd_t hci_write_authentication_enable = {
48     OPCODE(OGF_CONTROLLER_BASEBAND, 0x20), "1"
49     // Authentication_Enable
50 };
51 
52 hci_cmd_t hci_host_buffer_size = {
53     OPCODE(OGF_CONTROLLER_BASEBAND, 0x33), "2122"
54     // Host_ACL_Data_Packet_Length:, Host_Synchronous_Data_Packet_Length:, Host_Total_Num_ACL_Data_Packets:, Host_Total_Num_Synchronous_Data_Packets:
55 };
56 
57 
58 // the stack is here
59 static hci_stack_t       hci_stack;
60 
61 
62 void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
63     buffer[pos] = value & 0xff;
64     buffer[pos+1] = value >> 8;
65 }
66 
67 void bt_flip_addr(bd_addr_t dest, bd_addr_t src){
68     dest[0] = src[5];
69     dest[1] = src[4];
70     dest[2] = src[3];
71     dest[3] = src[2];
72     dest[4] = src[1];
73     dest[5] = src[0];
74 }
75 
76 void hexdump(uint8_t *data, int size){
77     int i;
78     for (i=0; i<size;i++){
79         printf("%02X ", data[i]);
80     }
81     printf("\n");
82 }
83 
84 #if 0
85 static void *hci_daemon_thread(void *arg){
86     printf("HCI Daemon started\n");
87     hci_run(transport, &config);
88     return NULL;
89 }
90 #endif
91 
92 /**
93  * Linked link list
94  */
95 
96 /**
97  * get link for given address
98  *
99  * @return connection OR NULL, if not found
100  */
101 static hci_connection_t *link_for_addr(bd_addr_t addr){
102     return NULL;
103 }
104 
105 /**
106  * Handler called by HCI transport
107  */
108 static void dummy_handler(uint8_t *packet, int size){
109 }
110 
111 static void acl_handler(uint8_t *packet, int size){
112     hci_stack.acl_packet_handler(packet, size);
113 }
114 
115 static void event_handler(uint8_t *packet, int size){
116     bd_addr_t addr;
117 
118     // Get Num_HCI_Command_Packets
119     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
120         packet[0] == HCI_EVENT_COMMAND_STATUS){
121         hci_stack.num_cmd_packets = packet[2];
122     }
123 
124     // handle BT initialization
125     if (hci_stack.state == HCI_STATE_INITIALIZING){
126         if (hci_stack.substate % 2){
127             // odd: waiting for event
128             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
129                 hci_stack.substate++;
130             }
131         }
132     }
133 
134     // link key request
135     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
136         bt_flip_addr(addr, &packet[2]);
137         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
138         return;
139     }
140 
141     // pin code request
142     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
143         bt_flip_addr(addr, &packet[2]);
144         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
145     }
146 
147     hci_stack.event_packet_handler(packet, size);
148 }
149 
150 /** Register L2CAP handlers */
151 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
152     hci_stack.event_packet_handler = handler;
153 }
154 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
155     hci_stack.acl_packet_handler = handler;
156 }
157 
158 void hci_init(hci_transport_t *transport, void *config){
159 
160     // reference to use transport layer implementation
161     hci_stack.hci_transport = transport;
162 
163     // empty cmd buffer
164     hci_stack.hci_cmd_buffer = malloc(3+255);
165 
166     // set up state
167     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
168     hci_stack.state = HCI_STATE_INITIALIZING;
169     hci_stack.substate = 0;
170 
171     // higher level handler
172     hci_stack.event_packet_handler = dummy_handler;
173     hci_stack.acl_packet_handler = dummy_handler;
174 
175     // register packet handlers with transport
176     transport->register_event_packet_handler( event_handler);
177     transport->register_acl_packet_handler( acl_handler);
178 
179     // open low-level device
180     transport->open(config);
181 }
182 
183 int hci_power_control(HCI_POWER_MODE power_mode){
184     return 0;
185 }
186 
187 uint32_t hci_run(){
188     uint8_t micro_packet;
189     switch (hci_stack.state){
190         case HCI_STATE_INITIALIZING:
191             if (hci_stack.substate % 2) {
192                 // odd: waiting for command completion
193                 return 0;
194             }
195             if (hci_stack.num_cmd_packets == 0) {
196                 // cannot send command yet
197                 return 0;
198             }
199             switch (hci_stack.substate/2){
200                 case 0:
201                     hci_send_cmd(&hci_reset);
202                     break;
203                 case 1:
204                     // ca. 15 sec
205                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
206                     break;
207                 case 2:
208                     // done.
209                     hci_stack.state = HCI_STATE_WORKING;
210                     micro_packet = BTSTACK_EVENT_HCI_WORKING;
211                     hci_stack.event_packet_handler(&micro_packet, 1);
212                     break;
213                 default:
214                     break;
215             }
216             hci_stack.substate++;
217             break;
218         default:
219             break;
220     }
221 
222     // don't check for timetous yet
223     return 0;
224 }
225 
226 
227 int hci_send_acl_packet(uint8_t *packet, int size){
228     return hci_stack.hci_transport->send_acl_packet(packet, size);
229 }
230 
231 
232 /**
233  * pre: numcmds >= 0 - it's allowed to send a command to the controller
234  */
235 int hci_send_cmd(hci_cmd_t *cmd, ...){
236     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
237     hci_cmd_buffer[0] = cmd->opcode & 0xff;
238     hci_cmd_buffer[1] = cmd->opcode >> 8;
239     int pos = 3;
240 
241     va_list argptr;
242     va_start(argptr, cmd);
243     const char *format = cmd->format;
244     uint16_t word;
245     uint32_t longword;
246     uint8_t * ptr;
247     while (*format) {
248         switch(*format) {
249             case '1': //  8 bit value
250             case '2': // 16 bit value
251             case 'H': // hci_handle
252                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
253                 hci_cmd_buffer[pos++] = word & 0xff;
254                 if (*format == '2') {
255                     hci_cmd_buffer[pos++] = word >> 8;
256                 } else if (*format == 'H') {
257                     // TODO
258                 }
259                 break;
260             case '3':
261             case '4':
262                 longword = va_arg(argptr, uint32_t);
263                 // longword = va_arg(argptr, int);
264                 hci_cmd_buffer[pos++] = longword;
265                 hci_cmd_buffer[pos++] = longword >> 8;
266                 hci_cmd_buffer[pos++] = longword >> 16;
267                 if (*format == '4'){
268                     hci_cmd_buffer[pos++] = longword >> 24;
269                 }
270                 break;
271             case 'B': // bt-addr
272                 ptr = va_arg(argptr, uint8_t *);
273                 hci_cmd_buffer[pos++] = ptr[5];
274                 hci_cmd_buffer[pos++] = ptr[4];
275                 hci_cmd_buffer[pos++] = ptr[3];
276                 hci_cmd_buffer[pos++] = ptr[2];
277                 hci_cmd_buffer[pos++] = ptr[1];
278                 hci_cmd_buffer[pos++] = ptr[0];
279                 break;
280             case 'P': // c string passed as pascal string with leading 1-byte len
281                 ptr = va_arg(argptr, uint8_t *);
282                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
283                 pos += 16;
284                 break;
285             default:
286                 break;
287         }
288         format++;
289     };
290     va_end(argptr);
291     hci_cmd_buffer[2] = pos - 3;
292     // send packet
293     hci_stack.num_cmd_packets--;
294     return hci_stack.hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
295 }