#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main(int argc, char * argv[])
{
	int fd;
	struct pollfd  fds;
	struct timeval tv;
	char buffer[2];

	if (argc != 2) {
		fprintf(stderr, "usage: %s <gpio-file>\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	if ((fd = open(argv[1], O_RDONLY)) < 0) {
		perror(argv[1]);
		exit(EXIT_FAILURE);
	}
	while (1) {
		fds.fd = fd;
		fds.events = POLLPRI;
		if (poll(& fds, 1, -1) < 0) {
			perror("poll");
			break;
		}
		gettimeofday(& tv, NULL);
		lseek(fd, 0, SEEK_SET);
		if (read(fd, & buffer, 2) != 2) {
			perror("read");
			break;
		}
		buffer[1] = '\0';
		fprintf(stdout, "[%ld.%06ld]: %s\n", tv.tv_sec, tv.tv_usec, buffer);
	}
	close(fd);
	return EXIT_SUCCESS;
}


