xref: /btstack/src/hci.c (revision 16833f0a4e9e562990f7384eae1771a5c962f5f0)
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 static void event_handler(uint8_t *packet, int size){
93     hci_stack.event_packet_handler(packet, size);
94 }
95 
96 /** Register L2CAP handlers */
97 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
98     hci_stack.event_packet_handler = handler;
99 }
100 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
101     hci_stack.acl_packet_handler = handler;
102 }
103 
104 void hci_init(hci_transport_t *transport, void *config){
105 
106     // reference to use transport layer implementation
107     hci_stack.hci_transport = transport;
108 
109     // empty cmd buffer
110     hci_stack.hci_cmd_buffer = malloc(3+255);
111 
112     // higher level handler
113     hci_stack.event_packet_handler = dummy_handler;
114     hci_stack.acl_packet_handler = dummy_handler;
115 
116     // register packet handlers with transport
117     transport->register_event_packet_handler( event_handler);
118     transport->register_acl_packet_handler( acl_handler);
119 
120     // open low-level device
121     transport->open(&config);
122 
123     // open unix socket
124 
125     // wait for connections
126 
127     // enter loop
128 
129     // handle events
130 }
131 
132 int hci_power_control(HCI_POWER_MODE power_mode){
133     return 0;
134 }
135 
136 void hci_run(){
137     while (1) {
138         //  construct file descriptor set to wait for
139         //  select
140 
141         // for each ready file in FD - call handle_data
142         sleep(1);
143     }
144 }
145 
146 
147 
148 
149 
150 int hci_send_acl_packet(uint8_t *packet, int size){
151     return hci_stack.hci_transport->send_acl_packet(packet, size);
152 }
153 
154 int hci_send_cmd(hci_cmd_t *cmd, ...){
155     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
156     hci_cmd_buffer[0] = cmd->opcode & 0xff;
157     hci_cmd_buffer[1] = cmd->opcode >> 8;
158     int pos = 3;
159 
160     va_list argptr;
161     va_start(argptr, cmd);
162     const char *format = cmd->format;
163     uint16_t word;
164     uint32_t longword;
165     uint8_t * ptr;
166     while (*format) {
167         switch(*format) {
168             case '1': //  8 bit value
169             case '2': // 16 bit value
170             case 'H': // hci_handle
171                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
172                 hci_cmd_buffer[pos++] = word & 0xff;
173                 if (*format == '2') {
174                     hci_cmd_buffer[pos++] = word >> 8;
175                 } else if (*format == 'H') {
176                     // TODO
177                 }
178                 break;
179             case '3':
180             case '4':
181                 longword = va_arg(argptr, uint32_t);
182                 // longword = va_arg(argptr, int);
183                 hci_cmd_buffer[pos++] = longword;
184                 hci_cmd_buffer[pos++] = longword >> 8;
185                 hci_cmd_buffer[pos++] = longword >> 16;
186                 if (*format == '4'){
187                     hci_cmd_buffer[pos++] = longword >> 24;
188                 }
189                 break;
190             case 'B': // bt-addr
191                 ptr = va_arg(argptr, uint8_t *);
192                 hci_cmd_buffer[pos++] = ptr[5];
193                 hci_cmd_buffer[pos++] = ptr[4];
194                 hci_cmd_buffer[pos++] = ptr[3];
195                 hci_cmd_buffer[pos++] = ptr[2];
196                 hci_cmd_buffer[pos++] = ptr[1];
197                 hci_cmd_buffer[pos++] = ptr[0];
198                 break;
199             case 'P': // c string passed as pascal string with leading 1-byte len
200                 ptr = va_arg(argptr, uint8_t *);
201                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
202                 pos += 16;
203                 break;
204             default:
205                 break;
206         }
207         format++;
208     };
209     va_end(argptr);
210     hci_cmd_buffer[2] = pos - 3;
211     // send packet
212     return hci_stack.hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
213 }