xref: /btstack/src/hci_transport_h4.c (revision 1713bcea3a49b492b9437ccc3270646a8b9d602e)
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 
44 #include "hci.h"
45 #include "hci_transport_h4.h"
46 #include "hci_dump.h"
47 
48 typedef enum {
49     H4_W4_PACKET_TYPE,
50     H4_W4_EVENT_HEADER,
51     H4_W4_EVENT_PAYLOAD,
52     H4_W4_ACL_HEADER,
53     H4_W4_ACL_PAYLOAD
54 } H4_STATE;
55 
56 typedef struct hci_transport_h4 {
57     hci_transport_t transport;
58     data_source_t *ds;
59 } hci_transport_h4_t;
60 
61 // single instance
62 static hci_transport_h4_t * hci_transport_h4 = NULL;
63 
64 static int  h4_process(struct data_source *ds);
65 static void dummy_handler(uint8_t *packet, int size);
66 static      hci_uart_config_t *hci_uart_config;
67 
68 static  void (*event_packet_handler)(uint8_t *packet, int size) = dummy_handler;
69 static  void (*acl_packet_handler)  (uint8_t *packet, int size) = dummy_handler;
70 
71 // packet reader state machine
72 static  H4_STATE h4_state;
73 static int bytes_to_read;
74 static int read_pos;
75 // static uint8_t hci_event_buffer[255+2]; // maximal payload + 2 bytes header
76 static uint8_t hci_packet[400]; // bigger than largest packet
77 
78 // prototypes
79 static int    h4_open(void *transport_config){
80     hci_uart_config = (hci_uart_config_t*) transport_config;
81     struct termios toptions;
82     int fd = open(hci_uart_config->device_name, O_RDWR | O_NOCTTY | O_NDELAY);
83     if (fd == -1)  {
84         perror("init_serialport: Unable to open port ");
85         perror(hci_uart_config->device_name);
86         return -1;
87     }
88 
89     if (tcgetattr(fd, &toptions) < 0) {
90         perror("init_serialport: Couldn't get term attributes");
91         return -1;
92     }
93     speed_t brate = hci_uart_config->baudrate; // let you override switch below if needed
94     switch(hci_uart_config->baudrate) {
95         case 57600:  brate=B57600;  break;
96         case 115200: brate=B115200; break;
97 #ifdef B230400
98         case 230400: brate=B230400; break;
99 #endif
100 #ifdef B460800
101         case 460800: brate=B460800; break;
102 #endif
103 #ifdef B921600
104         case 921600: brate=B921600; break;
105 #endif
106     }
107     cfsetispeed(&toptions, brate);
108     cfsetospeed(&toptions, brate);
109 
110     // 8N1
111     toptions.c_cflag &= ~PARENB;
112     toptions.c_cflag &= ~CSTOPB;
113     toptions.c_cflag &= ~CSIZE;
114     toptions.c_cflag |= CS8;
115 
116     if (hci_uart_config->flowcontrol) {
117         // with flow control
118         toptions.c_cflag |= CRTSCTS;
119     } else {
120         // no flow control
121         toptions.c_cflag &= ~CRTSCTS;
122     }
123 
124     toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
125     toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
126 
127     toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
128     toptions.c_oflag &= ~OPOST; // make raw
129 
130     // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
131     toptions.c_cc[VMIN]  = 1;
132     toptions.c_cc[VTIME] = 0;
133 
134     if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
135         perror("init_serialport: Couldn't set term attributes");
136         return -1;
137     }
138 
139     // set up data_source
140     hci_transport_h4->ds = malloc(sizeof(data_source_t));
141     if (!hci_transport_h4) return -1;
142     hci_transport_h4->ds->fd = fd;
143     hci_transport_h4->ds->process = h4_process;
144     run_loop_add_data_source(hci_transport_h4->ds);
145 
146     // init state machine
147     bytes_to_read = 1;
148     h4_state = H4_W4_PACKET_TYPE;
149     read_pos = 0;
150 
151     return 0;
152 }
153 
154 static int    h4_close(){
155     // first remove run loop handler
156 	run_loop_remove_data_source(hci_transport_h4->ds);
157 
158     // close device
159     close(hci_transport_h4->ds->fd);
160     free(hci_transport_h4->ds);
161 
162     // free struct
163     hci_transport_h4->ds = NULL;
164     return 0;
165 }
166 
167 static int    h4_send_cmd_packet(uint8_t *packet, int size){
168     if (hci_transport_h4->ds == NULL) return -1;
169     if (hci_transport_h4->ds->fd == 0) return -1;
170     char *data = (char*) packet;
171     char packet_type = HCI_COMMAND_DATA_PACKET;
172 
173     hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
174 
175     write(hci_transport_h4->ds->fd, &packet_type, 1);
176     while (size > 0) {
177         int bytes_written = write(hci_transport_h4->ds->fd, data, size);
178         if (bytes_written < 0) {
179             return bytes_written;
180         }
181         data += bytes_written;
182         size -= bytes_written;
183     }
184     return 0;
185 }
186 
187 static int    h4_send_acl_packet(uint8_t *packet, int size){
188     if (hci_transport_h4->ds->fd == 0) return -1;
189 
190     char *data = (char*) packet;
191     char packet_type = HCI_ACL_DATA_PACKET;
192 
193     hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
194 
195     write(hci_transport_h4->ds->fd, &packet_type, 1);
196     while (size > 0) {
197         int bytes_written = write(hci_transport_h4->ds->fd, data, size);
198         if (bytes_written < 0) {
199             return bytes_written;
200         }
201         data += bytes_written;
202         size -= bytes_written;
203     }
204     return 0;
205 }
206 
207 static void   h4_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
208     event_packet_handler = handler;
209 }
210 
211 static void   h4_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
212     acl_packet_handler = handler;
213 }
214 
215 static int    h4_process(struct data_source *ds) {
216     if (hci_transport_h4->ds->fd == 0) return -1;
217 
218     // read up to bytes_to_read data in
219     int bytes_read = read(hci_transport_h4->ds->fd, &hci_packet[read_pos], bytes_to_read);
220     if (bytes_read < 0) {
221         return bytes_read;
222     }
223     bytes_to_read -= bytes_read;
224     read_pos      += bytes_read;
225     if (bytes_to_read > 0) {
226         return 0;
227     }
228 
229     // act
230     switch (h4_state) {
231         case H4_W4_PACKET_TYPE:
232             if (hci_packet[0] == HCI_EVENT_PACKET){
233                 read_pos = 0;
234                 bytes_to_read = HCI_EVENT_PKT_HDR;
235                 h4_state = H4_W4_EVENT_HEADER;
236             } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){
237                 read_pos = 0;
238                 bytes_to_read = HCI_ACL_DATA_PKT_HDR;
239                 h4_state = H4_W4_ACL_HEADER;
240             } else {
241             }
242             break;
243         case H4_W4_EVENT_HEADER:
244             bytes_to_read = hci_packet[1];
245             h4_state = H4_W4_EVENT_PAYLOAD;
246             break;
247         case H4_W4_EVENT_PAYLOAD:
248 			hci_dump_packet( HCI_EVENT_PACKET, 1, hci_packet, read_pos);
249             event_packet_handler(hci_packet, read_pos);
250             h4_state = H4_W4_PACKET_TYPE;
251             read_pos = 0;
252             bytes_to_read = 1;
253             break;
254         case H4_W4_ACL_HEADER:
255             bytes_to_read = READ_BT_16( hci_packet, 2);
256             h4_state = H4_W4_ACL_PAYLOAD;
257             break;
258         case H4_W4_ACL_PAYLOAD:
259             hci_dump_packet( HCI_ACL_DATA_PACKET, 1, hci_packet, read_pos);
260 
261             acl_packet_handler(hci_packet, read_pos);
262             h4_state = H4_W4_PACKET_TYPE;
263             read_pos = 0;
264             bytes_to_read = 1;
265             break;
266     }
267     return 0;
268 }
269 
270 static const char * h4_get_transport_name(){
271     return "H4";
272 }
273 
274 static void dummy_handler(uint8_t *packet, int size){
275 }
276 
277 // get h4 singleton
278 hci_transport_t * hci_transport_h4_instance() {
279     if (hci_transport_h4 == NULL) {
280         hci_transport_h4 = malloc( sizeof(hci_transport_h4_t));
281         hci_transport_h4->ds                                      = NULL;
282         hci_transport_h4->transport.open                          = h4_open;
283         hci_transport_h4->transport.close                         = h4_close;
284         hci_transport_h4->transport.send_cmd_packet               = h4_send_cmd_packet;
285         hci_transport_h4->transport.send_acl_packet               = h4_send_acl_packet;
286         hci_transport_h4->transport.register_event_packet_handler = h4_register_event_packet_handler;
287         hci_transport_h4->transport.register_acl_packet_handler   = h4_register_acl_packet_handler;
288         hci_transport_h4->transport.get_transport_name            = h4_get_transport_name;
289     }
290     return (hci_transport_t *) hci_transport_h4;
291 }
292