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