xref: /btstack/src/hci_transport_h4.c (revision 198279761c8761868ffebbf9cbe94d970ebdea9b)
1 /*
2  * Copyright (C) 2009-2012 by Matthias Ringwald
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at [email protected]
34  *
35  */
36 
37 /*
38  *  hci_h4_transport.c
39  *
40  *  HCI Transport API implementation for basic H4 protocol over POSIX
41  *
42  *  Created by Matthias Ringwald on 4/29/09.
43  */
44 
45 #include "btstack-config.h"
46 
47 #include <termios.h>  /* POSIX terminal control definitions */
48 #include <fcntl.h>    /* File control definitions */
49 #include <unistd.h>   /* UNIX standard function definitions */
50 #include <stdio.h>
51 #include <string.h>
52 #include <pthread.h>
53 
54 #include "debug.h"
55 #include "hci.h"
56 #include "hci_transport.h"
57 #include "hci_dump.h"
58 
59 static int  h4_process(struct data_source *ds);
60 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
61 static      hci_uart_config_t *hci_uart_config;
62 
63 typedef enum {
64     H4_W4_PACKET_TYPE,
65     H4_W4_EVENT_HEADER,
66     H4_W4_ACL_HEADER,
67     H4_W4_PAYLOAD,
68 } H4_STATE;
69 
70 typedef struct hci_transport_h4 {
71     hci_transport_t transport;
72     data_source_t *ds;
73     int uart_fd;    // different from ds->fd for HCI reader thread
74     /* power management support, e.g. used by iOS */
75     timer_source_t sleep_timer;
76 } hci_transport_h4_t;
77 
78 
79 // single instance
80 static hci_transport_h4_t * hci_transport_h4 = NULL;
81 
82 static  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
83 
84 // packet reader state machine
85 static  H4_STATE h4_state;
86 static int bytes_to_read;
87 static int read_pos;
88 
89 static uint8_t hci_packet[1+HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data)
90 
91 static int    h4_open(void *transport_config){
92     hci_uart_config = (hci_uart_config_t*) transport_config;
93     struct termios toptions;
94     int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;
95     int fd = open(hci_uart_config->device_name, flags);
96     if (fd == -1)  {
97         perror("init_serialport: Unable to open port ");
98         perror(hci_uart_config->device_name);
99         return -1;
100     }
101 
102     if (tcgetattr(fd, &toptions) < 0) {
103         perror("init_serialport: Couldn't get term attributes");
104         return -1;
105     }
106 
107     speed_t brate = hci_uart_config->baudrate_init; // let you override switch below if needed
108     switch(hci_uart_config->baudrate_init) {
109         case 57600:  brate=B57600;  break;
110         case 115200: brate=B115200; break;
111 #ifdef B230400
112         case 230400: brate=B230400; break;
113 #endif
114 #ifdef B460800
115         case 460800: brate=B460800; break;
116 #endif
117 #ifdef B921600
118         case 921600: brate=B921600; break;
119 #endif
120     }
121     cfsetospeed(&toptions, brate);
122     cfsetispeed(&toptions, brate);
123 
124     // 8N1
125     toptions.c_cflag &= ~PARENB;
126     toptions.c_cflag &= ~CSTOPB;
127     toptions.c_cflag &= ~CSIZE;
128     toptions.c_cflag |= CS8;
129 
130     if (hci_uart_config->flowcontrol) {
131         // with flow control
132         toptions.c_cflag |= CRTSCTS;
133     } else {
134         // no flow control
135         toptions.c_cflag &= ~CRTSCTS;
136     }
137 
138     toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
139     toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
140 
141     toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
142     toptions.c_oflag &= ~OPOST; // make raw
143 
144     // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
145     toptions.c_cc[VMIN]  = 1;
146     toptions.c_cc[VTIME] = 0;
147 
148     if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
149         perror("init_serialport: Couldn't set term attributes");
150         return -1;
151     }
152 
153     // set up data_source
154     hci_transport_h4->ds = (data_source_t*) malloc(sizeof(data_source_t));
155     if (!hci_transport_h4->ds) return -1;
156     hci_transport_h4->uart_fd = fd;
157     hci_transport_h4->ds->fd = fd;
158     hci_transport_h4->ds->process = h4_process;
159     run_loop_add_data_source(hci_transport_h4->ds);
160 
161     // init state machine
162     bytes_to_read = 1;
163     h4_state = H4_W4_PACKET_TYPE;
164     read_pos = 0;
165 
166     // bring bluetooth module into defined state
167     int i;
168     uint8_t reset[] = { 0x01, 0x03, 0x0c, 0x00};
169     write(fd, &reset, sizeof(reset));
170     usleep(100000);     // 100 ms
171     write(fd, &reset, sizeof(reset));
172     usleep(100000);     // 100 ms
173     tcflush(fd, TCIOFLUSH);
174 
175     return 0;
176 }
177 
178 static int h4_close(void *transport_config){
179     // first remove run loop handler
180 	run_loop_remove_data_source(hci_transport_h4->ds);
181 
182     // close device
183     close(hci_transport_h4->ds->fd);
184 
185     // free struct
186     free(hci_transport_h4->ds);
187     hci_transport_h4->ds = NULL;
188     return 0;
189 }
190 
191 static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
192     if (hci_transport_h4->ds == NULL) return -1;
193     if (hci_transport_h4->uart_fd == 0) return -1;
194 
195     hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
196     char *data = (char*) packet;
197     int bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1);
198     while (bytes_written < 1) {
199         usleep(5000);
200         bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1);
201     };
202     while (size > 0) {
203         int bytes_written = write(hci_transport_h4->uart_fd, data, size);
204         if (bytes_written < 0) {
205             usleep(5000);
206             continue;
207         }
208         data += bytes_written;
209         size -= bytes_written;
210     }
211     return 0;
212 }
213 
214 static void   h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
215     packet_handler = handler;
216 }
217 
218 static void   h4_deliver_packet(void){
219     if (read_pos < 3) return; // sanity check
220     hci_dump_packet( hci_packet[0], 1, &hci_packet[1], read_pos-1);
221     packet_handler(hci_packet[0], &hci_packet[1], read_pos-1);
222 
223     h4_state = H4_W4_PACKET_TYPE;
224     read_pos = 0;
225     bytes_to_read = 1;
226 }
227 
228 static void h4_statemachine(void){
229     switch (h4_state) {
230 
231         case H4_W4_PACKET_TYPE:
232             if (hci_packet[0] == HCI_EVENT_PACKET){
233                 bytes_to_read = HCI_EVENT_HEADER_SIZE;
234                 h4_state = H4_W4_EVENT_HEADER;
235             } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){
236                 bytes_to_read = HCI_ACL_HEADER_SIZE;
237                 h4_state = H4_W4_ACL_HEADER;
238             } else {
239                 log_error("h4_process: invalid packet type 0x%02x\n", hci_packet[0]);
240                 read_pos = 0;
241                 bytes_to_read = 1;
242             }
243             break;
244 
245         case H4_W4_EVENT_HEADER:
246             bytes_to_read = hci_packet[2];
247             h4_state = H4_W4_PAYLOAD;
248             break;
249 
250         case H4_W4_ACL_HEADER:
251             bytes_to_read = READ_BT_16( hci_packet, 3);
252             h4_state = H4_W4_PAYLOAD;
253             break;
254 
255         case H4_W4_PAYLOAD:
256             h4_deliver_packet();
257             break;
258         default:
259             break;
260     }
261 }
262 
263 static int    h4_process(struct data_source *ds) {
264     if (hci_transport_h4->uart_fd == 0) return -1;
265 
266     int read_now = bytes_to_read;
267 
268     // read up to bytes_to_read data in
269     ssize_t bytes_read = read(hci_transport_h4->uart_fd, &hci_packet[read_pos], read_now);
270     // printf("h4_process: bytes read %u\n", bytes_read);
271     if (bytes_read < 0) {
272         return bytes_read;
273     }
274 
275     // hexdump(&hci_packet[read_pos], bytes_read);
276 
277     bytes_to_read -= bytes_read;
278     read_pos      += bytes_read;
279     if (bytes_to_read > 0) {
280         return 0;
281     }
282 
283     h4_statemachine();
284     return 0;
285 }
286 
287 static const char * h4_get_transport_name(void){
288     return "H4";
289 }
290 
291 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
292 }
293 
294 // get h4 singleton
295 hci_transport_t * hci_transport_h4_instance() {
296     if (hci_transport_h4 == NULL) {
297         hci_transport_h4 = (hci_transport_h4_t*)malloc( sizeof(hci_transport_h4_t));
298         hci_transport_h4->ds                                      = NULL;
299         hci_transport_h4->transport.open                          = h4_open;
300         hci_transport_h4->transport.close                         = h4_close;
301         hci_transport_h4->transport.send_packet                   = h4_send_packet;
302         hci_transport_h4->transport.register_packet_handler       = h4_register_packet_handler;
303         hci_transport_h4->transport.get_transport_name            = h4_get_transport_name;
304         hci_transport_h4->transport.set_baudrate                  = NULL;
305         hci_transport_h4->transport.can_send_packet_now           = NULL;
306     }
307     return (hci_transport_t *) hci_transport_h4;
308 }
309