#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/mman.h>

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <linux/unistd.h>
#include <linux/ukevent.h>

#define _syscall2(type,name,type1,arg1,type2,arg2) \
type name (type1 arg1, type2 arg2) \
{\
	return syscall(__NR_##name, arg1, arg2);\
}

#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
type name (type1 arg1, type2 arg2, type3 arg3) \
{\
	return syscall(__NR_##name, arg1, arg2, arg3);\
}

#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
{\
	return syscall(__NR_##name, arg1, arg2, arg3, arg4);\
}

#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
	  type5,arg5) \
type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
{\
	return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5);\
}

#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
	  type5,arg5,type6,arg6) \
type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, type6 arg6) \
{\
	return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6);\
}

_syscall3(int, kevent_init, struct kevent_ring *, arg1, unsigned int, arg2, unsigned int, arg3);
_syscall4(int, kevent_ctl, int, arg1, unsigned int, argv2, unsigned int, argv3, void *, argv4);
_syscall6(int, kevent_get_events, int, arg1, unsigned int, argv2, unsigned int, argv3, struct timespec, argv4, void *, argv5, unsigned, arg6);
_syscall5(int, kevent_wait, int, arg1, unsigned int, arg2, unsigned int, argv3, struct timespec, argv4, unsigned int, argv5);
_syscall3(int, kevent_commit, int, arg1, unsigned int, arg2, unsigned int, arg3);

#define ulog(f, a...) fprintf(stderr, "%8lu: "f, time(NULL), ##a)
#define ulog_err(f, a...) ulog(f ": %s [%d].\n", ##a, strerror(errno), errno)

static void rb_usage(char *p)
{
	ulog("Usage: %s -t sec -T nsec -o oneshot -n ring_max_num -r init_event_num -h\n", p);
}

int main(int argc, char *argv[])
{
	int ch, fd, err, oneshot;
	unsigned int i, ready_num, tm_sec, tm_nsec, ring_num;
	char buf[4096];
	struct ukevent *uk;
	struct kevent_ring *ring;

	oneshot = 0;
	ring_num = 10;
	tm_sec = 3;
	tm_nsec = 0;
	ready_num = 1;

	while ((ch = getopt(argc, argv, "r:t:T:o:n:h")) > 0) {
		switch (ch) {
			case 'r':
				ready_num = atoi(optarg);
				break;
			case 'n':
				ring_num = atoi(optarg);
				break;
			case 't':
				tm_sec = atoi(optarg);
				break;
			case 'T':
				tm_nsec = atoi(optarg);
				break;
			case 'o':
				oneshot = atoi(optarg);
				break;
			default:
				rb_usage(argv[0]);
				return -1;
		}
	}

	ring = malloc(sizeof(struct kevent_ring) + ring_num*sizeof(struct ukevent));
	if (!ring)
		return -ENOMEM;
	memset(ring, 0, sizeof(struct kevent_ring) + ring_num*sizeof(struct ukevent));

	fd = kevent_init(ring, ring_num, 0);
	if (fd == -1) {
		ulog_err("Failed create kevent control block: ring: %p, ring_num: %d", ring, ring_num);
		return -1;
	}

	memset(buf, 0, sizeof(buf));

	for (i=0; i<ready_num; ++i) {
		uk = (struct ukevent *)buf;
		uk->event = KEVENT_TIMER_FIRED;
		uk->type = KEVENT_TIMER;
		if (oneshot)
			uk->req_flags |= KEVENT_REQ_ONESHOT;
		uk->user[0] = i;
		uk->id.raw[0] = tm_sec;
		uk->id.raw[1] = tm_nsec+i;

		err = kevent_ctl(fd, KEVENT_CTL_ADD, 1, uk);
		if (err < 0) {
			ulog_err("Failed to perform control operation: oneshot: %d, sec: %u, nsec: %u", 
					oneshot, tm_sec, tm_nsec);
			close(fd);
			return err;
		}
		if (err) {
			ulog("%d: %016llx: ret_flags: 0x%x, ret_data: %u %d.\n", 
					i, uk->id.raw_u64, 
					uk->ret_flags, uk->ret_data[0], (int)uk->ret_data[1]);
		}
	}

	ready_num = 2;
	while (1) {
		unsigned int idx, num;
		struct timespec ts;

		ts.tv_sec = time(NULL) + 1;
		ts.tv_nsec = 0;

		ulog("going to wait: kidx: %u, uidx: %u, over: %u, ready_num: %u.\n", 
				ring->ring_kidx, idx, ring->ring_over, ready_num);
		err = kevent_wait(fd, ready_num, idx, ts, KEVENT_FLAGS_ABSTIME);
		if (err < 0) {
			if (errno != EAGAIN) {
				ulog_err("Failed to perform control operation: oneshot: %d, sec: %u, nsec: %u", 
						oneshot, tm_sec, tm_nsec);
				close(fd);
				return err;
			}
			idx = 0;
			ready_num = 1;
		}
		ulog("ready: kidx: %u, uidx: %u, over: %u, ready_num: %u, err: %d.\n", 
			ring->ring_kidx, idx, ring->ring_over, ready_num, err);

		if (err <= 0)
			continue;

		num = err;
		for (i=0; i<num; ++i) {
			struct ukevent *uk = &ring->event[idx];
			
			ulog(" %2d: %08x.%08x - %08x\n", 
				i, uk->id.raw[0], uk->id.raw[1], uk->ret_flags);

			if (++idx >= ring_num)
				idx = 0;
		}
		err = kevent_commit(fd, idx, ring->ring_over);
		ulog("commit: uidx: %u, num: %u, over: %u, committed: %d.\n", idx, num, ring->ring_over, err);
	}

	close(fd);
	return 0;
}
