]> git.itanic.dy.fi Git - log-plotter/blobdiff - main.c
Implement state machine based architecture
[log-plotter] / main.c
diff --git a/main.c b/main.c
index 6189b7be34b7048123c441f8ea1c33e96c258741..9356d91b2ee7d7aa8097f6747d705c0a549dbe94 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,73 +1,31 @@
 #include <stdio.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <sys/epoll.h>
-#include <sys/types.h>
-#include <sys/stat.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"
 
-int read_data(int infd, int outfd)
-{
-       struct epoll_event ev;
-       int epoll_fd;
-       int ret;
-       char buf[256];
-
-       epoll_fd = epoll_create(1);
-       if (epoll_fd < 0) {
-               pr_err("Failed to create epoll socket: %m\n");
-               return -1;
-       }
-
-       ev.events = EPOLLIN;
-       ev.data.fd = infd;
-       if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, infd, &ev) == -1) {
-               pr_err("epoll_ctl: %m\n");
-               return 1;
-       }
-
-       while (1) {
-               ret = epoll_wait(epoll_fd, &ev, 1, -1);
-               if (ret == 0)
-                       continue;
-
-               if (ret < 0) {
-                       pr_err("epoll: %m\n");
-                       return -1;
-               }
-
-               ret = read(infd, buf, sizeof(buf));
-               if (read < 0) {
-                       pr_err("read: %m\n");
-                       break;
-               }
-
-               if (ret == 0) {
-                       pr_err("Read EOF, stopping\n");
-                       break;
-               }
-
-               ret = write(outfd, buf, ret);
-               if (read < 0) {
-                       pr_err("write: %m\n");
-                       break;
-               }
-       }
-
-       return 0;
-}
+struct log_plotter_status plotter_state;
 
 int main(int argc, char *argv[])
 {
        struct plotter_options options;
-       int fd, baud, ret = 0, out_fd = STDOUT_FILENO;
+       struct plotter_config cfg;
+       int fd, baud, ret = 0;
+
+       bzero(&cfg, sizeof(cfg));
 
        if (read_args(argc, argv, &options))
                return 1;
 
+       if (options.config_file_path)
+               populate_config_data_from_file(options.config_file_path, &cfg);
+
        baud = options.baud_rate;
        fd = open_at_baud(options.device_path, &baud);
        if (fd < 0)
@@ -80,22 +38,22 @@ int main(int argc, char *argv[])
                goto out;
        }
 
-       if (options.output_path) {
-               pr_debug("Opening %s for writing the log file\n",
-                       options.output_path);
+       init_data_parser(fd, &cfg);
+
+       while (plotter_state.system_status != SYSTEM_STATUS_NO_USB) {
+               poll_events(10000);
 
-               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;
+               if (plotter_state.old_system_status !=
+                       plotter_state.system_status) {
+                       pr_debug("Status changing from %s to %s",
+                               state_to_str(plotter_state.old_system_status),
+                               state_to_str(plotter_state.system_status));
+
+                       plotter_state.old_system_status =
+                               plotter_state.system_status;
                }
        }
 
-       read_data(fd, out_fd);
-
 out:
        close(fd);
        return ret;