/*C*****************************************************************************
* NAME:         DOBCNT1010.c
*-------------------------------------------------------------------------------
* RELEASE:          
* REVISION:     1.0   
*-------------------------------------------------------------------------------
* PURPOSE:
* 双枪计数器应用程序
*******************************************************************************/

#include "config.h"

u8 DiskFileOpen_Flag = 0;
u64 File_Len=0;

void Log_Init(void)
{
	u8 i= 0;
  mInitCH376Host();
	delay_ms(100);
	
}
/*
const char CMD_WifiWSSSID[]="AT+WSSSID=KFB-2.4G\r\n";
const char CMD_WifiWSKEY[]="AT+WSKEY=WPA2PSK,AES,KFB123456\r\n";
const char CMD_WifiNETP[]="AT+NETP=TCP,CLIENT,7011,test.joyfueling.com\r\n";
*/
u8 SysconfigFileName[20] = "\\SYSTEM.TXT";
u16 EndLen=0,StartLen=0;
u8 FindSysconfigFile(void)
{
  u16 i=0;
	
	u8 Next[] = {0x0D,0x0A};
	u8 Buff[1000];
	u16 pLocate = 0;
	{
		if(CH376DiskConnect() == USB_INT_SUCCESS)/* 检查U盘是否连接 */
		{
			DiskFileOpen_Flag = CH376FileOpen(SysconfigFileName);
			if(DiskFileOpen_Flag == USB_INT_SUCCESS)
			{
				File_Len = CH376GetFileSize();
				{
					CH376ByteLocate(pLocate);
					CH376ByteRead(Buff,500,&pLocate);
					
					StartLen = mystrstr(Buff,"APN=");
					if(StartLen)
					{
						for(i = 0;i < 40;i ++)
							TXHData.POSData.WirelessData.APN[i] = 0;
						EndLen = mystrstr(&Buff[StartLen],";");
						for(i = 0;i < EndLen-4;i ++)
							TXHData.POSData.WirelessData.APN[i] = Buff[StartLen + 3 + i];
						}
					StartLen = mystrstr(Buff,"WIFINAME=");
					if(StartLen)
					{
						for(i = 0;i < 40;i ++)
							TXHData.POSData.WirelessData.WiFIName[i] = 0;
						EndLen = mystrstr(&Buff[StartLen],";");
						for(i = 0;i < EndLen-9;i ++)
							TXHData.POSData.WirelessData.WiFIName[i] = Buff[StartLen + 8 + i];
						}
					StartLen = mystrstr(Buff,"WIFIPASSWORD=");
					if(StartLen)
					{
						for(i = 0;i < 20;i ++)
							TXHData.POSData.WirelessData.WiFIPassWord[i] = 0;
						EndLen = mystrstr(&Buff[StartLen],";");
						for(i = 0;i < EndLen-13;i ++)
							TXHData.POSData.WirelessData.WiFIPassWord[i] = Buff[StartLen + 12 + i];
						}
					StartLen = mystrstr(Buff,"SERVER=");
					if(StartLen)
					{
//						if(TXHData.POS_Info.NetType == POS_Net_Typ_WiFi)
//						{
							for(i = 0;i < 50;i ++)
								TXHData.POSData.WirelessData.CMD_SOCKA[i] = 0;
							EndLen = mystrstr(&Buff[StartLen],";");
							for(i = 0;i < EndLen-7;i ++)
								TXHData.POSData.WirelessData.CMD_SOCKA[i] = Buff[StartLen + 6 + i];
//						  }
//						else
//						{
//							for(i = 13;i < 50;i ++)
//								TXHData.POSData.WirelessData.CMD_SOCKA[i] = 0;
//							EndLen = mystrstr(&Buff[StartLen],";");
//							for(i = 0;i < EndLen-7;i ++)
//								TXHData.POSData.WirelessData.CMD_SOCKA[13+i] = Buff[StartLen + 6 + i];
//							strcat(TXHData.POSData.WirelessData.CMD_SOCKA, "\r\n\0");
//							}
						}
					FMItemWrite(CF_POSData,0);
					}
				CH376FileClose(TRUE);
				return 1;
			 }
			else
			{
				if(DiskFileOpen_Flag == ERR_MISS_FILE)
					CH376FileCreate(SysconfigFileName);
				else
				{
					CH376FileClose(TRUE);
					DiskFileOpen_Flag = 0;
					}
				}
			}
		else
		{
			TXHData.UDisk_Flag = 0;
			mInitCH376Host();
			}
		}
}
	u8 LogName[5][25] = {
		                  "\\M1_0000.TXT",
											"\\M2_0000.TXT",
											"\\M3_0000.TXT",
											"\\M4_0000.TXT",
											"\\POS0000.TXT",
										};
