xref: /btstack/src/hci_transport_h4.c (revision 622d0de9a08d2321369ef28bb9d6eca08d90d9d7)
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_type, uint8_t *packet, uint16_t size);
67 static      hci_uart_config_t *hci_uart_config;
68 
69 static  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t 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[HCI_ACL_3DH5_SIZE]; // bigger than largest packet
77 
78 #ifdef TEST_LONG_INVALID_REMOTE_NAME
79 // test event with remote name of 255 bytes length
80 const uint8_t remoteNameEvent[] = { 0x07, 0xFF, 0x00, 0xC4, 0xC6, 0x65, 0x5A, 0x12, 0x00, 0x4D, 0x69,
81       0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66, 0x74, 0xAE, 0x20, 0x57, 0x69, 0x72, 0x65, 0x6C, 0x65,
82       0x73, 0x73, 0x20, 0x4E, 0x6F, 0x74, 0x65, 0x62, 0x6F, 0x6F, 0x6B, 0x20, 0x50, 0x72, 0x65,
83       0x73, 0x65, 0x6E, 0x74, 0x65, 0x72, 0x20, 0x4D, 0x6F, 0x75, 0x73, 0x65, 0x20, 0x38, 0x30,
84       0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85       0x00, 0x00, 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 };
98 const int remoteNameEventSize = sizeof(remoteNameEvent);
99 #endif
100 
101 
102 // prototypes
103 static int    h4_open(void *transport_config){
104     hci_uart_config = (hci_uart_config_t*) transport_config;
105     struct termios toptions;
106     int fd = open(hci_uart_config->device_name, O_RDWR | O_NOCTTY | O_NDELAY);
107     if (fd == -1)  {
108         perror("init_serialport: Unable to open port ");
109         perror(hci_uart_config->device_name);
110         return -1;
111     }
112 
113     if (tcgetattr(fd, &toptions) < 0) {
114         perror("init_serialport: Couldn't get term attributes");
115         return -1;
116     }
117     speed_t brate = hci_uart_config->baudrate; // let you override switch below if needed
118     switch(hci_uart_config->baudrate) {
119         case 57600:  brate=B57600;  break;
120         case 115200: brate=B115200; break;
121 #ifdef B230400
122         case 230400: brate=B230400; break;
123 #endif
124 #ifdef B460800
125         case 460800: brate=B460800; break;
126 #endif
127 #ifdef B921600
128         case 921600: brate=B921600; break;
129 #endif
130     }
131     cfsetispeed(&toptions, brate);
132     cfsetospeed(&toptions, brate);
133 
134     // 8N1
135     toptions.c_cflag &= ~PARENB;
136     toptions.c_cflag &= ~CSTOPB;
137     toptions.c_cflag &= ~CSIZE;
138     toptions.c_cflag |= CS8;
139 
140     if (hci_uart_config->flowcontrol) {
141         // with flow control
142         toptions.c_cflag |= CRTSCTS;
143     } else {
144         // no flow control
145         toptions.c_cflag &= ~CRTSCTS;
146     }
147 
148     toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
149     toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
150 
151     toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
152     toptions.c_oflag &= ~OPOST; // make raw
153 
154     // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
155     toptions.c_cc[VMIN]  = 1;
156     toptions.c_cc[VTIME] = 0;
157 
158     if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
159         perror("init_serialport: Couldn't set term attributes");
160         return -1;
161     }
162 
163     // set up data_source
164     hci_transport_h4->ds = malloc(sizeof(data_source_t));
165     if (!hci_transport_h4) return -1;
166     hci_transport_h4->ds->fd = fd;
167     hci_transport_h4->ds->process = h4_process;
168     run_loop_add_data_source(hci_transport_h4->ds);
169 
170     // init state machine
171     bytes_to_read = 1;
172     h4_state = H4_W4_PACKET_TYPE;
173     read_pos = 0;
174 
175     return 0;
176 }
177 
178 static int    h4_close(){
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     free(hci_transport_h4->ds);
185 
186     // free struct
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->ds->fd == 0) return -1;
194     hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
195     write(hci_transport_h4->ds->fd, &packet_type, 1);
196     char *data = (char*) packet;
197     while (size > 0) {
198         int bytes_written = write(hci_transport_h4->ds->fd, data, size);
199         if (bytes_written < 0) {
200             return bytes_written;
201         }
202         data += bytes_written;
203         size -= bytes_written;
204     }
205     return 0;
206 }
207 
208 static void   h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
209     packet_handler = handler;
210 }
211 
212 static int    h4_process(struct data_source *ds) {
213     if (hci_transport_h4->ds->fd == 0) return -1;
214 
215     // read up to bytes_to_read data in
216     int bytes_read = read(hci_transport_h4->ds->fd, &hci_packet[read_pos], bytes_to_read);
217     if (bytes_read < 0) {
218         return bytes_read;
219     }
220     bytes_to_read -= bytes_read;
221     read_pos      += bytes_read;
222     if (bytes_to_read > 0) {
223         return 0;
224     }
225 
226     // act
227     switch (h4_state) {
228 
229         case H4_W4_PACKET_TYPE:
230             if (hci_packet[0] == HCI_EVENT_PACKET){
231                 read_pos = 0;
232                 bytes_to_read = HCI_EVENT_PKT_HDR;
233                 h4_state = H4_W4_EVENT_HEADER;
234             } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){
235                 read_pos = 0;
236                 bytes_to_read = HCI_ACL_DATA_PKT_HDR;
237                 h4_state = H4_W4_ACL_HEADER;
238             } else {
239                 fprintf(stderr, "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[1];
247             h4_state = H4_W4_EVENT_PAYLOAD;
248             break;
249 
250         case H4_W4_EVENT_PAYLOAD:
251 
252 #ifdef TEST_LONG_INVALID_REMOTE_NAME
253             if (hci_packet[0] == HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE){
254                 hci_packet[1] = remoteNameEvent[1];
255                 memcpy( &hci_packet[9], &remoteNameEvent[9], remoteNameEventSize);
256                 read_pos = remoteNameEventSize;
257             }
258 #endif
259 			hci_dump_packet( HCI_EVENT_PACKET, 1, hci_packet, read_pos);
260             packet_handler(HCI_EVENT_PACKET, hci_packet, read_pos);
261             h4_state = H4_W4_PACKET_TYPE;
262             read_pos = 0;
263             bytes_to_read = 1;
264             break;
265 
266         case H4_W4_ACL_HEADER:
267             bytes_to_read = READ_BT_16( hci_packet, 2);
268             h4_state = H4_W4_ACL_PAYLOAD;
269             break;
270 
271         case H4_W4_ACL_PAYLOAD:
272             hci_dump_packet( HCI_ACL_DATA_PACKET, 1, hci_packet, read_pos);
273             packet_handler(HCI_ACL_DATA_PACKET, hci_packet, read_pos);
274             h4_state = H4_W4_PACKET_TYPE;
275             read_pos = 0;
276             bytes_to_read = 1;
277             break;
278     }
279     return 0;
280 }
281 
282 static const char * h4_get_transport_name(){
283     return "H4";
284 }
285 
286 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
287 }
288 
289 // get h4 singleton
290 hci_transport_t * hci_transport_h4_instance() {
291     if (hci_transport_h4 == NULL) {
292         hci_transport_h4 = malloc( sizeof(hci_transport_h4_t));
293         hci_transport_h4->ds                                      = NULL;
294         hci_transport_h4->transport.open                          = h4_open;
295         hci_transport_h4->transport.close                         = h4_close;
296         hci_transport_h4->transport.send_packet                   = h4_send_packet;
297         hci_transport_h4->transport.register_packet_handler       = h4_register_packet_handler;
298         hci_transport_h4->transport.get_transport_name            = h4_get_transport_name;
299     }
300     return (hci_transport_t *) hci_transport_h4;
301 }
302