#include "examples.h"
#define INTF_NUMBER 0x00
#define ALT_SETTING_NUMBER 0x00
#define EP_READ 0x81
#define EP_WRITE 0x01
KUSB_DRIVER_API Usb;
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;
if (!Examples_GetTestDevice(&deviceList, &deviceInfo, argc, argv))
return GetLastError();
LibK_LoadDriverAPI(&Usb, deviceInfo->DriverID);
if (!Usb.Init(&handle, deviceInfo))
{
errorCode = GetLastError();
printf("Usb.Init failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
printf("Device opened successfully!\n");
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;
}
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);
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);
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);
}
Done:
if (handle) Usb.Free(handle);
LstK_Free(deviceList);
return errorCode;
}