// sdktest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

LOG_GLOBAL_INIT("sdktest.log");

int AnalyzeCmdTest(PDTL_WIRELESSNET_DEVICEINFO pInfo,int num);

#define CMDSTR_UNINSTALL	L"uninstall"
#define CMDSTR_UPDATEBEST	L"updatebest"
#define CMDSTR_UPDATELAST	L"updatelast"

CDtlApiInterface	g_dtlapi;

int _tmain(int argc, _TCHAR* argv[])
{
	int		ret				= 0;
	WCHAR	szMod[MAX_PATH]	= {0};

	LOGINOUT;
	PRT("正在获取设备列表...");

	::GetModuleFileName(NULL, szMod, MAX_PATH);
	::PathAppend(szMod,L"..\\dtlsdk\\dtlapi.dll");

	if ( !::PathFileExists(szMod) )
		ZeroMemory(szMod,MAX_PATH*sizeof(WCHAR));
	if ( !g_dtlapi.InitInterface(szMod) )
	{
		PRT("error load[%S]",szMod);
		return ret;
	}

	PDTL_WIRELESSNET_DEVICEINFO pInfo = NULL;
	int num = g_dtlapi.GetWirelessNetArray(&pInfo);
	PRT("\n获取到的设备个数为[%d]\n",num);
	for ( int i = 0; i < num; i++ )
	{
		PRT("\n---------%d/%d---------",i,num);
		PRT("szName[%S]",pInfo[i].szName);
		PRT("guid[%S]",GUIDToString(pInfo[i].guid).c_str());
		PRT("szHwid[%S]",pInfo[i].szHwid);
		PRT("bIsWirelessNet[%d]",pInfo[i].bIsWirelessNet);
		PRT("bHasDriver[%d]",pInfo[i].bHasDriver);
		PRT("bEnableUpdateBestDriver[%d]",pInfo[i].bEnableUpdateBestDriver);
		PRT("bEnableUpdateLastDriver[%d]",pInfo[i].bEnableUpdateLastDriver);
		PRT("bEnableUninstDriver[%d]",pInfo[i].bEnableUninstDriver);
	}

	if ( num > 0 )
	{
		ret = AnalyzeCmdTest(pInfo,num);
	}
	else
	{
		system("pause");
	}

	g_dtlapi.FreeWirelessNetArray();
	g_dtlapi.CloseInterface();
	PRT("apicmd ret[%d].",ret);
	return ret;
}

int OperationCmd(WCHAR* szCmd,HDEVICE hDevice)
{
	int ret = 0;
	if ( szCmd && wcslen(szCmd) > 0 && hDevice )
	{
		if ( 0 == _wcsicmp(szCmd,CMDSTR_UNINSTALL) )
		{
			ret = g_dtlapi.UninstallDriver(hDevice);
		}
		else if ( 0 == _wcsicmp(szCmd,CMDSTR_UPDATEBEST) )
		{
			ret = g_dtlapi.UpdateBestDriver(hDevice);
		}
		else if ( 0 == _wcsicmp(szCmd,CMDSTR_UPDATELAST) )
		{
			ret = g_dtlapi.UpdateLastDriver(hDevice);
		}
	}
	return ret;
}

int AnalyzeCmdTest(PDTL_WIRELESSNET_DEVICEINFO pInfo,int num)
{
	int ret = 0;
	do 
	{
		PRT("\n输入[%S 0]卸载第0个设备的驱动",CMDSTR_UNINSTALL);
		PRT("输入[%S 0]更新第0个设备的推荐驱动",CMDSTR_UPDATEBEST);
		PRT("输入[%S 0]更新第0个设备的最新驱动",CMDSTR_UPDATELAST);
		PRT("输入[quit 0]退出\n");
		WCHAR cmd[MAX_PATH] = {0};
		int index = 0;
		scanf_s("%S %d",cmd,MAX_PATH,&index);	
		if ( 0 == _wcsicmp(cmd,L"quit") )
		{
			break;
		}
		else if ( index < num )
		{
			ret = OperationCmd(cmd,pInfo[index].hDevice);
			switch(ret)
			{
			case DTL_RESULT_ERROR:PRT("操作失败");break;
			case DTL_RESULT_SUCESS:PRT("操作成功");break;
			case DTL_RESULT_REBOOT:PRT("操作成功,需要重启");break;
			}
		}
	} while (true);
	return ret;
}