#include <stdio.h> 
#include <string.h>
#include "../util/sockutil.h"
#include "../util/myprintf.h"
#include "../iinchip/socket.h"
#include "SEEP.h"

#include "config.h"

SYSINFO	SysInfo;
NETCONF	NetConf;
CHCONF	ChConf;

void init_conf_variables(void)
{
	memset((void*)&SysInfo,0,sizeof(SYSINFO));	
	memset((void*)&NetConf,0,sizeof(NETCONF));
	memset((void*)&ChConf,0,sizeof(CHCONF));
	get_sysinfo(&SysInfo);
	get_netconf(&NetConf);
	get_chconf(&ChConf);
}
	
void set_sysinfo(SYSINFO* pSysInfo)
{
	eeprom_write_block((void*)pSysInfo,SYS_INFO, sizeof(SYSINFO));	
}

void get_sysinfo(SYSINFO* pSysInfo)
{
	if(eeprom_read_word(SYS_TEST) != TEST_VALUE)
	{
		pSysInfo->test = TEST_VALUE;
		pSysInfo->ver  = FW_VERSION;
		pSysInfo->auto_reset = SYSTEM_MANUAL_RESET;
		pSysInfo->any_port = SYSTEM_ANY_PORT;
		set_sysinfo(pSysInfo);
	}
	else
	{
		eeprom_read_block((void*)pSysInfo,SYS_INFO, sizeof(SYSINFO));
		pSysInfo->ver = FW_VERSION;
	}
}

u_char get_reset_flag(void)
{
	SysInfo.auto_reset = eeprom_read_byte(SYS_AUTORESET);
	return SysInfo.auto_reset;
}

void set_reset_flag(u_char flag)
{
	eeprom_write_byte(SYS_AUTORESET,flag);	
}

u_int  get_system_any_port(void)
{
	u_int i;
	SysInfo.any_port = eeprom_read_word(SYS_ANY_PORT)+1;
	for(i=0; i < MAX_SOCK_NUM; i++)
	{
		if(SysInfo.any_port == ChConf.ch[i].port) 
		{
			if(SysInfo.any_port++ > 1000) SysInfo.any_port = 1000;
		}
	}
	eeprom_write_word(SYS_ANY_PORT,SysInfo.any_port);
	return SysInfo.any_port;
}

void set_netconf(NETCONF* pNetConf)
{
	eeprom_write_block((void*)pNetConf,NET_CONF, sizeof(NETCONF));	
}

void get_netconf(NETCONF* pNetConf)
{
	if(eeprom_read_word(NET_TEST) != TEST_VALUE)
	{
		pNetConf->test = TEST_VALUE;
		eeprom_read_block((void*)pNetConf->mac, NET_MAC,6);
		if(!memcmp(pNetConf->mac,"\xFF\xFF\xFF\xFF\xFF\xFF",6) || !memcmp(pNetConf->mac,"\x00\x00\x00\x00\x00\x00",6))
		{
			memcpy(pNetConf->mac,DEFAULT_NET_MAC,6);
		}
		pNetConf->sip	= htonl(DEFAULT_NET_SIP);
		pNetConf->gwip	= htonl(DEFAULT_NET_GWIP);
		pNetConf->sn	= htonl(DEFAULT_NET_SN);
		pNetConf->dns	= htonl(DEFAULT_NET_DNS);
		pNetConf->Mem_alloc = DEFAULT_NET_MEMALLOC;
		
		set_netconf(pNetConf);
	}
	else eeprom_read_block((u_char*)pNetConf,NET_CONF, sizeof(NETCONF));
}

void set_chconf(CHCONF* pChConf)
{
	eeprom_write_block((void*)pChConf,CH_CONF, sizeof(CHCONF));
}

void get_chconf(CHCONF* pChConf)
{
	if(eeprom_read_word(CH_CONF) != TEST_VALUE)
	{
		u_int i;
		pChConf->test = TEST_VALUE;
		for(i=0;i < 4; i++)
		{
			pChConf->ch[i].type  = DEFAULT_CH_TYPE;
			pChConf->ch[i].port = DEFAULT_LISTEN_PORT;
			pChConf->ch[i].destip = 0;
		}
	}
	else eeprom_read_block((void*)pChConf,CH_CONF, sizeof(CHCONF));
}


void load_factory_netconf(void)
{
	u_int erase_value = 0x0000;
	eeprom_write_word(NET_TEST,erase_value);
	get_netconf(&NetConf);		
}


void load_factory_chconf(void)
{
	u_int erase_value = 0x0000;
	eeprom_write_word(CH_CONF,erase_value);
	get_chconf(&ChConf);		
}


void display_netconf(NETCONF* pNetConf)
{
	u_int i;
	printf("MAC Addr      : ");
	for(i=0; i<5;i++) printf("%02X.",pNetConf->mac[i]);
	printf("%02X\r\n",pNetConf->mac[i]);
	printf("Source IP     : %s\r\n",inet_ntoa(ntohl(pNetConf->sip)));
	printf("Gateway IP    : %s\r\n",inet_ntoa(ntohl(pNetConf->gwip)));
	printf("Subnet Mask   : %s\r\n",inet_ntoa(ntohl(pNetConf->sn)));
	printf("DNS Server IP : %s\r\n",inet_ntoa(ntohl(pNetConf->dns)));
	printf("Mem alloc     : %.2x\r\n", pNetConf->Mem_alloc);
}

void display_chconf(CHCONF* pChConf)
{
	u_int i;
	for(i=0; i < MAX_SOCK_NUM; i++)
	{
		printf("CH%d : ",i); 
		switch(pChConf->ch[i].type)
		{
		case NOTUSE: printf("Not Used\r\n");
			break;
		case DHCP_CLIENT: printf("DHCP Client\r\n");
			break;
		case LB_TCPS:
			printf("Loob-Back TCP Server : Listen Port %d\r\n",pChConf->ch[i].port);
			break;
		case LB_TCPC:
			printf("Loob-Back TCP Client : Server Port %d, ",pChConf->ch[i].port);
			printf("Dest IP %s\r\n", inet_ntoa(ntohl(pChConf->ch[i].destip)));
			break;
		case LB_UDP:
			printf("Loop-Back UDP : Source Port %d\r\n", pChConf->ch[i].port);
			break;
		case WEB_SERVER:
			printf("Web Server : HTTP Port %d\r\n", pChConf->ch[i].port);
			break;
		}
	}
}
