]> git.itanic.dy.fi Git - log-plotter/blob - main.c
refactor data handling into data.c
[log-plotter] / main.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4
5 #include "options.h"
6 #include "baud.h"
7 #include "debug.h"
8 #include "data.h"
9
10 int main(int argc, char *argv[])
11 {
12         struct plotter_options options;
13         int fd, baud, ret = 0, out_fd = 0;
14
15         if (read_args(argc, argv, &options))
16                 return 1;
17
18         baud = options.baud_rate;
19         fd = open_at_baud(options.device_path, &baud);
20         if (fd < 0)
21                 return 1;
22
23         if (baud != options.baud_rate) {
24                 pr_err("Failed to set baudrate to %d, only got %d\n",
25                         options.baud_rate, baud);
26                 ret = 1;
27                 goto out;
28         }
29
30         if (options.output_path) {
31                 pr_debug("Opening %s for writing the log file\n",
32                         options.output_path);
33
34                 out_fd = open(options.output_path,
35                         O_CREAT | O_APPEND | O_WRONLY, 0664);
36                 if (out_fd < 0) {
37                         pr_err("Failed to open file %s for writing: %m\n",
38                                 options.output_path);
39                         ret = 1;
40                         goto out;
41                 }
42         }
43
44         read_data(fd, out_fd);
45
46 out:
47         close(fd);
48         return ret;
49 }