xref: /btstack/src/hci_transport_h4.c (revision 142538a136ccf72f2030efcf41ad04f10b3642f9)
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 "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     cfsetspeed(&toptions, brate);
122 
123     // 8N1
124     toptions.c_cflag &= ~PARENB;
125     toptions.c_cflag &= ~CSTOPB;
126     toptions.c_cflag &= ~CSIZE;
127     toptions.c_cflag |= CS8;
128 
129     if (hci_uart_config->flowcontrol) {
130         // with flow control
131         toptions.c_cflag |= CRTSCTS;
132     } else {
133         // no flow control
134         toptions.c_cflag &= ~CRTSCTS;
135     }
136 
137     toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
138     toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
139 
140     toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
141     toptions.c_oflag &= ~OPOST; // make raw
142 
143     // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
144     toptions.c_cc[VMIN]  = 1;
145     toptions.c_cc[VTIME] = 0;
146 
147     if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
148         perror("init_serialport: Couldn't set term attributes");
149         return -1;
150     }
151 
152     // set up data_source
153     hci_transport_h4->ds = malloc(sizeof(data_source_t));
154     if (!hci_transport_h4->ds) return -1;
155     hci_transport_h4->uart_fd = fd;
156     hci_transport_h4->ds->fd = fd;
157     hci_transport_h4->ds->process = h4_process;
158     run_loop_add_data_source(hci_transport_h4->ds);
159 
160     // init state machine
161     bytes_to_read = 1;
162     h4_state = H4_W4_PACKET_TYPE;
163     read_pos = 0;
164 
165     return 0;
166 }
167 
168 static int h4_close(void *transport_config){
169     // first remove run loop handler
170 	run_loop_remove_data_source(hci_transport_h4->ds);
171 
172     // close device
173     close(hci_transport_h4->ds->fd);
174 
175     // free struct
176     free(hci_transport_h4->ds);
177     hci_transport_h4->ds = NULL;
178     return 0;
179 }
180 
181 static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
182     if (hci_transport_h4->ds == NULL) return -1;
183     if (hci_transport_h4->uart_fd == 0) return -1;
184 
185     hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
186     char *data = (char*) packet;
187     int bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1);
188     while (bytes_written < 1) {
189         usleep(5000);
190         bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1);
191     };
192     while (size > 0) {
193         int bytes_written = write(hci_transport_h4->uart_fd, data, size);
194         if (bytes_written < 0) {
195             usleep(5000);
196             continue;
197         }
198         data += bytes_written;
199         size -= bytes_written;
200     }
201     return 0;
202 }
203 
204 static void   h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
205     packet_handler = handler;
206 }
207 
208 static void   h4_deliver_packet(void){
209     if (read_pos < 3) return; // sanity check
210     hci_dump_packet( hci_packet[0], 1, &hci_packet[1], read_pos-1);
211     packet_handler(hci_packet[0], &hci_packet[1], read_pos-1);
212 
213     h4_state = H4_W4_PACKET_TYPE;
214     read_pos = 0;
215     bytes_to_read = 1;
216 }
217 
218 static void h4_statemachine(void){
219     switch (h4_state) {
220 
221         case H4_W4_PACKET_TYPE:
222             if (hci_packet[0] == HCI_EVENT_PACKET){
223                 bytes_to_read = HCI_EVENT_HEADER_SIZE;
224                 h4_state = H4_W4_EVENT_HEADER;
225             } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){
226                 bytes_to_read = HCI_ACL_HEADER_SIZE;
227                 h4_state = H4_W4_ACL_HEADER;
228             } else {
229                 log_error("h4_process: invalid packet type 0x%02x\n", hci_packet[0]);
230                 read_pos = 0;
231                 bytes_to_read = 1;
232             }
233             break;
234 
235         case H4_W4_EVENT_HEADER:
236             bytes_to_read = hci_packet[2];
237             h4_state = H4_W4_PAYLOAD;
238             break;
239 
240         case H4_W4_ACL_HEADER:
241             bytes_to_read = READ_BT_16( hci_packet, 3);
242             h4_state = H4_W4_PAYLOAD;
243             break;
244 
245         case H4_W4_PAYLOAD:
246             h4_deliver_packet();
247             break;
248         default:
249             break;
250     }
251 }
252 
253 static int    h4_process(struct data_source *ds) {
254     if (hci_transport_h4->uart_fd == 0) return -1;
255 
256     int read_now = bytes_to_read;
257 
258     // read up to bytes_to_read data in
259     ssize_t bytes_read = read(hci_transport_h4->uart_fd, &hci_packet[read_pos], read_now);
260     // printf("h4_process: bytes read %u\n", bytes_read);
261     if (bytes_read < 0) {
262         return bytes_read;
263     }
264 
265     // hexdump(&hci_packet[read_pos], bytes_read);
266 
267     bytes_to_read -= bytes_read;
268     read_pos      += bytes_read;
269     if (bytes_to_read > 0) {
270         return 0;
271     }
272 
273     h4_statemachine();
274     return 0;
275 }
276 
277 static const char * h4_get_transport_name(void){
278     return "H4";
279 }
280 
281 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
282 }
283 
284 // get h4 singleton
285 hci_transport_t * hci_transport_h4_instance() {
286     if (hci_transport_h4 == NULL) {
287         hci_transport_h4 = malloc( sizeof(hci_transport_h4_t));
288         hci_transport_h4->ds                                      = NULL;
289         hci_transport_h4->transport.open                          = h4_open;
290         hci_transport_h4->transport.close                         = h4_close;
291         hci_transport_h4->transport.send_packet                   = h4_send_packet;
292         hci_transport_h4->transport.register_packet_handler       = h4_register_packet_handler;
293         hci_transport_h4->transport.get_transport_name            = h4_get_transport_name;
294         hci_transport_h4->transport.set_baudrate                  = NULL;
295         hci_transport_h4->transport.can_send_packet_now           = NULL;
296     }
297     return (hci_transport_t *) hci_transport_h4;
298 }
299