#region Copyright(c) Travis Robinson
#endregion
#define BMFW
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Test.Devices;
using libusbK;
using libusbK.Examples;
namespace Xfer.Stm
{
internal class Program
{
#region TODO USER: Set the test parameters for your device.
public static StmTestParameters Test = new StmTestParameters(0x04d8, 0xfa2e, 0, 0x81, 1024, null, -1, 4, 64);
#endregion
private static void Main()
{
bool success;
WINUSB_PIPE_INFORMATION_EX pipeInfo;
UsbK usb;
USB_INTERFACE_DESCRIPTOR interfaceDescriptor;
if (!Test.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor)) return;
if (Test.TransferBufferSize == -1)
Test.TransferBufferSize = pipeInfo.MaximumPacketSize*64;
#if BMFW
Console.WriteLine("Configuring for benchmark device..");
BM_TEST_TYPE testType = ((Test.PipeId & 0x80) > 0) ? BM_TEST_TYPE.READ : BM_TEST_TYPE.WRITE;
success = Benchmark.Configure(usb, BM_COMMAND.SET_TEST, interfaceDescriptor.bInterfaceNumber, ref testType);
if (!success)
{
Console.WriteLine("Bench_Configure failed.");
}
#endif
if (!Test.ShowTestReady()) goto Done;
KSTM_CALLBACK callback = new KSTM_CALLBACK();
StmK stm = new StmK(
usb.Handle,
pipeInfo.PipeId,
Test.TransferBufferSize,
Test.MaxPendingTransfers,
Test.MaxPendingIO,
ref callback,
KSTM_FLAG.USE_TIMEOUT | (KSTM_FLAG) 3000);
byte[] tempBuffer = new byte[Test.TransferBufferSize];
Thread.Sleep(0);
Test.Dcs.Start();
success = stm.Start();
long totalTransferCount = 0;
while (success)
{
uint transferred;
if ((pipeInfo.PipeId & 0x80) == 0x80)
{
success = stm.Read(tempBuffer, 0, tempBuffer.Length, out transferred);
if (!success) break;
}
else
{
success = stm.Write(tempBuffer, 0, tempBuffer.Length, out transferred);
if (!success) break;
}
string dataPrefix = String.Format(" Data Prefix: [{0:X2} {1:X2} {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2}] ",
tempBuffer[0],
tempBuffer[1],
tempBuffer[2],
tempBuffer[3],
tempBuffer[4],
tempBuffer[5],
tempBuffer[6],
tempBuffer[7]);
Console.WriteLine(
totalTransferCount > Test.MaxTransfersTotal
? "#{0}: [Stream Stopped] {1} transferred. {2}"
: "#{0}: {1} transferred. {2}",
totalTransferCount.ToString("0000"),
transferred,
dataPrefix);
totalTransferCount++;
if (totalTransferCount == Test.MaxTransfersTotal)
success = stm.Stop(3000);
}
Console.WriteLine("Done. TotalTransfers:{0} ErrorCode:{1:X8}h", totalTransferCount, Marshal.GetLastWin32Error());
stm.Free();
Done:
usb.Free();
}
}
}