14 * Read data from a slow device
16 * return 1 when a complete NULL terminated line has been read
18 * return 0 when a partial line has been read and appended to the
21 * return negative on error
23 static int read_log_line(int infd, char *buf, size_t bufsize, int *offset)
28 ret = read(infd, buf + *offset, bufsize - *offset - 1);
35 pr_err("Read EOF, stopping\n");
38 buf[*offset + ret] = 0;
40 for (i = 0; i < ret; i++) {
41 if (buf[i + *offset] == '\n' ||
42 buf[i + *offset] == '\r') {
44 * Got a complete line when there is a newline
45 * at the end. Remove the newline and possible
46 * other junk, such as '\r'
55 * Fixme! Nothing guarantees that there isn't actually
56 * more data (a part of a new log entry perhaps) after
57 * the newline. So in rare cases (we are prevented
58 * from reading the serial line in very long time) we
59 * might lose data from the stream..
67 static int read_data(int infd, int outfd)
69 struct epoll_event ev;
75 epoll_fd = epoll_create(1);
77 pr_err("Failed to create epoll socket: %m\n");
83 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, infd, &ev) == -1) {
84 pr_err("epoll_ctl: %m\n");
89 ret = epoll_wait(epoll_fd, &ev, 1, -1);
94 pr_err("epoll: %m\n");
98 ret = read_log_line(infd, buf, sizeof(buf), &offset);
107 ret = write(outfd, buf, strlen(buf));
109 pr_err("write: %m\n");
112 ret = write(outfd, &newline, 1);
114 pr_err("write: %m\n");
119 pr_info("%s\n", buf);
125 int main(int argc, char *argv[])
127 struct plotter_options options;
128 int fd, baud, ret = 0, out_fd = 0;
130 if (read_args(argc, argv, &options))
133 baud = options.baud_rate;
134 fd = open_at_baud(options.device_path, &baud);
138 if (baud != options.baud_rate) {
139 pr_err("Failed to set baudrate to %d, only got %d\n",
140 options.baud_rate, baud);
145 if (options.output_path) {
146 pr_debug("Opening %s for writing the log file\n",
147 options.output_path);
149 out_fd = open(options.output_path,
150 O_CREAT | O_APPEND | O_WRONLY, 0664);
152 pr_err("Failed to open file %s for writing: %m\n",
153 options.output_path);
159 read_data(fd, out_fd);