xref: /btstack/src/hci_transport_h4.c (revision c6448b674257c22afbe6eb884223ac56c367a4bf)
1 /*
2  * Copyright (C) 2009 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  *
17  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 /*
33  *  hci_h4_transport.c
34  *
35  *  HCI Transport API implementation for basic H4 protocol
36  *
37  *  Created by Matthias Ringwald on 4/29/09.
38  */
39 #include <termios.h>  /* POSIX terminal control definitions */
40 #include <fcntl.h>    /* File control definitions */
41 #include <unistd.h>   /* UNIX standard function definitions */
42 #include <stdio.h>
43 #include <string.h>
44 
45 #include "hci.h"
46 #include "hci_transport.h"
47 #include "hci_dump.h"
48 
49 typedef enum {
50     H4_W4_PACKET_TYPE,
51     H4_W4_EVENT_HEADER,
52     H4_W4_EVENT_PAYLOAD,
53     H4_W4_ACL_HEADER,
54     H4_W4_ACL_PAYLOAD
55 } H4_STATE;
56 
57 typedef struct hci_transport_h4 {
58     hci_transport_t transport;
59     data_source_t *ds;
60 } hci_transport_h4_t;
61 
62 // single instance
63 static hci_transport_h4_t * hci_transport_h4 = NULL;
64 
65 static int  h4_process(struct data_source *ds);
66 static void dummy_handler(uint8_t *packet, int size);
67 static      hci_uart_config_t *hci_uart_config;
68 
69 static  void (*event_packet_handler)(uint8_t *packet, int size) = dummy_handler;
70 static  void (*acl_packet_handler)  (uint8_t *packet, int size) = dummy_handler;
71 
72 // packet reader state machine
73 static  H4_STATE h4_state;
74 static int bytes_to_read;
75 static int read_pos;
76 // static uint8_t hci_event_buffer[255+2]; // maximal payload + 2 bytes header
77 static uint8_t hci_packet[400]; // bigger than largest packet
78 
79 #ifdef TEST_LONG_INVALID_REMOTE_NAME
80 // test event with remote name of 255 bytes length
81 const uint8_t remoteNameEvent[] = { 0x07, 0xFF, 0x00, 0xC4, 0xC6, 0x65, 0x5A, 0x12, 0x00, 0x4D, 0x69,
82       0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66, 0x74, 0xAE, 0x20, 0x57, 0x69, 0x72, 0x65, 0x6C, 0x65,
83       0x73, 0x73, 0x20, 0x4E, 0x6F, 0x74, 0x65, 0x62, 0x6F, 0x6F, 0x6B, 0x20, 0x50, 0x72, 0x65,
84       0x73, 0x65, 0x6E, 0x74, 0x65, 0x72, 0x20, 0x4D, 0x6F, 0x75, 0x73, 0x65, 0x20, 0x38, 0x30,
85       0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98       0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
99 const int remoteNameEventSize = sizeof(remoteNameEvent);
100 #endif
101 
102 
103 // prototypes
104 static int    h4_open(void *transport_config){
105     hci_uart_config = (hci_uart_config_t*) transport_config;
106     struct termios toptions;
107     int fd = open(hci_uart_config->device_name, O_RDWR | O_NOCTTY | O_NDELAY);
108     if (fd == -1)  {
109         perror("init_serialport: Unable to open port ");
110         perror(hci_uart_config->device_name);
111         return -1;
112     }
113 
114     if (tcgetattr(fd, &toptions) < 0) {
115         perror("init_serialport: Couldn't get term attributes");
116         return -1;
117     }
118     speed_t brate = hci_uart_config->baudrate; // let you override switch below if needed
119     switch(hci_uart_config->baudrate) {
120         case 57600:  brate=B57600;  break;
121         case 115200: brate=B115200; break;
122 #ifdef B230400
123         case 230400: brate=B230400; break;
124 #endif
125 #ifdef B460800
126         case 460800: brate=B460800; break;
127 #endif
128 #ifdef B921600
129         case 921600: brate=B921600; break;
130 #endif
131     }
132     cfsetispeed(&toptions, brate);
133     cfsetospeed(&toptions, brate);
134 
135     // 8N1
136     toptions.c_cflag &= ~PARENB;
137     toptions.c_cflag &= ~CSTOPB;
138     toptions.c_cflag &= ~CSIZE;
139     toptions.c_cflag |= CS8;
140 
141     if (hci_uart_config->flowcontrol) {
142         // with flow control
143         toptions.c_cflag |= CRTSCTS;
144     } else {
145         // no flow control
146         toptions.c_cflag &= ~CRTSCTS;
147     }
148 
149     toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
150     toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
151 
152     toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
153     toptions.c_oflag &= ~OPOST; // make raw
154 
155     // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
156     toptions.c_cc[VMIN]  = 1;
157     toptions.c_cc[VTIME] = 0;
158 
159     if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
160         perror("init_serialport: Couldn't set term attributes");
161         return -1;
162     }
163 
164     // set up data_source
165     hci_transport_h4->ds = malloc(sizeof(data_source_t));
166     if (!hci_transport_h4) return -1;
167     hci_transport_h4->ds->fd = fd;
168     hci_transport_h4->ds->process = h4_process;
169     run_loop_add_data_source(hci_transport_h4->ds);
170 
171     // init state machine
172     bytes_to_read = 1;
173     h4_state = H4_W4_PACKET_TYPE;
174     read_pos = 0;
175 
176     return 0;
177 }
178 
179 static int    h4_close(){
180     // first remove run loop handler
181 	run_loop_remove_data_source(hci_transport_h4->ds);
182 
183     // close device
184     close(hci_transport_h4->ds->fd);
185     free(hci_transport_h4->ds);
186 
187     // free struct
188     hci_transport_h4->ds = NULL;
189     return 0;
190 }
191 
192 static int    h4_send_cmd_packet(uint8_t *packet, int size){
193     if (hci_transport_h4->ds == NULL) return -1;
194     if (hci_transport_h4->ds->fd == 0) return -1;
195     char *data = (char*) packet;
196     char packet_type = HCI_COMMAND_DATA_PACKET;
197 
198     hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
199 
200     write(hci_transport_h4->ds->fd, &packet_type, 1);
201     while (size > 0) {
202         int bytes_written = write(hci_transport_h4->ds->fd, data, size);
203         if (bytes_written < 0) {
204             return bytes_written;
205         }
206         data += bytes_written;
207         size -= bytes_written;
208     }
209     return 0;
210 }
211 
212 static int    h4_send_acl_packet(uint8_t *packet, int size){
213     if (hci_transport_h4->ds->fd == 0) return -1;
214 
215     char *data = (char*) packet;
216     char packet_type = HCI_ACL_DATA_PACKET;
217 
218     hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
219 
220     write(hci_transport_h4->ds->fd, &packet_type, 1);
221     while (size > 0) {
222         int bytes_written = write(hci_transport_h4->ds->fd, data, size);
223         if (bytes_written < 0) {
224             return bytes_written;
225         }
226         data += bytes_written;
227         size -= bytes_written;
228     }
229     return 0;
230 }
231 
232 static void   h4_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
233     event_packet_handler = handler;
234 }
235 
236 static void   h4_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
237     acl_packet_handler = handler;
238 }
239 
240 static int    h4_process(struct data_source *ds) {
241     if (hci_transport_h4->ds->fd == 0) return -1;
242 
243     // read up to bytes_to_read data in
244     int bytes_read = read(hci_transport_h4->ds->fd, &hci_packet[read_pos], bytes_to_read);
245     if (bytes_read < 0) {
246         return bytes_read;
247     }
248     bytes_to_read -= bytes_read;
249     read_pos      += bytes_read;
250     if (bytes_to_read > 0) {
251         return 0;
252     }
253 
254     // act
255     switch (h4_state) {
256 
257         case H4_W4_PACKET_TYPE:
258             if (hci_packet[0] == HCI_EVENT_PACKET){
259                 read_pos = 0;
260                 bytes_to_read = HCI_EVENT_PKT_HDR;
261                 h4_state = H4_W4_EVENT_HEADER;
262             } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){
263                 read_pos = 0;
264                 bytes_to_read = HCI_ACL_DATA_PKT_HDR;
265                 h4_state = H4_W4_ACL_HEADER;
266             } else {
267                 fprintf(stderr, "h4_process: invalid packet type 0x%02x\n", hci_packet[0]);
268                 read_pos = 0;
269                 bytes_to_read = 1;
270             }
271             break;
272 
273         case H4_W4_EVENT_HEADER:
274             bytes_to_read = hci_packet[1];
275             h4_state = H4_W4_EVENT_PAYLOAD;
276             break;
277 
278         case H4_W4_EVENT_PAYLOAD:
279 
280 #ifdef TEST_LONG_INVALID_REMOTE_NAME
281             if (hci_packet[0] == HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE){
282                 hci_packet[1] = remoteNameEvent[1];
283                 memcpy( &hci_packet[9], &remoteNameEvent[9], remoteNameEventSize);
284                 read_pos = remoteNameEventSize;
285             }
286 #endif
287 			hci_dump_packet( HCI_EVENT_PACKET, 1, hci_packet, read_pos);
288             event_packet_handler(hci_packet, read_pos);
289             h4_state = H4_W4_PACKET_TYPE;
290             read_pos = 0;
291             bytes_to_read = 1;
292             break;
293 
294         case H4_W4_ACL_HEADER:
295             bytes_to_read = READ_BT_16( hci_packet, 2);
296             h4_state = H4_W4_ACL_PAYLOAD;
297             break;
298 
299         case H4_W4_ACL_PAYLOAD:
300             hci_dump_packet( HCI_ACL_DATA_PACKET, 1, hci_packet, read_pos);
301             acl_packet_handler(hci_packet, read_pos);
302             h4_state = H4_W4_PACKET_TYPE;
303             read_pos = 0;
304             bytes_to_read = 1;
305             break;
306     }
307     return 0;
308 }
309 
310 static const char * h4_get_transport_name(){
311     return "H4";
312 }
313 
314 static void dummy_handler(uint8_t *packet, int size){
315 }
316 
317 // get h4 singleton
318 hci_transport_t * hci_transport_h4_instance() {
319     if (hci_transport_h4 == NULL) {
320         hci_transport_h4 = malloc( sizeof(hci_transport_h4_t));
321         hci_transport_h4->ds                                      = NULL;
322         hci_transport_h4->transport.open                          = h4_open;
323         hci_transport_h4->transport.close                         = h4_close;
324         hci_transport_h4->transport.send_cmd_packet               = h4_send_cmd_packet;
325         hci_transport_h4->transport.send_acl_packet               = h4_send_acl_packet;
326         hci_transport_h4->transport.register_event_packet_handler = h4_register_event_packet_handler;
327         hci_transport_h4->transport.register_acl_packet_handler   = h4_register_acl_packet_handler;
328         hci_transport_h4->transport.get_transport_name            = h4_get_transport_name;
329     }
330     return (hci_transport_t *) hci_transport_h4;
331 }
332