]> git.itanic.dy.fi Git - log-plotter/blob - main.c
main: Refactor data reading out from main
[log-plotter] / main.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <sys/epoll.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7
8 #include "options.h"
9 #include "baud.h"
10 #include "debug.h"
11
12 int read_data(int infd, int outfd)
13 {
14         struct epoll_event ev;
15         int epoll_fd;
16         int ret;
17         char buf[256];
18
19         epoll_fd = epoll_create(1);
20         if (epoll_fd < 0) {
21                 pr_err("Failed to create epoll socket: %m\n");
22                 return -1;
23         }
24
25         ev.events = EPOLLIN;
26         ev.data.fd = infd;
27         if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, infd, &ev) == -1) {
28                 pr_err("epoll_ctl: %m\n");
29                 return 1;
30         }
31
32         while (1) {
33                 ret = epoll_wait(epoll_fd, &ev, 1, -1);
34                 if (ret == 0)
35                         continue;
36
37                 if (ret < 0) {
38                         pr_err("epoll: %m\n");
39                         return -1;
40                 }
41
42                 ret = read(infd, buf, sizeof(buf));
43                 if (read < 0) {
44                         pr_err("read: %m\n");
45                         break;
46                 }
47
48                 if (ret == 0) {
49                         pr_err("Read EOF, stopping\n");
50                         break;
51                 }
52
53                 ret = write(outfd, buf, ret);
54                 if (read < 0) {
55                         pr_err("write: %m\n");
56                         break;
57                 }
58         }
59
60         return 0;
61 }
62
63 int main(int argc, char *argv[])
64 {
65         struct plotter_options options;
66         int fd, baud, ret = 0, out_fd = STDOUT_FILENO;
67
68         if (read_args(argc, argv, &options))
69                 return 1;
70
71         baud = options.baud_rate;
72         fd = open_at_baud(options.device_path, &baud);
73         if (fd < 0)
74                 return 1;
75
76         if (baud != options.baud_rate) {
77                 pr_err("Failed to set baudrate to %d, only got %d\n",
78                         options.baud_rate, baud);
79                 ret = 1;
80                 goto out;
81         }
82
83         if (options.output_path) {
84                 pr_debug("Opening %s for writing the log file\n",
85                         options.output_path);
86
87                 out_fd = open(options.output_path,
88                         O_CREAT | O_APPEND | O_WRONLY, 0664);
89                 if (out_fd < 0) {
90                         pr_err("Failed to open file %s for writing: %m\n",
91                                 options.output_path);
92                         ret = 1;
93                         goto out;
94                 }
95         }
96
97         read_data(fd, out_fd);
98
99 out:
100         close(fd);
101         return ret;
102 }