xref: /btstack/src/hci.c (revision 3429f56b3afeda9fa3084e9ddb7f766faae8048d)
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  * Handler called by HCI transport
94  */
95 static void dummy_handler(uint8_t *packet, int size){
96 }
97 
98 static void acl_handler(uint8_t *packet, int size){
99     hci_stack.acl_packet_handler(packet, size);
100 }
101 
102 static void event_handler(uint8_t *packet, int size){
103     bd_addr_t addr;
104 
105     // Get Num_HCI_Command_Packets
106     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
107         packet[0] == HCI_EVENT_COMMAND_STATUS){
108         hci_stack.num_cmd_packets = packet[2];
109     }
110 
111     // handle BT initialization
112     if (hci_stack.state == HCI_STATE_INITIALIZING){
113         if (hci_stack.substate % 2){
114             // odd: waiting for event
115             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
116                 hci_stack.substate++;
117             }
118         }
119     }
120 
121     // link key request
122     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
123         bt_flip_addr(addr, &packet[2]);
124         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
125         return;
126     }
127 
128     // pin code request
129     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
130         bt_flip_addr(addr, &packet[2]);
131         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
132     }
133 
134     hci_stack.event_packet_handler(packet, size);
135 }
136 
137 /** Register L2CAP handlers */
138 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
139     hci_stack.event_packet_handler = handler;
140 }
141 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
142     hci_stack.acl_packet_handler = handler;
143 }
144 
145 void hci_init(hci_transport_t *transport, void *config){
146 
147     // reference to use transport layer implementation
148     hci_stack.hci_transport = transport;
149 
150     // empty cmd buffer
151     hci_stack.hci_cmd_buffer = malloc(3+255);
152 
153     // set up state
154     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
155     hci_stack.state = HCI_STATE_INITIALIZING;
156     hci_stack.substate = 0;
157 
158     // higher level handler
159     hci_stack.event_packet_handler = dummy_handler;
160     hci_stack.acl_packet_handler = dummy_handler;
161 
162     // register packet handlers with transport
163     transport->register_event_packet_handler( event_handler);
164     transport->register_acl_packet_handler( acl_handler);
165 
166     // open low-level device
167     transport->open(config);
168 }
169 
170 int hci_power_control(HCI_POWER_MODE power_mode){
171     return 0;
172 }
173 
174 uint32_t hci_run(){
175     uint8_t micro_packet;
176     switch (hci_stack.state){
177         case HCI_STATE_INITIALIZING:
178             if (hci_stack.substate % 2) {
179                 // odd: waiting for command completion
180                 return 0;
181             }
182             if (hci_stack.num_cmd_packets == 0) {
183                 // cannot send command yet
184                 return 0;
185             }
186             switch (hci_stack.substate/2){
187                 case 0:
188                     hci_send_cmd(&hci_reset);
189                     break;
190                 case 1:
191                     // ca. 15 sec
192                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
193                     break;
194                 case 2:
195                     // done.
196                     hci_stack.state = HCI_STATE_WORKING;
197                     micro_packet = BTSTACK_EVENT_HCI_WORKING;
198                     hci_stack.event_packet_handler(&micro_packet, 1);
199                     break;
200                 default:
201                     break;
202             }
203             hci_stack.substate++;
204             break;
205         default:
206             break;
207     }
208 
209     // don't check for timetous yet
210     return 0;
211 }
212 
213 
214 int hci_send_acl_packet(uint8_t *packet, int size){
215     return hci_stack.hci_transport->send_acl_packet(packet, size);
216 }
217 
218 
219 /**
220  * pre: numcmds >= 0 - it's allowed to send a command to the controller
221  */
222 int hci_send_cmd(hci_cmd_t *cmd, ...){
223     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
224     hci_cmd_buffer[0] = cmd->opcode & 0xff;
225     hci_cmd_buffer[1] = cmd->opcode >> 8;
226     int pos = 3;
227 
228     va_list argptr;
229     va_start(argptr, cmd);
230     const char *format = cmd->format;
231     uint16_t word;
232     uint32_t longword;
233     uint8_t * ptr;
234     while (*format) {
235         switch(*format) {
236             case '1': //  8 bit value
237             case '2': // 16 bit value
238             case 'H': // hci_handle
239                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
240                 hci_cmd_buffer[pos++] = word & 0xff;
241                 if (*format == '2') {
242                     hci_cmd_buffer[pos++] = word >> 8;
243                 } else if (*format == 'H') {
244                     // TODO
245                 }
246                 break;
247             case '3':
248             case '4':
249                 longword = va_arg(argptr, uint32_t);
250                 // longword = va_arg(argptr, int);
251                 hci_cmd_buffer[pos++] = longword;
252                 hci_cmd_buffer[pos++] = longword >> 8;
253                 hci_cmd_buffer[pos++] = longword >> 16;
254                 if (*format == '4'){
255                     hci_cmd_buffer[pos++] = longword >> 24;
256                 }
257                 break;
258             case 'B': // bt-addr
259                 ptr = va_arg(argptr, uint8_t *);
260                 hci_cmd_buffer[pos++] = ptr[5];
261                 hci_cmd_buffer[pos++] = ptr[4];
262                 hci_cmd_buffer[pos++] = ptr[3];
263                 hci_cmd_buffer[pos++] = ptr[2];
264                 hci_cmd_buffer[pos++] = ptr[1];
265                 hci_cmd_buffer[pos++] = ptr[0];
266                 break;
267             case 'P': // c string passed as pascal string with leading 1-byte len
268                 ptr = va_arg(argptr, uint8_t *);
269                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
270                 pos += 16;
271                 break;
272             default:
273                 break;
274         }
275         format++;
276     };
277     va_end(argptr);
278     hci_cmd_buffer[2] = pos - 3;
279     // send packet
280     hci_stack.num_cmd_packets--;
281     return hci_stack.hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
282 }