/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 2003  by Matthew R. Stein                     	    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* Test module for jls process
 * Matthew R. Stein 6/18/2001
 * based roughly on frank-zappa test program from RTLinux examples
 */
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <rtl_fifo.h>
#include <rtl_time.h>

#include "../h/fifos.h"


#define BUFSIZE 70

char buf[BUFSIZE];

int main()
{
	fd_set rfds;
        struct timeval tv;
        int retval;
	int fd0, fd1, ctl, tsk, act;
	int n;
	int i;
	jlsMessageType msg;
	RciTimerEvent tev;
	/* Create Real-Time FIFOs to communicate with kernel module */
	if ((fd0 = open("/dev/rtf1", O_RDONLY)) < 0) {
		fprintf(stderr, "Error opening /dev/rtf1\n");
		exit(1);
	}

	if ((fd1 = open("/dev/rtf2", O_RDONLY)) < 0) {
		fprintf(stderr, "Error opening /dev/rtf2\n");
		exit(1);
	}


	if ((act = open("/dev/rtf3", O_WRONLY)) < 0) {
		fprintf(stderr, "Error opening /dev/rtf3\n");
		exit(1);
	}

	if ((tsk = open("/dev/rtf4", O_WRONLY)) < 0) {
		fprintf(stderr, "Error opening /dev/rtf3\n");
		exit(1);
	}

	if ((ctl = open("/dev/rtf5", O_RDONLY)) < 0) {
		fprintf(stderr, "Error opening /dev/rtf3\n");
		exit(1);
	}

	/* now start the tasks */
	msg.command = START_ALRM_MSG;
	msg.arg = 30;
	if (write(act, &msg, sizeof(msg)) < 0) {
		fprintf(stderr, "Can't send a command to RT-task\n");
		exit(1);
	}

	msg.command = START_BEAT_MSG;
	msg.arg = 60;
	if (write(act, &msg, sizeof(msg)) < 0) {
		fprintf(stderr, "Can't send a command to RT-task\n");
		exit(1);
	}


        /* Perform 20 reads and writes to the FIFOs to test */
	for (i = 0; i < 20; i++) {
		FD_ZERO(&rfds);
		FD_SET(fd0, &rfds);
		FD_SET(fd1, &rfds);
		FD_SET(ctl, &rfds);
		tv.tv_sec = 1;
		tv.tv_usec = 0;

		retval = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
		if (retval > 0) {
			if (FD_ISSET(fd0, &rfds)) {
				n = read(fd0, buf, BUFSIZE - 1);
				buf[n] = 0;
				printf("FIFO 1: %s\n", buf);
			}
			if (FD_ISSET(fd1, &rfds)) {
				n = read(fd1, buf, BUFSIZE - 1);
				buf[n] = 0;
				printf("FIFO 2: %s\n", buf);
			}
			if (FD_ISSET(ctl, &rfds)) {
				n = read(ctl, &tev, sizeof(tev));
				printf("Control Level FIFO: Req: %d\n", tev.req);
			}
		}


	}

	fprintf(stderr, "jls_test: now sending commands to stop RT-tasks\n");
	/* stop the tasks */
	msg.command = STOP_BEAT_MSG;
	msg.arg = 0;
	if (write(act, &msg, sizeof(msg)) < 0) {
		fprintf(stderr, "Can't send a command to RT-task\n");
		exit(1);
	}
	msg.command = STOP_ALRM_MSG;
	msg.arg = 0;
	if (write(act, &msg, sizeof(msg)) < 0) {
		fprintf(stderr, "Can't send a command to RT-task\n");
		exit(1);
	}
	return 0;
}
