Enables auto suspend, sets suspend timeout and transfers synchronously.
DWORD __cdecl main(int argc, char* argv[])
{
DWORD errorCode = ERROR_SUCCESS;
BOOL success;
ULONG polLength;
BYTE polAutoSuspend;
ULONG polSuspendDelay;
return GetLastError();
{
printf(
"[Warning] libusb-win32 driver (libusb0.sys) does not support power\n"
" management.\n");
}
else
{
printf(
"[Note] If the DeviceIdleEnabled policy is not set in the .inf file when\n"
" the device is installed, the AUTO_SUSPEND policy will be ignored.\n");
{
printf(
"[Note] WinUSB behaves slightly different then libusbK because it reset\n"
" power policies back to their default values when the usb handle\n"
" is closed.\n");
}
}
if (!Usb.
Init(&usbHandle, deviceInfo))
{
errorCode = GetLastError();
printf("Init device failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
printf("Device opened successfully!\n");
polLength = 1;
success = Usb.
GetPowerPolicy(usbHandle, AUTO_SUSPEND, &polLength, &polAutoSuspend);
if (!success)
{
errorCode = GetLastError();
printf("GetPowerPolicy AUTO_SUSPEND failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
printf("AUTO_SUSPEND is currently %s.\n", (polAutoSuspend ? "enabled" : "disabled"));
polLength = 4;
success = Usb.GetPowerPolicy(usbHandle, SUSPEND_DELAY, &polLength, &polSuspendDelay);
if (!success)
{
errorCode = GetLastError();
printf("GetPowerPolicy SUSPEND_DELAY failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
if (polAutoSuspend)
{
printf("SUSPEND_DELAY is set to %d ms\n", polSuspendDelay);
}
polAutoSuspend = polAutoSuspend ? 0 : 1;
polLength = 1;
success = Usb.
SetPowerPolicy(usbHandle, AUTO_SUSPEND, polLength, &polAutoSuspend);
if (!success)
{
errorCode = GetLastError();
printf("SetPowerPolicy AUTO_SUSPEND=%u failed. ErrorCode: %08Xh\n", polAutoSuspend, errorCode);
goto Done;
}
printf("Set AUTO_SUSPEND = %s.\n", (polAutoSuspend ? "On" : "Off"));
if (polAutoSuspend)
{
polSuspendDelay *= 2;
if (!polSuspendDelay || polSuspendDelay > 12800)
polSuspendDelay = 100;
polLength = 4;
success = Usb.SetPowerPolicy(usbHandle, SUSPEND_DELAY, polLength, &polSuspendDelay);
if (!success)
{
errorCode = GetLastError();
printf("SetPowerPolicy SUSPEND_DELAY=%u failed. ErrorCode: %08Xh\n", polSuspendDelay, errorCode);
goto Done;
}
printf("Set SUSPEND_DELAY = %d ms\n", polSuspendDelay);
}
Done:
if (usbHandle) Usb.
Free(usbHandle);
return errorCode;
}