]> git.itanic.dy.fi Git - log-plotter/blob - baud.c
baud: Add missing newline to error print
[log-plotter] / baud.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <linux/termios.h>
7 #include <unistd.h>
8
9 /*
10  * HACK: declare the ioctl function by hand...
11  *
12  * We cannot include <sys/ioctl.h> as it will eventually include
13  * <termios.h>, which will conflict with <linux/temios.h>. As we
14  * really need the latter one AND the ioctl, just declare it by hand
15  * here so that we get to use it...
16  */
17 int ioctl(int d, int request, ...);
18
19 /**
20  * Open a serial device and adjust the baud rate to give (arbitrary)
21  * baud rate. The actual baud rate is stored back to the @baud
22  * variable. This might differ from the requested one as the
23  * underlying hardware may not support all possible baud
24  * rates.
25  *
26  * Returns the file descriptor that was opened.
27  */
28 int open_at_baud(const char *device, int *baud)
29 {
30         struct termios2 tios;
31         int fd;
32
33         fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
34
35         if (fd == -1) {
36                 fprintf(stderr, "error opening %s: %m\n",       device);
37                 return fd;
38         }
39
40         if (ioctl(fd, TCGETS2, &tios)) {
41                 perror("TCGETS2");
42                 goto err;
43         }
44
45         tios.c_cflag &= ~CBAUD;
46         tios.c_cflag |= BOTHER;
47         tios.c_ispeed = *baud;
48         tios.c_ospeed = *baud;
49
50         if (ioctl(fd, TCSETS2, &tios)) {
51                 perror("TCSETS2");
52                 goto err;
53         }
54
55         if (ioctl(fd, TCGETS2, &tios)) {
56                 perror("TCGETS2");
57                 goto err;
58         }
59
60         *baud = tios.c_ospeed;
61
62         return fd;
63 err:
64         close(fd);
65
66         return -1;
67 }