// TSA.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "TSG.h"

CONST BYTE bufferSize = 65;

BYTE __stdcall Get_Hid_Handle(HANDLE* _DeviceHandle, CHAR* _SerialNumber)
{
	BOOL LastDevice=FALSE;
	BOOLEAN count = 0;

	GUID guidHID;
	HidD_GetHidGuid(&guidHID);

	HDEVINFO hDevInfo = SetupDiGetClassDevs(&guidHID,NULL,0,DIGCF_PRESENT|DIGCF_DEVICEINTERFACE );
	
	if(hDevInfo==INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}

	SP_DEVICE_INTERFACE_DATA strtInterfaceData;
	strtInterfaceData.cbSize=sizeof(SP_DEVICE_INTERFACE_DATA);		
	
	BOOL bSuccess ;
	DWORD index=0;

	do
	{
		bSuccess= SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&guidHID,index,&strtInterfaceData);

		if(bSuccess==FALSE)
		{
			LastDevice=TRUE;
		}
		
		if(bSuccess!=FALSE)
		{
			PSP_DEVICE_INTERFACE_DETAIL_DATA strtDetailData;
			
			DWORD strSzie=0,requiesize=0;
			SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,NULL,0,&strSzie,NULL);
			
			requiesize=strSzie;
			strtDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiesize);
			strtDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
			
			SP_DEVINFO_DATA infodata;
			infodata.cbSize=sizeof(SP_DEVINFO_DATA);

			SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,strtDetailData,strSzie,&requiesize,NULL);

			HANDLE hCom = CreateFile (
							  strtDetailData->DevicePath,
							  GENERIC_READ | GENERIC_WRITE,
							  FILE_SHARE_READ | FILE_SHARE_WRITE,
							  NULL, 
							  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 
							  NULL);

			HIDD_ATTRIBUTES strtAttrib;

			strtAttrib.Size=sizeof(HIDD_ATTRIBUTES);
			HidD_GetAttributes(hCom,&strtAttrib);

			if(((strtAttrib.VendorID==0x16d0)&&(strtAttrib.ProductID==0x0b0f))&&(hCom != (HANDLE)(0xffffffff)))
			{

				*_DeviceHandle =hCom;
				_DeviceHandle++;
				count++;
			}
			else
			{
                CloseHandle(hCom);
			}
		}
		index++;
	}
	while((LastDevice==FALSE));

	LastDevice = FALSE;

	GUID guidUSB;

	guidUSB.Data1 = 0xa5dcbf10L;
	guidUSB.Data2 = 0x6530;
	guidUSB.Data3 = 0x11d2;
	guidUSB.Data4[0] = 0x90;
	guidUSB.Data4[1] = 0x1F;
	guidUSB.Data4[2] = 0x00;
	guidUSB.Data4[3] = 0xC0;
	guidUSB.Data4[4] = 0x4F;
	guidUSB.Data4[5] = 0xB9;
	guidUSB.Data4[6] = 0x51;
	guidUSB.Data4[7] = 0xED; 

	hDevInfo = SetupDiGetClassDevs(&guidUSB,NULL,0,DIGCF_PRESENT|DIGCF_DEVICEINTERFACE );
	
	if(hDevInfo==INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}

	strtInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);		
	
	index = 0;
	UINT32 x = 0;

	do
	{
		bSuccess= SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&guidUSB,index,&strtInterfaceData);

		if(bSuccess==FALSE)
		{
			LastDevice=TRUE;
		}
		
		if(bSuccess!=FALSE)
		{
			PSP_DEVICE_INTERFACE_DETAIL_DATA strtDetailData;
			
			DWORD strSzie=0,requiesize=0;
			SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,NULL,0,&strSzie,NULL);
			
			requiesize=strSzie;
			strtDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiesize);
			strtDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
			
			SP_DEVINFO_DATA infodata;
			infodata.cbSize=sizeof(SP_DEVINFO_DATA);

			SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,strtDetailData,strSzie,&requiesize,NULL);

			HANDLE hCom = CreateFile (
							  strtDetailData->DevicePath,
							  GENERIC_READ | GENERIC_WRITE,
							  FILE_SHARE_READ | FILE_SHARE_WRITE,
							  NULL, 
							  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 
							  NULL);

			if(((strtDetailData->DevicePath)[12] == 0x31)&&((strtDetailData->DevicePath)[13] == 0x36)&&((strtDetailData->DevicePath)[14] == 0x64)&&((strtDetailData->DevicePath)[15] == 0x30)
				&&(((strtDetailData->DevicePath)[21] == 0x30)&&((strtDetailData->DevicePath)[22] == 0x62)&&((strtDetailData->DevicePath)[23] == 0x30)&&((strtDetailData->DevicePath)[24] == 0x66)))
			{
				for (BYTE i = 0; i < 10; i++)
				{
					_SerialNumber[x++] = (strtDetailData->DevicePath)[i + 26];
				}
			}

			CloseHandle(hCom);
		}
		index++;
	}
	while((LastDevice==FALSE));


	return count;
}

