// 2003.09.08, 2003.12.28, 2004.12.10
//****************************************
//**  Copyright  (C)  W.ch  1999-2004   **
//**  Web:  http://www.winchiphead.com  **
//****************************************
//**  EXE for USB interface chip CH375  **
//**  C, VC5.0                          **
//****************************************
//
// USB总线接口芯片CH375的应用层演示程序 V1.1
// 南京沁恒电子有限公司  作者: W.ch 2004.12
// CH375-EXE  V1.1 , Support: Ctrl/Bulk/Int
// 运行环境: Windows 98/ME, Windows 2000/XP
// support USB chip: CH372/CH375
//



#include	<windows.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>

#include	"CH375DLL.H"		// CH372/CH375的动态链接库
#include	"DEMO.H"

HINSTANCE   mSaveInstance;		// 实例
HWND        mSaveDialogMain;	// 主对话框

UINT	mIndex = 0;				// 如果有多个CH372/CH375则指示序号

CHAR	mCaptionInform[] = " 信息提示 ";
CHAR   CmdFile[256];
HANDLE hFile;
OPENFILENAME mOpenFile;

LRESULT CALLBACK mDialogMain(HWND,UINT,WPARAM,LPARAM);  //主对话框调用程序
HANDLE  OpencmdFile(HWND hDialog,UCHAR *FileName);
BOOL TransFile(HWND hDialog);

//程序入口
int	APIENTRY	WinMain( HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd )
{
	CHAR	*CmdLine;
	CmdLine = lpCmdLine;
	memcpy(CmdFile,CmdLine,strlen(CmdLine));
	mSaveInstance = hInstance;
	return( DialogBox( hInstance, "IDD_MAIN", NULL, mDialogMain ) );    //调用主对话框
}

//主对话框事件
LRESULT CALLBACK mDialogMain( HWND hDialog, UINT uMessage, WPARAM wParam, LPARAM lParam )
{
	switch( uMessage )
	{
		case WM_INITDIALOG:        //初始化
			{
				if(CmdFile != NULL)
					SetDlgItemText(hDialog,IDC_EDIT_FILE,CmdFile);
			}
			return ( TRUE );
			break;
		case WM_COMMAND:
			switch ( LOWORD( wParam ) )
			{
				
				case IDC_CMD1:  // 以十六进制输入3个字符共12位到演示板上的CH451芯片
					{
						UCHAR pFileN[100];
						// 需要使用DLL则需要先加载
						if ( LoadLibrary( "CH375DLL.DLL" ) == NULL )  // 加载DLL失败,可能未安装到系统中
						{
							MessageBox( hDialog, "无法加载CH372/CH375的DLL", mCaptionInform, MB_ICONSTOP | MB_OK );
							EndDialog( hDialog, 0x81 );  // 关闭对话框
							return( TRUE );
						}
						if ( CH375OpenDevice( mIndex ) == INVALID_HANDLE_VALUE )  /* 使用之前必须打开设备 */
						{
							MessageBox( hDialog, "无法打开CH372/CH375设备", mCaptionInform, MB_ICONSTOP | MB_OK );
							EndDialog( hDialog, 0x81 );  // 关闭对话框
							return( TRUE );
						}
						CH375SetTimeout( mIndex, 5000, 5000 );  // 设置USB数据读写的超时,超过3000mS未完成读写将强制返回,避免一直等待下去
						CH375SetExclusive( mIndex, TRUE );  // 设置独占使用当前CH375设备,在此之后其它应用程序不能同时使用当前这个CH375设备
						
						mSaveDialogMain = hDialog;
				
						if(CmdFile == "")
						{
							GetDlgItemText(hDialog,IDC_EDIT_FILE,pFileN,100);
						}

						else
						{
							strcpy(pFileN,"");
							memcpy(pFileN,CmdFile,strlen(CmdFile));
							pFileN[strlen(CmdFile)]='\0';
						}
							
						
						if(pFileN =="")
							MessageBox(hDialog,"请输入文件名！",mCaptionInform,MB_OK|MB_ICONSTOP);
						else
						{
							hFile=OpencmdFile(hDialog,pFileN);
							if((hFile != 0) && (hFile != NULL))
							{
								TransFile(hDialog);
								CloseHandle(hFile);
							}
						}
						CH375CloseDevice( mIndex );  // 退出程序前必须关闭CH375设备	
					}
					return( TRUE );
					break;
				case IDC_BUTTON_OPEN:
					{
						OPENFILENAME mOpenFile;
						TCHAR         szFile[260]      = "\0";
						// Fill in the OPENFILENAME structure to support a template and hook.
						mOpenFile.lStructSize       = sizeof(OPENFILENAME);
						mOpenFile.hwndOwner         = hDialog;
						mOpenFile.hInstance         = mSaveInstance;
						mOpenFile.lpstrFilter       = "目标程序 *.*\x0*.*\x0\x0";
						mOpenFile.lpstrCustomFilter = NULL;
						mOpenFile.nMaxCustFilter    = 0;
						mOpenFile.nFilterIndex      = 0;
						mOpenFile.lpstrFile         = szFile;
						mOpenFile.nMaxFile          = sizeof(szFile);
						mOpenFile.lpstrFileTitle    = NULL;
						mOpenFile.nMaxFileTitle     = 0;
						mOpenFile.lpstrInitialDir   = NULL;
						mOpenFile.lpstrTitle        = "选择将下载的目标程序文件";
						mOpenFile.nFileOffset       = 0;
						mOpenFile.nFileExtension    = 0;
						mOpenFile.lpstrDefExt       = NULL;
						mOpenFile.lCustData         = 0;
						mOpenFile.lpfnHook 		   = NULL;
						mOpenFile.lpTemplateName    = NULL;
						mOpenFile.Flags             = OFN_SHOWHELP | OFN_EXPLORER | OFN_READONLY | OFN_FILEMUSTEXIST;

						if (GetOpenFileName(&mOpenFile))
						{
							memset(CmdFile,0x00,sizeof(CmdFile));
							memcpy(CmdFile,mOpenFile.lpstrFile,strlen(mOpenFile.lpstrFile));
							SetDlgItemText(hDialog,IDC_EDIT_FILE,CmdFile);
						}
						else
						{
							return (TRUE);
						}
					}
					return (TRUE);
					break;
				case IDC_BUTTON_CLOSE:
				case WM_DESTROY:  // 退出
					
					EndDialog( hDialog, 1 );
					return( TRUE );
					break;
			}
			break; 
	}
	return ( FALSE );
}

