#include 	<sys/time.h>
#include 	<sys/timeb.h>
#include    <sys/types.h>
#include    <sys/stat.h>
#include 	<sys/types.h>
#include 	<sys/ioctl.h>
#include 	<sys/socket.h>
#include 	<sys/ipc.h>
#include 	<sys/shm.h>
#include 	<sys/mman.h>
#include 	<linux/wireless.h>
#include 	<arpa/inet.h>
#include 	<netinet/in.h>

#include 	<unistd.h>
#include 	<stdarg.h>
#include    <stdio.h>      /*�зǿ�J��X�w�q*/
#include    <stdlib.h>     /*�зǨ�Ʈw�w�q*/
#include    <unistd.h>     /*Unix �зǨ�Ʃw�q*/
#include    <fcntl.h>      /*�ɱ���w�q*/
#include    <termios.h>    /*PPSIX �׺ݱ���w�q*/
#include    <errno.h>      /*���~���w�q*/
#include 	<string.h>
#include	<time.h>
#include	<ctype.h>
#include 	<ifaddrs.h>
#include 	<math.h>
#include 	"PrimaryComm.h"

#define PASS				1
#define FAIL				-1

struct Address Addr={0x01,0x02,0x03,0x04,0xFF};
struct Command Cmd={0x01,0x02,0x0a,0x2C,0x83,0x86,0x87,0xe0,0xe1,0xe2,0xe3};

int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
{
	int len;
	//sleep(2); //required to make flush work, for some reason
	tcflush(fd,TCIOFLUSH);
	if(write(fd, cmd, cmd_len) >= cmd_len)
	{
		usleep(50000);
		len = read(fd, rx, 512);
	}
	else
	{
		#ifdef SystemLogMessage
		DEBUG_ERROR("Serial command %s response fail.\n", cmd);
		#endif
	}

	return len;
}

unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
{
	unsigned char result = FAIL;
	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_FW_Ver, 0x00, 0x00, 0x00};
	unsigned char rx[512];
	unsigned char chksum = 0x00;
	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);

	if(len > 0)
	{
		for(int idx = 0; idx < (rx[4] | rx[5]<<8); idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
		   (rx[2] == tx[1]) &&
		   (rx[1] == tx[2]) &&
		   (rx[3] == tx[3]))
		{
			memcpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
			*(Ret_Buf->Version_FW + 8) = 0x00;
			result = PASS;
		}
	}

	return result;
}

unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
{
	unsigned char result = FAIL;
	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_HW_Ver, 0x00, 0x00, 0x00};
	unsigned char rx[512];
	unsigned char chksum = 0x00;

	if(tranceive(fd, tx, sizeof(tx), rx) >0)
	{
		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
		   (rx[2] == tx[1]) &&
		   (rx[1] == tx[2]) &&
		   (rx[3] == tx[3]))
		{
			memcpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
			//*(Ret_Buf->Version_HW + 8) = 0x00;
			result = PASS;
		}
	}

	return result;
}

unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
{
	unsigned char result = FAIL;
	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
	unsigned char rx[512];
	unsigned char chksum = 0x00;
	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);

	if(len > 0)
	{
		for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
		   (rx[2] == tx[1]) &&
		   (rx[1] == tx[2]) &&
		   (rx[3] == tx[3]))
		{
			Ret_Buf->AC_Connector 		= (rx[6] >> 0) & 0x01;
			Ret_Buf->AC_MainBreaker 	= (rx[6] >> 1) & 0x01;
			Ret_Buf->SPD 				= (rx[6] >> 2) & 0x01;
			Ret_Buf->Door_Open 			= (rx[6] >> 3) & 0x01;
			Ret_Buf->GFD[0] 			= (rx[6] >> 4) & 0x01;
			Ret_Buf->GFD[1] 			= (rx[6] >> 5) & 0x01;
			Ret_Buf->AC_Drop 			= (rx[6] >> 6) & 0x01;
			Ret_Buf->Emergency_IO 		= (rx[6] >> 7) & 0x01;

			Ret_Buf->Emergency_Btn		= (rx[7] >> 0) & 0x01;
			Ret_Buf->Button[0] 			= (rx[7] >> 1) & 0x01;
			Ret_Buf->Button[1] 			= (rx[7] >> 2) & 0x01;
			Ret_Buf->Key[0]				= (rx[7] >> 3) & 0x01;
			Ret_Buf->Key[1]				= (rx[7] >> 4) & 0x01;
			Ret_Buf->Key[2]				= (rx[7] >> 5) & 0x01;
			Ret_Buf->Key[3]				= (rx[7] >> 6) & 0x01;

			result = PASS;
		}
	}

	return result;
}

unsigned char Query_Meter_value(unsigned char fd, unsigned char targetAddr, struct StructMeter *Ret_Buf, byte *isWork, unsigned char index)
{
	unsigned char result = FAIL;
	unsigned char tx[8] = {0xaa, 0x00, targetAddr, Cmd.query_charging_power, 0x01, 0x00, index, 0x00};
	unsigned char rx[512];
	unsigned char chksum = 0x00;

	for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
			chksum ^= tx[6 + idx];
	tx[7] = chksum;

	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
	if(len > 0)
	{
		chksum = 0x00;
		for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
		   (rx[2] == tx[1]) &&
		   (rx[1] == tx[2]) &&
		   (rx[3] == tx[3]))
		{
			Ret_Buf->newMeterValue = (rx[6] | (rx[7]<<8) | (rx[8]<<8) | (rx[9]<<8));
			*isWork = rx[23];
			result = PASS;
		}
	}

	return result;
}

unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
{
	unsigned char result = FAIL;
	unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
	unsigned char rx[512];
	unsigned char chksum = 0x00;

	for (int idx = 0; idx < 2; idx++)
		tx[6] |= (Set_Buf->Button_LED[idx] ? 0x01:0x00) << (0+idx);

	for (int idx = 0; idx < 4; idx++)
			tx[6] |= (Set_Buf->System_LED[idx] ? 0x01:0x00) << (2+idx);

	tx[6] |= (Set_Buf->AC_Connector ? 0x01:0x00) << 6;
	tx[6] |= (Set_Buf->AC_Breaker ? 0x01:0x00) << 7;

	for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
		chksum ^= tx[6+idx];
	tx[7] = chksum;

	if (tranceive(fd, tx, sizeof(tx), rx) > 0)
	{
		chksum = 0x00;
		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
		   (rx[2] == tx[1]) &&
		   (rx[1] == tx[2]) &&
		   (rx[3] == tx[3]) &&
		   (rx[6] == tx[6]))
		{
			result = PASS;
		}
	}

	return result;
}

unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
{
	unsigned char result = FAIL;
	unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
			Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
			Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]};
	unsigned char rx[512];
	unsigned char chksum = 0x00;

	for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
		chksum ^= tx[6 + idx];
	tx[20] = chksum;

	if (tranceive(fd, tx, sizeof(tx), rx) > 0)
	{
		chksum = 0x00;
		for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
		{
			chksum ^= rx[6 + idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
				(rx[2] == tx[1]) &&
				(rx[1] == tx[2]) &&
				(rx[3] == tx[3]) &&
				(rx[6] == tx[6]))
		{
			result = PASS;
		}
	}

	return result;
}

unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
{
	unsigned char result = FAIL;
	unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
	unsigned char rx[512];
	unsigned char chksum = 0x00;

	memcpy(tx + 6, modelname, 14);

	for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
		chksum ^= tx[6+idx];
	tx[20] = chksum;

//	for(int i = 0; i < 21; i++)
//				printf ("tx = %x \n", tx[i]);
	unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
//	for(int i = 0; i < len; i++)
//					printf ("rx = %x \n", rx[i]);
	if(len > 6)
	{
		if (len < 6+(rx[4] | rx[5]<<8))
			return result;

		chksum = 0x00;
		for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
			  (rx[2] == tx[1]) &&
			  (rx[1] == tx[2]) &&
			  (rx[3] == tx[3]) &&
			  rx[6] == PASS)
		{
			result = PASS;
		}
	}

	return result;
}

unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
{
	unsigned char result = FAIL;
	unsigned char tx[11] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
	unsigned char rx[512];
	unsigned char chksum = 0x00;

	for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
		chksum ^= tx[6+idx];
	tx[10] = chksum;


	if(tranceive(fd, tx, sizeof(tx), rx) > 0)
	{
		chksum = 0x00;
		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
		   (rx[2] == tx[1]) &&
		   (rx[1] == tx[2]) &&
		   (rx[3] == tx[3]) &&
		   (rx[6] == 0x00))
		{
			result = PASS;
		}
	}

	return result;
}

unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
{
	unsigned char result = FAIL;
	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
	unsigned char rx[512];
	unsigned char chksum = 0x00;

	if(tranceive(fd, tx, sizeof(tx), rx) >0)
	{
		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
		   (rx[2] == tx[1]) &&
		   (rx[1] == tx[2]) &&
		   (rx[3] == tx[3]) &&
		   (rx[6] == 0x00))
		{
			result = PASS;
		}
	}

	return result;
}

unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
{
	unsigned char result = FAIL;
	unsigned char tx[11 + length];
	unsigned char rx[512];
	unsigned char chksum = 0x00;

	tx[0] = 0xaa;
	tx[1] = 0x00;
	tx[2] = targetAddr;
	tx[3] = Cmd.update_Transfer;
	tx[4] = (4 + length) & 0xff;
	tx[5] = ((4 + length)>>8) & 0xff;
	tx[6] = (startAddr>>0) & 0xff;
	tx[7] = (startAddr>>8) & 0xff;
	tx[8] = (startAddr>>16) & 0xff;
	tx[9] = (startAddr>>24) & 0xff;
	memcpy(tx+10, data, length);

	for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
		chksum ^= tx[6+idx];
	tx[sizeof(tx)-1] = chksum;

	if(tranceive(fd, tx, sizeof(tx), rx) >0)
	{
		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
		   (rx[2] == tx[1]) &&
		   (rx[1] == tx[2]) &&
		   (rx[3] == tx[3]) &&
		   (rx[6] == 0x00))
		{
			result = PASS;
		}
	}

	return result;
}

unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
{
	unsigned char result = FAIL;
	unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
	unsigned char rx[512];
	unsigned char chksum = 0x00;

	if(tranceive(fd, tx, sizeof(tx), rx) >0)
	{
		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
		{
			chksum ^= rx[6+idx];
		}

		if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
		   (rx[2] == tx[1]) &&
		   (rx[1] == tx[2]) &&
		   (rx[3] == tx[3]) &&
		   (rx[6] == 0x00))
		{
			result = PASS;
		}
	}

	return result;
}