]> git.itanic.dy.fi Git - log-plotter/blob - main.c
main: Fix busy loop with reading
[log-plotter] / main.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/epoll.h>
4
5 #include "baud.h"
6
7 int main(int argc, char *argv[])
8 {
9         struct epoll_event ev;
10         int fd, baud, ret;
11         int epoll_fd;
12         char *device;
13         char buf[256];
14
15         if (argc < 2) {
16                 printf("Usage: %s SERIAL_DEVICE\n", argv[0]);
17                 return 1;
18         }
19
20         baud = 128000;
21         device = argv[1];
22
23         fd = open_at_baud(device, &baud);
24         if (fd < 0)
25                 return 1;
26
27         if (baud != 128000) {
28                 printf("Failed to set baudrate to 128000, only got %d\n", baud);
29                 close(fd);
30                 return 1;
31         }
32
33         epoll_fd = epoll_create(1);
34
35         ev.events = EPOLLIN;
36         ev.data.fd = fd;
37         if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
38                 perror("epoll_ctl");
39                 return 1;
40         }
41
42         while (1) {
43                 ret = epoll_wait(epoll_fd, &ev, 1, -1);
44                 if (ret == 0)
45                         continue;
46
47                 if (ret < 0) {
48                         perror("epoll");
49                         return 1;
50                 }
51
52                 ret = read(fd, buf, sizeof(buf));
53                 if (read < 0) {
54                         perror("read");
55                         break;
56                 }
57
58                 ret = write(1, buf, ret);
59                 if (read < 0) {
60                         perror("write");
61                         break;
62                 }
63         }
64
65         close(fd);
66         return 0;
67 }