/*
 * doorServer.c
 *
 *  Created on: Apr 29, 2015
 *      Author: kapvx4
 */



#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 <pthread.h>
#include <fcntl.h>
#define size 40
#define ROW 4
#define COL 4

char hostName[50];
struct hostent *myHost;
struct in_addr *addresses;
struct sockaddr_in server, from;
unsigned int length;
int sock, n;
char buffer[size] = " ";
socklen_t fromlen;
int boolval = 1;
unsigned short pnumber = 2000;
int rnum = 0;
char password[6];
char Oipaddress[10], Aipaddress[10];
int i;
int status = 0; //0 - clear 1- breach
char pin[6];
int fifo_in = 0;
char repeat[6];
char temp[6];
char string10[2];
pthread_t thread [1];
void error(const char *msg);



//pthread to get the password from the board
void get_password(void)
{
	fifo_in =(open("/dev/rtf/0",O_RDWR));
	while(1)
	{
	read(fifo_in, &password, 6*sizeof(password[0]));
	//strcpy(repeat, password); //save last password
	}

	pthread_exit(0);

}



int main (void)
{


	//get the host name
		gethostname(hostName,sizeof(hostName));

		//get host by name
		myHost = gethostbyname(hostName);

		//Print host information
		printf("Host Name: %s\n", myHost->h_name);
		printf("Host IP Addresses: \n");
		addresses = (struct in_addr*)myHost->h_addr_list[0];

		printf("%s \n ", inet_ntoa(*addresses));


		bzero((char*)&server, length);
			server.sin_family = AF_INET; //Symbol constant for internet domain
			server.sin_addr.s_addr = INADDR_ANY; //IP address of the machine on which
			printf("What is the port number?\n");
			scanf("%hu", &pnumber); //read unsigned short port number from the user
			server.sin_port = htons(pnumber); //port number**
			fromlen = sizeof(struct sockaddr_in); //size of structure
			from.sin_family = AF_INET;
			from.sin_port = htons(pnumber);
			from.sin_addr.s_addr = inet_addr("10.3.52.255"); //Broadcast Address




		sock = socket(AF_INET, SOCK_DGRAM, 0); //Creates Socket Connectionless.

		if(sock <0)
			error("socket");

		//bind the socket to the address of the host and the port number
		if(bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
			error("Error Binding");

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

		}
		//create pthread
		pthread_create(&thread[0], NULL,(void*)&get_password, NULL);


		while(1)
			{
			sleep(1);
				bzero(buffer, size);
				//receive a message
				n = recvfrom(sock, buffer, size, 0, (struct sockaddr*)&from, &fromlen);
				if(n < 0)
					error("recvfrom");
				printf("Message Received: ");
				printf("%s\n", buffer);

				if (strncmp("ALL CLEAR", buffer,  9)==0) //if all clear proceed as normal
				{
					status = 0; //clear
						//get password from the fifo
						//strcpy(password,"#12345");
					printf("Password Entered:  %s\n", password);
					if(strcmp(password, repeat) !=  0 && (password[0] != '\0')) // while password is not equal to last password.
					{
					//Send password entry to the client
					strcpy(buffer,password);//Copy message into the buffer
					from.sin_addr.s_addr = inet_addr("10.3.52.255");
					n = sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr*)&from, fromlen); //Send message to client
					if(n < 0)
						error("There was an error sending to the client\n"); //report an error
						}
					strcpy(repeat, password);//save last password
					password[0] = '\0';//clear password
				}

		if (strncmp("SECURITY BREACH", buffer,  15)==0) //if all clear proceed as normal
						{
			from.sin_addr.s_addr = inet_addr("10.3.52.255");
				char warning[size] = "LOCKDOWN\n";
				//Broadcast warning to all systems
				strcpy(buffer,warning);//Copy message into the buffer
			n = sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr*)&from, fromlen); //Send message to client
			if(n < 0)
			error("There was an error sending to the client\n"); //report an error

			//check password entries
			if(strcmp(password, repeat) !=  0 && (password[0] != '\0')) // while password is not equal to last password.
			{
			//Send password entry to the client
			strcpy(buffer,password);//Copy message into the buffer
			from.sin_addr.s_addr = inet_addr("10.3.52.255");
			n = sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr*)&from, fromlen); //Send message to client
			if(n < 0)
			error("There was an error sending to the client\n"); //report an error
			}
			strcpy(repeat, password);//save last password
			password[0] = '\0';//clear password
			}


		if ((strncmp("LOCKDOWN", buffer, 8)==0)) //Lock down, dont allow entry of passwords
		{

			status = 1; //breach
			printf("All doors have been locked. \n");

		}

		if(strncmp("OPENDOOR", buffer, 8)==0)
		{
			printf("The door has been opened.\n");


		}



}//end while loop
}//end main function

