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