void Log_Task(u8 *pBuff,u16 Len,u8 port,u8 Source)
{
	u16 i=0,Temp16=0;

	u8 Next[] = {0x0D,0x0A};
	u8 Buff[2000];
	u16 pLocate = 0;
  if(port <= 4)
	{
		LogName[port][4] = ((TXHData.CTIME.Month >> 4) & 0x0f) + 0x30;
		LogName[port][5] = (TXHData.CTIME.Month & 0x0f) + 0x30;
		LogName[port][6] = ((TXHData.CTIME.Day >> 4) & 0x0f) + 0x30;
		LogName[port][7] = (TXHData.CTIME.Day & 0x0f) + 0x30;
		if(CH376DiskConnect() == USB_INT_SUCCESS)/* 检查U盘是否连接 */
		{
			if(DiskFileOpen_Flag != DEF_DISK_OPEN_FILE)
				DiskFileOpen_Flag = CH376FileOpen(LogName[port]);
			if(DiskFileOpen_Flag == DEF_DISK_OPEN_FILE)
			{
				if(Len > 1000)
					Len = 1000;
				File_Len = CH376GetFileSize();
				CH376ByteLocate(File_Len);
				Buff[0] = ((TXHData.CTIME.Day >> 4) & 0x0f) + 0x30;
				Buff[1] = (TXHData.CTIME.Day & 0x0f) + 0x30;
				Buff[2] = ((TXHData.CTIME.Hour >> 4) & 0x0f) + 0x30;
				Buff[3] = (TXHData.CTIME.Hour & 0x0f) + 0x30;
				Buff[4] = ((TXHData.CTIME.Min >> 4) & 0x0f) + 0x30;
				Buff[5] = (TXHData.CTIME.Min & 0x0f) + 0x30;
				Buff[6] = ((TXHData.CTIME.Sec >> 4) & 0x0f) + 0x30;
				Buff[7] = (TXHData.CTIME.Sec & 0x0f) + 0x30;
				if(Source == Info_String)
				{
					Buff[8] = ':';
					for(i = 0;i < Len;i ++)
					  Buff[i+9] = *(pBuff + i);
				  CH376ByteWrite(Buff,Len + 9,&pLocate);
				  }
				else
				{
					if(Source == Info_Issue)
						Buff[8] = 'T';
					else if(Source == Info_Receive)
						Buff[8] = 'R';
					Buff[9] = ':';
					for(i = 0;i < Len * 2;i += 2)
					{
						Temp16 = *(pBuff + i / 2) >> 4;
						Buff[i + 10] = Temp16 + 0x30 + (Temp16 / 10) * 7;
						Temp16 = *(pBuff + i / 2) & 0x0f;
						Buff[i+11] = Temp16 + 0x30 + (Temp16 / 10) * 7;
						}
					CH376ByteWrite(Buff,Len*2 + 10,&pLocate);
					}
				CH376ByteWrite(Next,2,&pLocate);
				CH376FileClose(TRUE);
				DiskFileOpen_Flag = 0;
			 }
			else
			{
				if(DiskFileOpen_Flag == ERR_MISS_FILE)
					CH376FileCreate(LogName[port]);
				else
				{
					CH376FileClose(TRUE);
					DiskFileOpen_Flag = 0;
					}
				}
			}
		else
		{
			TXHData.UDisk_Flag = 0;
			mInitCH376Host();
			}
		}
 }
