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

Configures device interface/alt settings.

config-interface 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. Claims the specified interface. (re-define at top of example)
  4. Finds the specified interface/alt setting. (re-define at top of example)
  5. Finds the specified read and write pipes. (re-define at top of example)
  6. Sets the specified alternate setting as the "active" interface.
  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!
Interface/alt setting number 00h/00h found!
Read pipe 81h found!
Write pipe 01h found!
Alternate setting 00h selected!
#include "examples.h"
// Example configuration:
#define INTF_NUMBER 0x00
#define ALT_SETTING_NUMBER 0x00
#define EP_READ 0x81
#define EP_WRITE 0x01
// Globals:
DWORD __cdecl main(int argc, char* argv[])
{
KLST_HANDLE deviceList = NULL;
KLST_DEVINFO_HANDLE deviceInfo = NULL;
KUSB_HANDLE handle = NULL;
DWORD errorCode = ERROR_SUCCESS;
WINUSB_PIPE_INFORMATION readInfo, writeInfo, pipeInfo;
USB_INTERFACE_DESCRIPTOR interfaceInfo;
UCHAR pipeIndex = 0;
// Find the test device. Uses "vid/pid=hhhh" arguments supplied
// on the command line. (default is: vid=04D8 pid=FA2E)
if (!Examples_GetTestDevice(&deviceList, &deviceInfo, argc, argv))
return GetLastError();
LibK_LoadDriverAPI(&Usb, deviceInfo->DriverID);
// Initialize the device
if (!Usb.Init(&handle, deviceInfo))
{
errorCode = GetLastError();
printf("Usb.Init failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
printf("Device opened successfully!\n");
/*
Claim the interface. You can use the claim/release interface functions
cooperatively in libusbK application to ensure only one application is
usng the interface at a time.
*/
if (!Usb.ClaimInterface(handle, INTF_NUMBER, FALSE))
{
errorCode = GetLastError();
if (errorCode == ERROR_NO_MORE_ITEMS)
printf("Interface number %02Xh does not exists.\n", INTF_NUMBER);
else
printf("Usb.ClaimInterface failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
/*
Get the interface descriptor for the specified alternate settings number.
*/
if (!Usb.QueryInterfaceSettings(handle, ALT_SETTING_NUMBER, &interfaceInfo))
{
errorCode = GetLastError();
if (errorCode == ERROR_NO_MORE_ITEMS)
printf("Alt Setting number %02Xh does not exists.\n", ALT_SETTING_NUMBER);
else
printf("Usb.QueryInterfaceSettings failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
printf("Interface/alt setting number %02Xh/%02Xh found!\n",
interfaceInfo.bInterfaceNumber, interfaceInfo.bAlternateSetting);
/*
Get the pipe information for the specified read & write pipe IDs.
*/
memset(&readInfo, 0, sizeof(readInfo));
memset(&writeInfo, 0, sizeof(writeInfo));
while (Usb.QueryPipe(handle, ALT_SETTING_NUMBER, pipeIndex++, &pipeInfo))
{
if (pipeInfo.PipeId == EP_READ)
memcpy(&readInfo, &pipeInfo, sizeof(readInfo));
else if (pipeInfo.PipeId == EP_WRITE)
memcpy(&writeInfo, &pipeInfo, sizeof(writeInfo));
}
if (readInfo.PipeId == 0)
{
printf("Read pipe %02Xh not found.\n", EP_READ);
goto Done;
}
else
printf("Read pipe %02Xh found!\n", readInfo.PipeId);
if (writeInfo.PipeId == 0)
{
printf("Write pipe %02Xh not found.\n", EP_WRITE);
goto Done;
}
else
printf("Write pipe %02Xh found!\n", writeInfo.PipeId);
/*
Set the alternate setting number. This is only required if the device supports
alternate settings, but it is a standard request and all usb devices must
support it to be compliant with usb specs.
*/
if (!Usb.SetCurrentAlternateSetting(handle, interfaceInfo.bAlternateSetting))
{
errorCode = GetLastError();
printf("Usb.SetCurrentAlternateSetting failed. bAlternateSetting: %u, ErrorCode: %08Xh\n",
interfaceInfo.bAlternateSetting, errorCode);
goto Done;
}
else
{
printf("Alternate setting %02Xh selected!\n", interfaceInfo.bAlternateSetting);
}
/*
.. The device is open, configured and ready for use.
*/
Done:
if (handle) Usb.Free(handle);
// Free the device list
// if deviceList is invalid (NULL), has no effect
LstK_Free(deviceList);
return errorCode;
}