#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 "control.h"
#include "mbuff.h"

#define BUFSIZE 70

char buf[BUFSIZE];

/* the contents of shared memory may change at any time, thus volatile */
volatile char * shm1;

int main()
{
	fd_set rfds;
        struct timeval tv;
        int retval;
	int fd0, fd1, ctl;
	int n;
	int i;
	robot_JLS_type *robot_JLS;
	struct my_msg_struct msg;


	shm1 = (volatile char*) mbuff_alloc("control",1024);
	robot_JLS = (robot_JLS_type *)shm1;

	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 ((ctl = open("/dev/rtf3", O_WRONLY)) < 0) {
		fprintf(stderr, "Error opening /dev/rtf3\n");
		exit(1);
	}

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

	/*
	for (i = 0; i < 10; i++) {
		FD_ZERO(&rfds);
		FD_SET(fd0, &rfds);
		FD_SET(fd1, &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);
			}
		}


	}
	*/
	for (i = 0; i < 10; i++) {

	  printf("Joint positions:  %d %d %d %d %d %d\n",
		 robot_JLS->pos[0],robot_JLS->pos[1],robot_JLS->pos[2],
		 robot_JLS->pos[3],robot_JLS->pos[4],robot_JLS->pos[5],
		 robot_JLS->pos[6]);
	  sleep(1);
	}        
	fprintf(stderr, "control_app: now sending commands to stop RT-tasks\n");
	/* stop the tasks */
	msg.command = STOP_TASK;
	msg.task = 0;
	if (write(ctl, &msg, sizeof(msg)) < 0) {
		fprintf(stderr, "Can't send a command to RT-task\n");
		exit(1);
	}
	for (i = 0; i < 10; i++) {

	  printf("Joint positions:  %d %d %d %d %d %d\n",
		 robot_JLS->pos[0],robot_JLS->pos[1],robot_JLS->pos[2],
		 robot_JLS->pos[3],robot_JLS->pos[4],robot_JLS->pos[5],
		 robot_JLS->pos[6]);
	  sleep(1);
	}        
	fprintf(stderr, "control_app: now sending commands to re-start RT-tasks\n");
	/* Restart the task */
	msg.command = START_TASK;
	msg.task = 0;
	if (write(ctl, &msg, sizeof(msg)) < 0) {
		fprintf(stderr, "Can't send a command to RT-task\n");
		exit(1);
	}

	for (i = 0; i < 10; i++) {

	  printf("Joint positions:  %d %d %d %d %d %d\n",
		 robot_JLS->pos[0],robot_JLS->pos[1],robot_JLS->pos[2],
		 robot_JLS->pos[3],robot_JLS->pos[4],robot_JLS->pos[5],
		 robot_JLS->pos[6]);
	  sleep(1);
	}        

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

	mbuff_free("control",(void*)shm1);
	return 0;
}