BYTE __stdcall Start_Dongle(HANDLE hDongle, BYTE Option, BYTE SaveToFlash, BYTE FreqMode, BYTE PulseMode, BYTE IQMode, BYTE AsynMode, BYTE ColockMode, BYTE Switch, DWORD StartFreq, DWORD Step, WORD Points, DWORD Period, DWORD PulseWidth, DWORD PulseDelay, BYTE PulseNumber, BYTE Ampitulde, DWORD IQStep)
{
	BYTE WriteBuffer[bufferSize];

	WriteBuffer[0] = 0x00;
	WriteBuffer[1] = 0x10;
	WriteBuffer[2] = 0x01;

	WriteBuffer[3] = Option; 
	WriteBuffer[4] = (SaveToFlash & 0x01) << 7; 

	BYTE m = 6;

	for(BYTE i = 0; i < 8; i++)
	{
		if (((Option >> i) & 1) == 1)
		{
			if(i == 0)
			{
				WriteBuffer[m++] = (FreqMode & 0x03) | ((PulseMode & 0x01) << 2) | ((IQMode & 0x03) << 3) | ((AsynMode & 0x01) << 5)  | ((ColockMode & 0x01) << 6) | ((Switch & 0x01) << 7);
			}
			else if(i == 1)
			{
				WriteBuffer[m++] = StartFreq & 0xff;
				WriteBuffer[m++] = (StartFreq >> 8) & 0xff;
				WriteBuffer[m++] = (StartFreq >> 16) & 0xff;
				WriteBuffer[m++] = (StartFreq >> 24) & 0xff;
				WriteBuffer[m++] = Step & 0xff;
				WriteBuffer[m++] = (Step >> 8) & 0xff;
				WriteBuffer[m++] = (Step >> 16) & 0xff;
				WriteBuffer[m++] = (Step >> 24) & 0xff;
				WriteBuffer[m++] = Points & 0xff;
				WriteBuffer[m++] = (Points >> 8) & 0xff;
			}
			else if(i == 2)
			{
				WriteBuffer[m++] = Period & 0xff;
				WriteBuffer[m++] = (Period >> 8) & 0xff;
				WriteBuffer[m++] = (Period >> 16) & 0xff;
				WriteBuffer[m++] = (Period >> 24) & 0xff;
				WriteBuffer[m++] = PulseWidth & 0xff;
				WriteBuffer[m++] = (PulseWidth >> 8) & 0xff;
				WriteBuffer[m++] = (PulseWidth >> 16) & 0xff;
				WriteBuffer[m++] = (PulseWidth >> 24) & 0xff;
				WriteBuffer[m++] = PulseDelay & 0xff;
				WriteBuffer[m++] = (PulseDelay >> 8) & 0xff;
				WriteBuffer[m++] = (PulseDelay >> 16) & 0xff;
				WriteBuffer[m++] = (PulseDelay >> 24) & 0xff;
				WriteBuffer[m++] = PulseNumber;
			}
			else if(i == 3)
			{
				WriteBuffer[m++] = Ampitulde;
			}
			else if(i == 4)
			{
				WriteBuffer[m++] = IQStep & 0xff;
				WriteBuffer[m++] = (IQStep >> 8) & 0xff;
				WriteBuffer[m++] = (IQStep >> 16) & 0xff;
				WriteBuffer[m++] = (IQStep >> 24) & 0xff;
			}
		}

		WriteBuffer[5] = m - 6;

		WriteBuffer[6 + WriteBuffer[5]] = 0xaa;
		WriteBuffer[7 + WriteBuffer[5]] = 0xaa;

		WriteBuffer[8 + WriteBuffer[5]] = 0x10;
		WriteBuffer[9 + WriteBuffer[5]] = 0x03;
	}

	DWORD lpNumberOfBytesWritten = 0;

	BOOL result = WriteFile(hDongle, &WriteBuffer, bufferSize, &lpNumberOfBytesWritten, NULL);

	BYTE ReadBuf[bufferSize];
	DWORD lpNumberOfBytesRead = 0;

	result =ReadFile(hDongle, &ReadBuf, bufferSize, &lpNumberOfBytesRead, NULL);
		
	return ReadBuf[3];

}