]> git.itanic.dy.fi Git - log-plotter/blobdiff - main.c
refactor data handling into data.c
[log-plotter] / main.c
diff --git a/main.c b/main.c
index b4a4c8fb635b2f813e23f4e6a43a403ae12abbdd..51e52605dafc2590a9cd160da1f2d014e0516d98 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,49 +1,49 @@
 #include <stdio.h>
 #include <unistd.h>
+#include <fcntl.h>
 
+#include "options.h"
 #include "baud.h"
+#include "debug.h"
+#include "data.h"
 
 int main(int argc, char *argv[])
 {
-       int fd, baud, ret;
-       char *device;
-       char buf[256];
+       struct plotter_options options;
+       int fd, baud, ret = 0, out_fd = 0;
 
-       if (argc < 2) {
-               printf("Usage: %s SERIAL_DEVICE\n", argv[0]);
+       if (read_args(argc, argv, &options))
                return 1;
-       }
-
-       baud = 128000;
-       device = argv[1];
 
-       fd = open_at_baud(device, &baud);
+       baud = options.baud_rate;
+       fd = open_at_baud(options.device_path, &baud);
        if (fd < 0)
                return 1;
 
-       if (baud != 128000) {
-               printf("Failed to set baudrate to 128000, only got %d\n", baud);
-               close(fd);
-               return 1;
+       if (baud != options.baud_rate) {
+               pr_err("Failed to set baudrate to %d, only got %d\n",
+                       options.baud_rate, baud);
+               ret = 1;
+               goto out;
        }
 
-       while (1) {
-               ret = read(fd, buf, sizeof(buf));
-               if (read < 0) {
-                       perror("read");
-                       break;
-               }
-
-               if (read == 0)
-                       break;
-
-               ret = write(1, buf, ret);
-               if (read < 0) {
-                       perror("write");
-                       break;
+       if (options.output_path) {
+               pr_debug("Opening %s for writing the log file\n",
+                       options.output_path);
+
+               out_fd = open(options.output_path,
+                       O_CREAT | O_APPEND | O_WRONLY, 0664);
+               if (out_fd < 0) {
+                       pr_err("Failed to open file %s for writing: %m\n",
+                               options.output_path);
+                       ret = 1;
+                       goto out;
                }
        }
 
+       read_data(fd, out_fd);
+
+out:
        close(fd);
-       return 0;
+       return ret;
 }