コード例 #1
0
ファイル: apitest.c プロジェクト: JanD1943/ndas4windows
/*
main function
returns non-zero if any function fails.
*/
int __cdecl wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
{
	BOOL bResult;
	DWORD dwError = 0;
	DWORD i;

	DWORD dwVersion;

	HNDAS hNDAS = NULL;
	NDASCOMM_CONNECTION_INFO ci;
	BYTE DeviceID[6];
	DWORD dwUnitNo;

	PBYTE Buffer;
	DWORD dwBufferLen;
	BYTE data[512]; /* 1 sector sized buffer */
	INT64 i64Location;
	UINT ui64SectorCount;
	NDASCOMM_IDE_REGISTER IdeRegister;
	BYTE pbData[8];
	DWORD cbData = sizeof(pbData);
	NDASCOMM_VCMD_PARAM param_vcmd;
	UINT32 uiTimeValue;
	BOOL bEnableTimer;

	NDAS_DEVICE_HARDWARE_INFO dinfo;

	BYTE vid;

	/* simple check parameter */
	if(2 != argc)
	{
		wprintf(
			L"usage : apitest.exe ID-KEY\n"
			L"\n"
			L"ID-KEY : 20 chars of id and 5 chars of key of the NDAS Device ex(01234ABCDE56789FGHIJ13579)\n"
			L"ex : apitest.exe 01234ABCDE56789FGHIJ13579\n"
			);
		wprintf(L"\n");
		return -1;
	}

	wprintf(L"\n\n\n* Start NDASComm API test on the NDAS Device : %s\n", argv[1]);

	wprintf(L"* Initialize NdasComm library : NdasCommInitialize()\n");
	API_CALL(NdasCommInitialize());

	wprintf(L"* Get API Version : NdasCommGetAPIVersion()\n");
	API_CALL_JMP(dwVersion = NdasCommGetAPIVersion(), out);
	wprintf(L"- Version : Major %d, Minor %d\n",
		(int)LOWORD(dwVersion), (int)HIWORD(dwVersion));

//////////////////////////////////////////////////////////////
//#define	TEST_SET_PASSWORD

#ifdef TEST_SET_PASSWORD
	wprintf(L"### TESTING 'Set user password' ###\n");

	ZeroMemory(&ci, sizeof(ci));
	ci.Size = sizeof(NDASCOMM_CONNECTION_INFO);
	ci.AddressType = NDASCOMM_CIT_NDAS_IDW; /* wide char set */
	ci.UnitNo = 0; /* Use first Unit Device */
	ci.WriteAccess = TRUE; /* Connect with read-write privilege */
	ci.Protocol = NDASCOMM_TRANSPORT_LPX; /* Use LPX protocol */

	CopyMemory(
		ci.OEMCode.Bytes,
		NdasOemCodeX1, 
		sizeof(NdasOemCodeX1)); /* HASH_KEY_USER_X1 */ /* Use default password */

	CopyMemory(
		ci.PrivilegedOEMCode.Bytes, 
		NdasPrivOemCodeX1, 
		sizeof(NdasPrivOemCodeX1)); /* HASH_KEY_SUPER_X1 */ /* Log in as super user */

	ci.LoginType = NDASCOMM_LOGIN_TYPE_NORMAL; /* Normal operations */

	wcsncpy(ci.NdasIdW.Id, argv[1], 20); /* ID */
	wcsncpy(ci.NdasIdW.Key, argv[1] +20, 5); /* Key */

	wprintf(L"* Connect to the NDAS Device : NdasCommConnect()\n");
	API_CALL_JMP(
		hNDAS = NdasCommConnect(
		&ci,
		0 /* synchronous mode */,
		NULL /* no connection hint */
		),
		out);

	wprintf(L"* Setting user password : NdasCommVendorCommand()\n");

	*(UINT64 *)param_vcmd.SET_SUPERVISOR_PW.SupervisorPassword = HASH_KEY_SUPER_X1;
	bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_set_supervisor_pw, &param_vcmd, NULL, 0, NULL, 0);
	if(!bResult)
	{
		if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != GetLastError())
		{
			API_CALL_JMP(FALSE && "NdasCommVendorCommand", out);
		}
		wprintf(L"- Not supported for this Hardware version\n");
	}

	*(UINT64 *)param_vcmd.SET_USER_PW.UserPassword = HASH_KEY_USER_X1;
	bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_set_user_pw, &param_vcmd, NULL, 0, NULL, 0);
	if(!bResult)
	{
		if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != GetLastError())
		{
			API_CALL_JMP(FALSE && "NdasCommVendorCommand", out);
		}
		wprintf(L"- Not supported for this Hardware version\n");
	}

