]> git.itanic.dy.fi Git - log-plotter/blobdiff - main.c
Fix read return value check
[log-plotter] / main.c
diff --git a/main.c b/main.c
index b4a4c8fb635b2f813e23f4e6a43a403ae12abbdd..6cfdd879d35692e9c5ccbcfa615ecb3dd249dc74 100644 (file)
--- a/main.c
+++ b/main.c
 #include <stdio.h>
 #include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/epoll.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
+#include "options.h"
 #include "baud.h"
+#include "debug.h"
 
-int main(int argc, char *argv[])
+/**
+ * Read data from a slow device
+ *
+ * return 1 when a complete NULL terminated line has been read
+ *
+ * return 0 when a partial line has been read and appended to the
+ *              buffer at @offset
+ *
+ * return negative on error
+ */
+static int read_log_line(int infd, char *buf, size_t bufsize, int *offset)
 {
-       int fd, baud, ret;
-       char *device;
-       char buf[256];
+       int ret;
+       int i;
 
-       if (argc < 2) {
-               printf("Usage: %s SERIAL_DEVICE\n", argv[0]);
-               return 1;
+       ret = read(infd, buf + *offset, bufsize - *offset - 1);
+       if (ret < 0) {
+               pr_err("read: %m\n");
+               return -1;
        }
 
-       baud = 128000;
-       device = argv[1];
+       if (ret == 0) {
+               pr_err("Read EOF, stopping\n");
+               return -1;
+       }
+       buf[*offset + ret] = 0;
 
-       fd = open_at_baud(device, &baud);
-       if (fd < 0)
-               return 1;
+       for (i = 0; i < ret; i++) {
+               if (buf[i + *offset] == '\n' ||
+                   buf[i + *offset] == '\r') {
+                       /*
+                        * Got a complete line when there is a newline
+                        * at the end. Remove the newline and possible
+                        * other junk, such as '\r'
+                        */
+                       buf[i + *offset] = 0;
+                       *offset = 0;
+
+                       return 1;
+               }
+
+               /*
+                * Fixme! Nothing guarantees that there isn't actually
+                * more data (a part of a new log entry perhaps) after
+                * the newline. So in rare cases (we are prevented
+                * from reading the serial line in very long time) we
+                * might lose data from the stream..
+                */
+       }
 
-       if (baud != 128000) {
-               printf("Failed to set baudrate to 128000, only got %d\n", baud);
-               close(fd);
+       *offset += ret;
+       return 0;
+}
+
+static int read_data(int infd, int outfd)
+{
+       struct epoll_event ev;
+       int epoll_fd;
+       int ret;
+       char buf[256];
+       int offset;
+
+       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 = read(fd, buf, sizeof(buf));
-               if (read < 0) {
-                       perror("read");
-                       break;
+               ret = epoll_wait(epoll_fd, &ev, 1, -1);
+               if (ret == 0)
+                       continue;
+
+               if (ret < 0) {
+                       pr_err("epoll: %m\n");
+                       return -1;
+               }
+
+               ret = read_log_line(infd, buf, sizeof(buf), &offset);
+               if (ret < 0)
+                       return ret;
+
+               if (ret == 0)
+                       continue;
+
+               if (outfd) {
+                       char newline = '\n';
+                       ret = write(outfd, buf, strlen(buf));
+                       if (read < 0) {
+                               pr_err("write: %m\n");
+                               break;
+                       }
+                       ret = write(outfd, &newline, 1);
+                       if (read < 0) {
+                               pr_err("write: %m\n");
+                               break;
+                       }
                }
 
-               if (read == 0)
-                       break;
+               pr_info("%s\n", buf);
+       }
+
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       struct plotter_options options;
+       int fd, baud, ret = 0, out_fd = 0;
+
+       if (read_args(argc, argv, &options))
+               return 1;
+
+       baud = options.baud_rate;
+       fd = open_at_baud(options.device_path, &baud);
+       if (fd < 0)
+               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;
+       }
+
+       if (options.output_path) {
+               pr_debug("Opening %s for writing the log file\n",
+                       options.output_path);
 
-               ret = write(1, buf, ret);
-               if (read < 0) {
-                       perror("write");
-                       break;
+               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;
 }