]> git.itanic.dy.fi Git - log-plotter/blob - main.c
d81f7333e25f062c28e15e4b43f3f9e77335c6f6
[log-plotter] / main.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/epoll.h>
4
5 #include "options.h"
6 #include "baud.h"
7
8 int main(int argc, char *argv[])
9 {
10         struct epoll_event ev;
11         struct plotter_options options;
12         int fd, baud, ret;
13         int epoll_fd;
14         char buf[256];
15
16
17         if (read_args(argc, argv, &options))
18                 return 1;
19
20         baud = options.baud_rate;
21         fd = open_at_baud(options.device_path, &baud);
22         if (fd < 0)
23                 return 1;
24
25         if (baud != options.baud_rate) {
26                 printf("Failed to set baudrate to %d, only got %d\n",
27                         options.baud_rate, baud);
28                 close(fd);
29                 return 1;
30         }
31
32         epoll_fd = epoll_create(1);
33
34         ev.events = EPOLLIN;
35         ev.data.fd = fd;
36         if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
37                 perror("epoll_ctl");
38                 return 1;
39         }
40
41         while (1) {
42                 ret = epoll_wait(epoll_fd, &ev, 1, -1);
43                 if (ret == 0)
44                         continue;
45
46                 if (ret < 0) {
47                         perror("epoll");
48                         return 1;
49                 }
50
51                 ret = read(fd, buf, sizeof(buf));
52                 if (read < 0) {
53                         perror("read");
54                         break;
55                 }
56
57                 if (ret == 0)
58                         break;
59
60                 ret = write(1, buf, ret);
61                 if (read < 0) {
62                         perror("write");
63                         break;
64                 }
65         }
66
67         close(fd);
68         return 0;
69 }