#define TEST_SET_PASSWORD_RESET
#ifdef TEST_SET_PASSWORD_RESET
	wprintf(L"* Resetting : NdasCommVendorCommand()\n");
	bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_reset, &param_vcmd, NULL, 0, NULL, 0);
	if(!bResult)
	{
		if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != GetLastError())
		{
			API_CALL_JMP(FALSE && "NdasCommVendorCommand", out);
		}
		wprintf(L"- Not supported for this Hardware version\n");
	}
#else
	wprintf(L"* Disconnect the connection from the NDAS Device : NdasCommDisconnect()\n");
	API_CALL_JMP(NdasCommDisconnect(hNDAS), out);
#endif //TEST_SET_PASSWORD_RESET
	wprintf(L"### TESTED 'Set user passoword' ###\n");

#endif //TEST_SET_PASSWORD
//////////////////////////////////////////////////////////////

	wprintf(L"* Initialize connection info to create connection to the NDAS Device\n");
	ZeroMemory(&ci, sizeof(ci));
	ci.Size = sizeof(NDASCOMM_CONNECTION_INFO);
	ci.AddressType = NDASCOMM_CIT_NDAS_IDW; /* wide char set */
	ci.UnitNo = 0; /* Use first Unit Device */
	ci.WriteAccess = TRUE; /* Connect with read-write privilege */
	ci.Protocol = NDASCOMM_TRANSPORT_LPX; /* Use LPX protocol */
	ci.OEMCode.UI64Value = 0; /* Use default password */
	ci.PrivilegedOEMCode.UI64Value = 0; /* Log in as normal user */
	ci.LoginType = NDASCOMM_LOGIN_TYPE_NORMAL; /* Normal operations */
	wcsncpy(ci.Address.NdasIdW.Id, argv[1], 20); /* ID */
	wcsncpy(ci.Address.NdasIdW.Key, argv[1] + 20, 5); /* Key */

	wprintf(L"* Connect to the NDAS Device : NdasCommConnect()\n");
	API_CALL_JMP( hNDAS = NdasCommConnect(&ci), out );

	wprintf(L"* Retrieve NDAS Device ID & unit number : NdasCommGetDeviceID()\n");
	API_CALL_JMP(NdasCommGetDeviceID(hNDAS, NULL, DeviceID, &dwUnitNo, &vid), out);
	wprintf(L"- DeviceID : %02X%02X%02X%02X%02X%02X, Unit No. : %d\n",
		DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5],
		(int)dwUnitNo);

	wprintf(L"* Retrieve the address of the host attached to the NDAS Device : NdasCommGetHostAddress()\n");
	API_CALL_JMP(NdasCommGetHostAddress(hNDAS, NULL, &dwBufferLen), out);
	wprintf(L"- buffer length : %d\n", dwBufferLen);
	Buffer = malloc(dwBufferLen);
	API_CALL_JMP(NdasCommGetHostAddress(hNDAS, Buffer, &dwBufferLen), out);
	wprintf(L"- Host Address : ");
	for(i = 0 ; i < dwBufferLen; i++)
	{
		wprintf(L"%02X", (UINT)Buffer[i]);
	}
	wprintf(L"\n");
	free(Buffer);

	ui64SectorCount = 1;
	i64Location = 0;

	wprintf(L"* Read %d sector(s) of data from Address %d : NdasCommBlockDeviceRead()\n",
		ui64SectorCount, i64Location);
	API_CALL_JMP(NdasCommBlockDeviceRead(hNDAS, i64Location, ui64SectorCount, data), out);

	i64Location = 1;
	wprintf(L"* Write %d sector(s) of data to Address %d : NdasCommBlockDeviceWriteSafeBuffer()\n",
		ui64SectorCount, i64Location);
	API_CALL_JMP(NdasCommBlockDeviceWriteSafeBuffer(hNDAS, i64Location, ui64SectorCount, data), out);

	ui64SectorCount = 2;
	i64Location = 2;
	wprintf(L"* Verify %d sector(s) from Address %d : NdasCommBlockDeviceVerify()\n",
		ui64SectorCount, i64Location);
	API_CALL_JMP(NdasCommBlockDeviceVerify(hNDAS, i64Location, ui64SectorCount), out);

	IdeRegister.command.command = 0xEC; /* WIN_IDENTIFY */
	IdeRegister.device.dev = 0;
	wprintf(L"* Identify the NDAS Unit Device : NdasCommIdeCommand()\n");
	API_CALL_JMP(NdasCommIdeCommand(hNDAS, &IdeRegister, NULL, 0, data, 512), out);
	/* data[] now has 512 bytes of identified data as per ANSI NCITS ATA6 rev.1b spec */


	ZeroMemory(&dinfo, sizeof(NDAS_DEVICE_HARDWARE_INFO));
	dinfo.Size = sizeof(NDAS_DEVICE_HARDWARE_INFO);
	API_CALL_JMP(NdasCommGetDeviceHardwareInfo(hNDAS,&dinfo), out);
	wprintf(L"Hardware Version : %d\n", dinfo.HardwareVersion);

	wprintf(L"* get standby timer : NdasCommVendorCommand()\n");
	bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_get_ret_time, &param_vcmd, NULL, 0, NULL, 0);
	if(!bResult)
	{
		if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != GetLastError())
		{
			API_CALL_JMP(FALSE && "NdasCommVendorCommand", out);
		}
		wprintf(L"- Not supported for this Hardware version\n");
	}
	else
	{
		uiTimeValue = param_vcmd.GET_STANDBY_TIMER.TimeValue;
		bEnableTimer = param_vcmd.GET_STANDBY_TIMER.EnableTimer;
		wprintf(L"- standby timer : %d, enable : %d\n", uiTimeValue, bEnableTimer);

		param_vcmd.SET_STANDBY_TIMER.TimeValue = uiTimeValue;
		param_vcmd.SET_STANDBY_TIMER.EnableTimer = bEnableTimer ? 0 : 1;
		wprintf(L"* set standby timer : NdasCommVendorCommand()\n");
		API_CALL_JMP(NdasCommVendorCommand(hNDAS, ndascomm_vcmd_set_ret_time, &param_vcmd, NULL, 0, NULL, 0), out);

		uiTimeValue = param_vcmd.SET_STANDBY_TIMER.TimeValue;
		bEnableTimer = param_vcmd.SET_STANDBY_TIMER.EnableTimer;
		wprintf(L"- standby timer : %d, enable : %d\n", uiTimeValue, bEnableTimer);
	}

	wprintf(L"* Disconnect the connection from the NDAS Device : NdasCommDisconnect()\n");
	API_CALL_JMP(NdasCommDisconnect(hNDAS), out);

	wprintf(L"* Uninitialize NDASComm API : NdasCommUninitialize()\n");

