/* * Copyright 2012 Digital Aggregates Corporation, Colorado, USA. * Licensed under the terms of your choice of the GPL. * Chip Overclock */ #include #include #include #include #include #include #include #include #include static void handler(int signum) { fprintf(stderr, "signal %d!\n", signum); } int main(void) { int fd; struct termios tios; struct sigaction action; sigset_t set; sigset_t was; struct itimerval timer; struct itimerval remaining; char buffer[32]; /* Configure serial port. */ if ((fd = open("/dev/ttyS2", O_RDWR)) < 0) { perror("open"); return 1; } if (!isatty(fd)) { errno = EINVAL; perror("isatty"); return 1; } if (tcgetattr(fd, &tios) < 0) { perror("tcgetattr"); return 1; } #if 1 tios.c_cc[VTIME] = 1; tios.c_cc[VMIN] = 32; #endif if (tcsetattr(fd, TCSANOW, &tios) < 0) { perror("tcsetattr"); return 1; } /* Install alarm signal handler. */ if (sigaction(SIGALRM, (const struct sigaction *) 0, &action)) { perror("sigaction"); return 1; } action.sa_handler = handler; action.sa_flags &= ~SA_RESTART; if (sigaction(SIGALRM, &action, (struct sigaction *) 0) < 0) { perror("sigaction"); return 1; } /* Insure that SIGALRM is not blocked. */ if (sigemptyset(&set) < 0) { perror("sigemptyset"); return 1; } if (sigaddset(&set, SIGALRM) < 0) { perror("sigaddset"); return 1; } if (sigprocmask(SIG_UNBLOCK, &set, (sigset_t *) 0) < 0) { perror("sigprocmask"); return 1; } /* Start an one-shot interval timer. */ timer.it_value.tv_sec = 5; timer.it_value.tv_usec = 0; timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 0; if (setitimer(ITIMER_REAL, &timer, &remaining) < 0) { perror("setitimer"); return 1; } /* Block on a read. */ if (read(fd, buffer, sizeof(buffer)) < 0) { perror("read"); return 1; } return 0; }