#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <rtai.h>
#include <rtai_lxrt.h>
#include <sys/mman.h>
#include <rtai_fifos.h>
#include <fcntl.h>

typedef struct {
	char r1status[20];
	char r2status[20];
}Data1;


#define MSG_SIZE 20			// message size
#define MSG1 "ON ROOM1"
#define MSG2 "ON ROOM2"
#define MSG3 "OFF ROOM1"
#define MSG4 "OFF ROOM2"
#define MSG5 "WARMINGUP ROOM1"
#define MSG6 "WARMINGUP ROOM2"
#define MSG7 "WARNING ROOM1"
#define MSG8 "WARNING ROOM2"
#define MSG9 "NORMAL ROOM1"
#define MSG10 "NORMAL ROOM2"
#define MSG11 "UPDATING"
char receive[MSG_SIZE];


void error(const char *msg)
{
    perror(msg);
    exit(0);
}


int main(int argc, char *argv[])
{

	char *msg1 = "ACTIVATE1";	//first message   //second message
	char *msg2 = "ACTIVATE2";
	char *msg3 = "DEACTIVATE1";
	char *msg4 = "DEACTIVATE2";	//first message
	char *msg5 = "STATUS";
	char *msg6 = "ON";
	char *msg7 = "OFF";
	char *msg8 = "NORMAL";
	char *msg9 = "DETECTION";
	char *msg10 = "WARMING";
	char *msg11 = "UPDATING";

	char message[20];

	Data1 display;

				int i;

				int fd_fifo_in;
				fd_fifo_in = open("/dev/rtf/0",O_RDWR);

			    int sock, n;
			    unsigned int length;
			    struct sockaddr_in anybody, from;
			    int adata;
			    char receive[MSG_SIZE];
			    int boolval = 1;			// for a socket option

			   if (argc != 2)
			   {
				   printf("usage: %s port\n", argv[0]);
			       exit(1);
			   }

			   sock = socket(AF_INET, SOCK_DGRAM, 0); // Creates socket. Connectionless.
			   if (sock < 0)
				   error("socket");

			   // change socket permissions to allow broadcast
			   if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &boolval, sizeof(boolval)) < 0)
			     	{
			     		printf("error setting socket options\n");
			     		exit(-1);
			     	}

			   anybody.sin_family = AF_INET;		// symbol constant for Internet domain
			   anybody.sin_port = htons(atoi(argv[1]));				// port field
			   anybody.sin_addr.s_addr = inet_addr("10.3.52.255");	// broadcast address

			   length = sizeof(struct sockaddr_in);		// size of structure


			  while(1){

				  bzero(receive,MSG_SIZE);		// sets all values to zero. memset() could be used
				  bzero(message, 20);
				   read(fd_fifo_in,&adata,sizeof(adata));


					   if(adata == 1){
						   strcpy(message, msg5);
					   }

					   if(adata == 2){
						   strcpy(message, msg1);
					   }
					   if(adata == 3){
					   		strcpy(message, msg3);
					   }
					   if(adata == 4){
						   strcpy(message, msg2);
					   		}
					   if(adata == 5){
					   		strcpy(message, msg4);
					   		}



					   //send message to anyone there...
					   n = sendto(sock, message, strlen(message), 0,(const struct sockaddr *)&anybody,length);
					   if (n < 0)
						   error("Sendto");

			 		   // receive message

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

						   n = recvfrom(sock, receive, MSG_SIZE, 0, (struct sockaddr *)&from, &length);

						   if(strncmp(receive, MSG1, 8) == 0){
							   bzero(display.r1status, 20);
							   strcpy(display.r1status, msg6);
						   }
						   else if(strncmp(receive,MSG2, 8) == 0){
							   bzero(display.r2status, 20);
							   strcpy(display.r2status, msg6);
						   }
						   else if(strncmp(receive, MSG3, 9) == 0){
							   bzero(display.r1status, 20);
							   strcpy(display.r1status, msg7);
						   }

						   else if(strncmp(receive, MSG4, 9) == 0){
							   bzero(display.r2status, 20);
							   strcpy(display.r2status, msg7);
						   }
						   else if(strncmp(receive, MSG5, 16) == 0){
							   bzero(display.r1status, 20);
							   strcpy(display.r1status, msg10);
						   }
						   else if(strncmp(receive, MSG6, 16) == 0){
							   bzero(display.r2status, 20);
							   strcpy(display.r2status, msg10);
						   }
						   else if(strncmp(receive, MSG7, 13) == 0){
							   bzero(display.r1status, 20);
							   strcpy(display.r1status, msg9);
						   }
						   else if(strncmp(receive, MSG8, 13) == 0){
							   bzero(display.r2status, 20);
							   strcpy(display.r2status, msg9);
						   }
						   else if(strncmp(receive, MSG9, 12) == 0){
							   bzero(display.r1status, 20);
							   strcpy(display.r1status, msg8);
						   }
						   else if(strncmp(receive, MSG10, 12) == 0){
							   bzero(display.r2status, 20);
							   strcpy(display.r2status, msg8);
						   }
						   else{
							   bzero(display.r1status, 20);
							   bzero(display.r2status, 20);
							   strcpy(display.r1status, msg11);
							   strcpy(display.r2status, msg11);
						   }


					   }
					   system("clear");
					   printf("***********Security System***********\n(1) Turn on 1  (2) Turn on 2  (3) Turn off 1  (4) Turn off 2\nRoom1: %s\nRoom2: %s\n", display.r1status, display.r2status);
					  // printf("***********Security System***********\n(1) Turn on 1  (2) Turn on 2  (3) Turn off 1  (4) Turn off 2\nRoom1: %s\n", display.r1status);

				}

			  return 0;

}
