xref: /btstack/src/hci.c (revision 22909952ea76c45a8db20409374b80e7e0204e4c)
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 hexdump(uint8_t *data, int size){
68     int i;
69     for (i=0; i<size;i++){
70         printf("%02X ", data[i]);
71     }
72     printf("\n");
73 }
74 
75 #if 0
76 static void *hci_daemon_thread(void *arg){
77     printf("HCI Daemon started\n");
78     hci_run(transport, &config);
79     return NULL;
80 }
81 #endif
82 
83 /**
84  * Handler called by HCI transport
85  */
86 static void dummy_handler(uint8_t *packet, int size){
87 }
88 
89 static void acl_handler(uint8_t *packet, int size){
90     hci_stack.acl_packet_handler(packet, size);
91 }
92 
93 static void event_handler(uint8_t *packet, int size){
94 
95     if ( COMMAND_COMPLETE_EVENT(packet, hci_reset) ) {
96         // reset done, write page timeout
97         hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec
98         return;
99     }
100 
101     if ( COMMAND_COMPLETE_EVENT(packet, hci_write_page_timeout) ) {
102         uint8_t micro_packet = 100;
103         hci_stack.event_packet_handler(&micro_packet, 1);
104         return;
105     }
106 
107     hci_stack.event_packet_handler(packet, size);
108 }
109 
110 /** Register L2CAP handlers */
111 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
112     hci_stack.event_packet_handler = handler;
113 }
114 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
115     hci_stack.acl_packet_handler = handler;
116 }
117 
118 void hci_init(hci_transport_t *transport, void *config){
119 
120     // reference to use transport layer implementation
121     hci_stack.hci_transport = transport;
122 
123     // empty cmd buffer
124     hci_stack.hci_cmd_buffer = malloc(3+255);
125 
126     // higher level handler
127     hci_stack.event_packet_handler = dummy_handler;
128     hci_stack.acl_packet_handler = dummy_handler;
129 
130     // register packet handlers with transport
131     transport->register_event_packet_handler( event_handler);
132     transport->register_acl_packet_handler( acl_handler);
133 
134     // open low-level device
135     transport->open(config);
136 
137     // open unix socket
138 
139     // wait for connections
140 
141     // enter loop
142 
143     // handle events
144 }
145 
146 int hci_power_control(HCI_POWER_MODE power_mode){
147     return 0;
148 }
149 
150 void hci_run(){
151 
152     // send hci reset
153     hci_send_cmd(&hci_reset);
154 
155 #if 0
156     while (1) {
157         //  construct file descriptor set to wait for
158         //  select
159 
160         // for each ready file in FD - call handle_data
161         sleep(1);
162     }
163 #endif
164 }
165 
166 
167 
168 
169 
170 int hci_send_acl_packet(uint8_t *packet, int size){
171     return hci_stack.hci_transport->send_acl_packet(packet, size);
172 }
173 
174 int hci_send_cmd(hci_cmd_t *cmd, ...){
175     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
176     hci_cmd_buffer[0] = cmd->opcode & 0xff;
177     hci_cmd_buffer[1] = cmd->opcode >> 8;
178     int pos = 3;
179 
180     va_list argptr;
181     va_start(argptr, cmd);
182     const char *format = cmd->format;
183     uint16_t word;
184     uint32_t longword;
185     uint8_t * ptr;
186     while (*format) {
187         switch(*format) {
188             case '1': //  8 bit value
189             case '2': // 16 bit value
190             case 'H': // hci_handle
191                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
192                 hci_cmd_buffer[pos++] = word & 0xff;
193                 if (*format == '2') {
194                     hci_cmd_buffer[pos++] = word >> 8;
195                 } else if (*format == 'H') {
196                     // TODO
197                 }
198                 break;
199             case '3':
200             case '4':
201                 longword = va_arg(argptr, uint32_t);
202                 // longword = va_arg(argptr, int);
203                 hci_cmd_buffer[pos++] = longword;
204                 hci_cmd_buffer[pos++] = longword >> 8;
205                 hci_cmd_buffer[pos++] = longword >> 16;
206                 if (*format == '4'){
207                     hci_cmd_buffer[pos++] = longword >> 24;
208                 }
209                 break;
210             case 'B': // bt-addr
211                 ptr = va_arg(argptr, uint8_t *);
212                 hci_cmd_buffer[pos++] = ptr[5];
213                 hci_cmd_buffer[pos++] = ptr[4];
214                 hci_cmd_buffer[pos++] = ptr[3];
215                 hci_cmd_buffer[pos++] = ptr[2];
216                 hci_cmd_buffer[pos++] = ptr[1];
217                 hci_cmd_buffer[pos++] = ptr[0];
218                 break;
219             case 'P': // c string passed as pascal string with leading 1-byte len
220                 ptr = va_arg(argptr, uint8_t *);
221                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
222                 pos += 16;
223                 break;
224             default:
225                 break;
226         }
227         format++;
228     };
229     va_end(argptr);
230     hci_cmd_buffer[2] = pos - 3;
231     // send packet
232     return hci_stack.hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
233 }