libusbK 
3.0
Library Documentation
© 2011-2021 Travis Lee Robinson. All rights reserved.
xfer-control.c

Sends various vendor specific control requests synchronously.

xfer-control example:
  1. Finds a device by vid/pid (use vid=hhhh, pid=hhhhh and the command line).
  2. Initializes a new UsbK (usb device) handle.
  3. Sends a control request for the device descriptor.
  4. Sends a vendor-specific HostToDevice request to set the benchmark vendor buffer.
  5. Sends a vendor-specific DeviceToHost request to get the benchmark vendor buffer.
  6. Compares and reports before and after results of the benchmark vendor buffer.
  7. Frees the UsbK (usb device) handle.
  8. Frees the LstK (device list) handle created in step #1.
Console Output
Looking for device vid/pid 04D8/FA2E..
Using 04D8:FA2E (LUSBW1): Benchmark Device - Microchip Technology, Inc.
Device opened successfully!
vendorBuffer: ABC=ABC
#include "examples.h"
// Example configuration:
// Globals:
DWORD __cdecl main(int argc, char* argv[])
{
KLST_HANDLE deviceList = NULL; // device list handle (the list of device infos)
KLST_DEVINFO_HANDLE deviceInfo = NULL; // device info handle (the device list element)
KUSB_HANDLE usbHandle = NULL; // device interface usbHandle (the opened USB device)
DWORD errorCode = ERROR_SUCCESS;
BOOL success;
USB_DEVICE_DESCRIPTOR deviceDescriptor;
UCHAR vendorBuffer[8 + 1];
KUSB_SETUP_PACKET setupPacket;
if (!Examples_GetTestDevice(&deviceList, &deviceInfo, argc, argv))
return GetLastError();
LibK_LoadDriverAPI(&Usb, deviceInfo->DriverID);
if (!Usb.Init(&usbHandle, deviceInfo))
{
errorCode = GetLastError();
printf("Open device failed. Win32Error=%u (0x%08X)\n", errorCode, errorCode);
goto Done;
}
printf("Device opened successfully!\n");
/*
Use a standard control transfer to get the device descriptor. (DeviceToHost)
*/
// Setup packets are always 8 bytes (64 bits)
*((__int64*)&setupPacket) = 0;
// Fill the setup packet.
setupPacket.BmRequest.Dir = BMREQUEST_DIR_DEVICE_TO_HOST;
setupPacket.BmRequest.Type = BMREQUEST_TYPE_STANDARD;
setupPacket.BmRequest.Recipient = BMREQUEST_RECIPIENT_DEVICE;
setupPacket.Length = sizeof(deviceDescriptor);
success = Usb.ControlTransfer(usbHandle, *((WINUSB_SETUP_PACKET*)&setupPacket), (PUCHAR)&deviceDescriptor, sizeof(deviceDescriptor), NULL, NULL);
if (!success)
{
errorCode = GetLastError();
printf("Usb.ControlTransfer failed. Win32Error=%u (0x%08X)\n", errorCode, errorCode);
goto Done;
}
/*
Initialize the 8 byte benchmark vendor buffer
*/
memset(vendorBuffer, 0, sizeof(vendorBuffer));
vendorBuffer[0] = 'A';
vendorBuffer[1] = 'B';
vendorBuffer[2] = 'C';
/*
Use a vendor control transfer to set the benchmark vendor buffer. (HostToDevice)
*/
setupPacket.BmRequest.Dir = BMREQUEST_DIR_HOST_TO_DEVICE;
setupPacket.BmRequest.Type = BMREQUEST_TYPE_VENDOR;
setupPacket.BmRequest.Recipient = BMREQUEST_RECIPIENT_DEVICE;
setupPacket.Request = BM_COMMAND_SET_VBUF;
setupPacket.Length = sizeof(vendorBuffer) - 1;
success = Usb.ControlTransfer(usbHandle, *((WINUSB_SETUP_PACKET*)&setupPacket), vendorBuffer, sizeof(vendorBuffer) - 1, NULL, NULL);
if (!success)
{
errorCode = GetLastError();
printf("Usb.ControlTransfer failed. Win32Error=%u (0x%08X)\n", errorCode, errorCode);
goto Done;
}
/*
Print the value assigned to the benchmark vendor buffer.
*/
printf("vendorBuffer: %s=", vendorBuffer);
memset(vendorBuffer, 0, sizeof(vendorBuffer));
/*
Use a vendor control transfer to get the benchmark vendor buffer. (DeviceToHost)
*/
// Setup packets are always 8 bytes (64 bits)
*((__int64*)&setupPacket) = 0;
setupPacket.BmRequest.Dir = BMREQUEST_DIR_DEVICE_TO_HOST;
setupPacket.BmRequest.Type = BMREQUEST_TYPE_VENDOR;
setupPacket.BmRequest.Recipient = BMREQUEST_RECIPIENT_DEVICE;
setupPacket.Request = BM_COMMAND_GET_VBUF;
setupPacket.Length = sizeof(vendorBuffer) - 1;
success = Usb.ControlTransfer(usbHandle, *((WINUSB_SETUP_PACKET*)&setupPacket), vendorBuffer, sizeof(vendorBuffer) - 1, NULL, NULL);
if (!success)
{
errorCode = GetLastError();
printf("Usb.ControlTransfer failed. Win32Error=%u (0x%08X)\n", errorCode, errorCode);
goto Done;
}
/*
Print the value stored in the benchmark vendor buffer.
*/
printf("%s\n", vendorBuffer);
Done:
if (usbHandle) Usb.Free(usbHandle);
LstK_Free(deviceList);
return errorCode;
}