xref: /btstack/src/hci_transport_h4.c (revision 11c41d51aec0f6b1c42fee9f5d4ea359c4e7a562)
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_cmd_packet(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     char *data = (char*) packet;
195     char packet_type = HCI_COMMAND_DATA_PACKET;
196 
197     hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
198 
199     write(hci_transport_h4->ds->fd, &packet_type, 1);
200     while (size > 0) {
201         int bytes_written = write(hci_transport_h4->ds->fd, data, size);
202         if (bytes_written < 0) {
203             return bytes_written;
204         }
205         data += bytes_written;
206         size -= bytes_written;
207     }
208     return 0;
209 }
210 
211 static int    h4_send_acl_packet(uint8_t *packet, int size){
212     if (hci_transport_h4->ds->fd == 0) return -1;
213 
214     char *data = (char*) packet;
215     char packet_type = HCI_ACL_DATA_PACKET;
216 
217     hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
218 
219     write(hci_transport_h4->ds->fd, &packet_type, 1);
220     while (size > 0) {
221         int bytes_written = write(hci_transport_h4->ds->fd, data, size);
222         if (bytes_written < 0) {
223             return bytes_written;
224         }
225         data += bytes_written;
226         size -= bytes_written;
227     }
228     return 0;
229 }
230 
231 static void   h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
232     packet_handler = handler;
233 }
234 
235 static int    h4_process(struct data_source *ds) {
236     if (hci_transport_h4->ds->fd == 0) return -1;
237 
238     // read up to bytes_to_read data in
239     int bytes_read = read(hci_transport_h4->ds->fd, &hci_packet[read_pos], bytes_to_read);
240     if (bytes_read < 0) {
241         return bytes_read;
242     }
243     bytes_to_read -= bytes_read;
244     read_pos      += bytes_read;
245     if (bytes_to_read > 0) {
246         return 0;
247     }
248 
249     // act
250     switch (h4_state) {
251 
252         case H4_W4_PACKET_TYPE:
253             if (hci_packet[0] == HCI_EVENT_PACKET){
254                 read_pos = 0;
255                 bytes_to_read = HCI_EVENT_PKT_HDR;
256                 h4_state = H4_W4_EVENT_HEADER;
257             } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){
258                 read_pos = 0;
259                 bytes_to_read = HCI_ACL_DATA_PKT_HDR;
260                 h4_state = H4_W4_ACL_HEADER;
261             } else {
262                 fprintf(stderr, "h4_process: invalid packet type 0x%02x\n", hci_packet[0]);
263                 read_pos = 0;
264                 bytes_to_read = 1;
265             }
266             break;
267 
268         case H4_W4_EVENT_HEADER:
269             bytes_to_read = hci_packet[1];
270             h4_state = H4_W4_EVENT_PAYLOAD;
271             break;
272 
273         case H4_W4_EVENT_PAYLOAD:
274 
275 #ifdef TEST_LONG_INVALID_REMOTE_NAME
276             if (hci_packet[0] == HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE){
277                 hci_packet[1] = remoteNameEvent[1];
278                 memcpy( &hci_packet[9], &remoteNameEvent[9], remoteNameEventSize);
279                 read_pos = remoteNameEventSize;
280             }
281 #endif
282 			hci_dump_packet( HCI_EVENT_PACKET, 1, hci_packet, read_pos);
283             packet_handler(HCI_EVENT_PACKET, hci_packet, read_pos);
284             h4_state = H4_W4_PACKET_TYPE;
285             read_pos = 0;
286             bytes_to_read = 1;
287             break;
288 
289         case H4_W4_ACL_HEADER:
290             bytes_to_read = READ_BT_16( hci_packet, 2);
291             h4_state = H4_W4_ACL_PAYLOAD;
292             break;
293 
294         case H4_W4_ACL_PAYLOAD:
295             hci_dump_packet( HCI_ACL_DATA_PACKET, 1, hci_packet, read_pos);
296             packet_handler(HCI_ACL_DATA_PACKET, hci_packet, read_pos);
297             h4_state = H4_W4_PACKET_TYPE;
298             read_pos = 0;
299             bytes_to_read = 1;
300             break;
301     }
302     return 0;
303 }
304 
305 static const char * h4_get_transport_name(){
306     return "H4";
307 }
308 
309 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
310 }
311 
312 // get h4 singleton
313 hci_transport_t * hci_transport_h4_instance() {
314     if (hci_transport_h4 == NULL) {
315         hci_transport_h4 = malloc( sizeof(hci_transport_h4_t));
316         hci_transport_h4->ds                                      = NULL;
317         hci_transport_h4->transport.open                          = h4_open;
318         hci_transport_h4->transport.close                         = h4_close;
319         hci_transport_h4->transport.send_cmd_packet               = h4_send_cmd_packet;
320         hci_transport_h4->transport.send_acl_packet               = h4_send_acl_packet;
321         hci_transport_h4->transport.register_packet_handler       = h4_register_packet_handler;
322         hci_transport_h4->transport.get_transport_name            = h4_get_transport_name;
323     }
324     return (hci_transport_t *) hci_transport_h4;
325 }
326