]> git.itanic.dy.fi Git - log-plotter/blobdiff - main.c
main.c: Add missing endline to debug print
[log-plotter] / main.c
diff --git a/main.c b/main.c
index 065d9dc2b1f20a19ecaf5a07e95409866a16382a..f8272e665b84d71d700c9b859cf6adeaf9bac9ac 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,72 +1,58 @@
 #include <stdio.h>
 #include <unistd.h>
-#include <sys/epoll.h>
+#include <string.h>
 
-#include "options.h"
+#include "config.h"
 #include "baud.h"
-#include "debug.h"
+#include "trace.h"
+#include "data.h"
+#include "event.h"
+#include "plotter_status.h"
+
+struct log_plotter_status plotter_state;
 
 int main(int argc, char *argv[])
 {
-       struct epoll_event ev;
-       struct plotter_options options;
-       int fd, baud, ret;
-       int epoll_fd;
-       char buf[256];
+       struct plotter_config cfg;
+       int fd, baud, ret = 0;
 
+       bzero(&cfg, sizeof(cfg));
 
-       if (read_args(argc, argv, &options))
+       if (read_args(argc, argv, &cfg))
                return 1;
 
-       baud = options.baud_rate;
-       fd = open_at_baud(options.device_path, &baud);
+       if (cfg.config_file_path)
+               populate_config_data_from_file(cfg.config_file_path, &cfg);
+
+       baud = cfg.baudrate;
+       fd = open_at_baud(cfg.device_path, &baud);
        if (fd < 0)
                return 1;
 
-       if (baud != options.baud_rate) {
+       if (baud != cfg.baudrate) {
                pr_err("Failed to set baudrate to %d, only got %d\n",
-                       options.baud_rate, baud);
-               close(fd);
-               return 1;
+                       cfg.baudrate, baud);
+               ret = 1;
+               goto out;
        }
 
-       epoll_fd = epoll_create(1);
-
-       ev.events = EPOLLIN;
-       ev.data.fd = fd;
-       if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
-               pr_err("epoll_ctl: %m\n");
-               return 1;
-       }
+       init_data_parser(fd, &cfg);
 
-       while (1) {
-               ret = epoll_wait(epoll_fd, &ev, 1, -1);
-               if (ret == 0)
-                       continue;
+       while (plotter_state.system_status != SYSTEM_STATUS_NO_USB) {
+               poll_events(10000);
 
-               if (ret < 0) {
-                       pr_err("epoll: %m\n");
-                       return 1;
-               }
-
-               ret = read(fd, buf, sizeof(buf));
-               if (read < 0) {
-                       pr_err("read: %m\n");
-                       break;
-               }
-
-               if (ret == 0) {
-                       pr_err("Read EOF, stopping\n");
-                       break;
-               }
+               if (plotter_state.old_system_status !=
+                       plotter_state.system_status) {
+                       pr_debug("Status changing from %s to %s\n",
+                               state_to_str(plotter_state.old_system_status),
+                               state_to_str(plotter_state.system_status));
 
-               ret = write(1, buf, ret);
-               if (read < 0) {
-                       pr_err("write: %m\n");
-                       break;
+                       plotter_state.old_system_status =
+                               plotter_state.system_status;
                }
        }
 
+out:
        close(fd);
-       return 0;
+       return ret;
 }