HANDLE  OpencmdFile(HWND hDialog,UCHAR *FileName)
{
	TCHAR         szFile[260]      = "\0";
	USHORT len = 0;
	HANDLE hf;
	//打开存在的文件
	if ((hf = CreateFile((LPCTSTR)FileName,
		GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		(HANDLE)NULL)) == (HANDLE)-1)
	{
		MessageBox( hDialog, "文件打开失败", mCaptionInform, MB_OK );
		return 0;
	}
	return hf;		
}

BOOL TransFile(HWND hDialog)
{
	UCHAR BufFile[1024];
	ULONG Trb;
	UCHAR ustatu[10];
	ULONG ReadLen;
	ULONG mLen=0;
	UCHAR by=0;
	ULONG bylen=0;
	SetDlgItemText(hDialog,IDC_STATIC_STATUS,"0K");
	Trb=0;					
	CH375WriteData( mIndex, &by, &bylen ); //发零字节 
	while(1)
	{
		ReadLen=1024;
		ReadFile(hFile,&BufFile[0],ReadLen,&ReadLen,NULL);
		if(ReadLen < 1024)
		{
			mLen = ReadLen;	
			if ( CH375WriteData( mIndex, &BufFile[0], &mLen ) )  // 通过CH375发送命令数据,成功
			{
				sprintf(ustatu,"%d",Trb);
				strcat(ustatu,"K");
				if(mLen != 0);
					Trb=Trb++;
				sprintf(ustatu,"%d",Trb);
				strcat(ustatu,"K");
				SetDlgItemText(hDialog,IDC_STATIC_STATUS,ustatu);
				MessageBox( hDialog, "写结束!", mCaptionInform, MB_OK );
				break;
			}
			else
			{
				MessageBox( hDialog, "CH375WriteData 失败", mCaptionInform, MB_OK | MB_ICONERROR );
				break;
			}
		}
		else
		{
			mLen = ReadLen;	
			if ( CH375WriteData( mIndex, &BufFile[0], &mLen ) )  // 通过CH375发送命令数据,成功
			{
				sprintf(ustatu,"%d",Trb);
				strcat(ustatu,"K");
				if(mLen != 0);
					Trb++;
				sprintf(ustatu,"%d",Trb);
				strcat(ustatu,"K");
				SetDlgItemText(hDialog,IDC_STATIC_STATUS,ustatu);
			}
			else
			{
				MessageBox( hDialog, "CH375WriteData 失败", mCaptionInform, MB_OK | MB_ICONERROR );
				break;
			}
		}
	}
	return FALSE;
}