out:
	if(hNDAS)
	{
		NdasCommDisconnect(hNDAS);
		hNDAS = NULL;
	}

	API_CALL(NdasCommUninitialize());
	return GetLastError();
}
コード例 #2
0
ファイル: apitest.cpp プロジェクト: yzx65/ndas4windows
/*
main function
returns non-zero if any function fails.
*/
int __cdecl main(int argc, char *argv[])
{
	DWORD dwError = 0;
	BOOL bResult;
	HNDAS hNdas;
	BYTE data[512]; // 1 sector sized buffer

	// simple check parameter
	if(2 != argc)
	{
		printf(
			"usage : apitest.exe ID-KEY\n"
			"\n"
			"ID-KEY : 20 chars of id and 5 chars of key of the NDAS Device ex(01234ABCDE56789FGHIJ13579)\n"
			"ex : apitest.exe 01234ABCDE56789FGHIJ13579\n"
			);
		printf("\n");
		return -1;
	}

	printf("\n\n\n* Start NDASComm API test on the NDAS Device : %s\n", argv[1]);

	printf("* Initialize NdasComm library : NdasCommInitialize()\n");
	API_CALL(NdasCommInitialize());

	DWORD dwVersion;
	printf("* Get API Version : NdasCommGetAPIVersion()\n");
	API_CALL(dwVersion = NdasCommGetAPIVersion());
	printf("- Version : Major %d, Minor %d\n",
		(int)LOWORD(dwVersion), (int)HIWORD(dwVersion));

	printf("* Initialize connection info to create connection to the NDAS Device\n");
	NDASCOMM_CONNECTION_INFO ci;
	ZeroMemory(&ci, sizeof(ci));
	ci.address_type = NDASCOMM_CONNECTION_INFO_TYPE_ID_A; // ASCII char set
	ci.UnitNo = 0; // Use first Unit Device
	ci.bWriteAccess = TRUE; // Connect with read-write privilege
	ci.protocol = NDASCOMM_TRANSPORT_LPX; // Use LPX protocol
	ci.ui64OEMCode = 0; // Use default password
	ci.bSupervisor = FALSE; // Log in as normal user
	ci.login_type = NDASCOMM_LOGIN_TYPE_NORMAL; // Normal operations
	strncpy(ci.DeviceIDA.szDeviceStringId, argv[1], 20); // ID
	strncpy(ci.DeviceIDA.szDeviceStringKey, argv[1] +20, 5); // Key

	HNDAS hNDAS;
	printf("* Connect to the NDAS Device : NdasCommConnect()\n");
	API_CALL(
		hNDAS = NdasCommConnect(
			&ci,
			0 /* synchronous mode */,
			NULL /* no connection hint */
			)
		);

	BYTE DeviceID[6];
	DWORD UnitNo;
	printf("* Retrieve NDAS Device ID & unit number : NdasCommGetDeviceID()\n");
	API_CALL(NdasCommGetDeviceID(hNDAS, DeviceID, &UnitNo));
	printf("- DeviceID : %02X%02X%02X%02X%02X%02X, Unit No. : %d\n",
		DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5],
		(int)UnitNo);

	PBYTE Buffer;
	DWORD BufferLen;
	printf("* Retrieve the address of the host attached to the NDAS Device : NdasCommGetHostAddress()\n");
	API_CALL(NdasCommGetHostAddress(hNDAS, NULL, &BufferLen));
	printf("- buffer length : %d\n", BufferLen);
	Buffer = new BYTE[BufferLen];
	API_CALL(NdasCommGetHostAddress(hNDAS, Buffer, &BufferLen));
	printf("- Host Address : ");
	for(DWORD i = 0 ; i < BufferLen; i++)
	{
		printf("%02X", (UINT)Buffer[i]);
	}
	printf("\n");
	delete [] Buffer;

	INT64 i64Location;
	UINT ui64SectorCount;

	ui64SectorCount = 1;
	i64Location = 0;

	printf("* Read %d sector(s) of data from Address %d : NdasCommBlockDeviceRead()\n",
		ui64SectorCount, i64Location);
	API_CALL(NdasCommBlockDeviceRead(hNDAS, i64Location, ui64SectorCount, data));

	i64Location = 1;
	printf("* Write %d sector(s) of data to Address %d : NdasCommBlockDeviceWriteSafeBuffer()\n",
		ui64SectorCount, i64Location);
	API_CALL(NdasCommBlockDeviceWriteSafeBuffer(hNDAS, i64Location, ui64SectorCount, data));

	ui64SectorCount = 2;
	i64Location = 2;
	printf("* Verify %d sector(s) from Address %d : NdasCommBlockDeviceVerify()\n",
		ui64SectorCount, i64Location);
	API_CALL(NdasCommBlockDeviceVerify(hNDAS, i64Location, ui64SectorCount));

	NDASCOMM_IDE_REGISTER IdeRegister;
	IdeRegister.command.command = 0xEC; // WIN_IDENTIFY
	printf("* Identify the NDAS Unit Device : NdasCommIdeCommand()\n");
	API_CALL(NdasCommIdeCommand(hNDAS, &IdeRegister, NULL, 0, data, 512));
	// data[] now has 512 bytes of identified data as per ANSI NCITS ATA6 rev.1b spec

	BYTE pbData[8];
	const DWORD cbData = sizeof(pbData);


	API_CALL(NdasCommGetDeviceInfo(hNDAS,ndascomm_handle_info_hw_version, pbData, cbData));
	printf("Hardware Version : %d\n", *(BYTE*)pbData);

	NDASCOMM_VCMD_PARAM param_vcmd;
	printf("* get standby timer : NdasCommVendorCommand()\n");
	bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_get_ret_time, &param_vcmd, NULL, 0, NULL, 0);
	if(!bResult)
	{
		if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != ::GetLastError())
		{
			API_CALL(FALSE && "NdasCommVendorCommand");
		}
		printf("- Not supported for this Hardware version\n");
	}
	else
	{
		UINT32 TimeValue = param_vcmd.GET_STANDBY_TIMER.TimeValue;
		BOOL EnableTimer = param_vcmd.GET_STANDBY_TIMER.EnableTimer;
		printf("- standby timer : %d, enable : %d\n", TimeValue, EnableTimer);

		param_vcmd.SET_STANDBY_TIMER.TimeValue = TimeValue;
		param_vcmd.SET_STANDBY_TIMER.EnableTimer = EnableTimer ? 0 : 1;
		printf("* set standby timer : NdasCommVendorCommand()\n");
		API_CALL(NdasCommVendorCommand(hNDAS, ndascomm_vcmd_set_ret_time, &param_vcmd, NULL, 0, NULL, 0));

		TimeValue = param_vcmd.SET_STANDBY_TIMER.TimeValue;
		EnableTimer = param_vcmd.SET_STANDBY_TIMER.EnableTimer;
		printf("- standby timer : %d, enable : %d\n", TimeValue, EnableTimer);
	}

	printf("* Disconnect the connection from the NDAS Device : NdasCommDisconnect()\n");
	API_CALL(NdasCommDisconnect(hNDAS));

	printf("* Uninitialize NDASComm API : NdasCommUninitialize()\n");
	API_CALL(NdasCommUninitialize());

	return 0;
}