Inheritance: IDisposable
Example #1
0
        public UsbStream(UsbK usb,
                         byte pipeId,
                         int maxTransferSize,
                         int maxPendingTransfers,
                         int maxPendingIo,
                         bool useTimeout,
                         UInt16 timeout)
        {
            mUsb = usb;
            mPipeId = pipeId;

            if (UseCallbacks)
            {
                mCallbacks.Complete = StmComplete;
                if (((mPipeId & AllKConstants.USB_ENDPOINT_DIRECTION_MASK) > 0))
                {
                    mCallbacks.Submit = StmSubmitRead;
                }
                else
                {
                    mCallbacks.Submit = StmSubmitWrite;
                }
            }
            KSTM_FLAG flags = useTimeout ? KSTM_FLAG.USE_TIMEOUT | (KSTM_FLAG) timeout : KSTM_FLAG.NONE;

            mStm = new StmK(mUsb.Handle,
                            pipeId,
                            maxTransferSize,
                            maxPendingTransfers,
                            maxPendingIo,
                            ref mCallbacks,
                            flags);
        }
Example #2
0
        private static void Main()
        {
            bool success;
            WINUSB_PIPE_INFORMATION pipeInfo;
            UsbK usb;
            USB_INTERFACE_DESCRIPTOR interfaceDescriptor;

            // Find and configure the device.
            if (!Test.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor)) return;
            if (Test.TransferBufferSize == -1)
                Test.TransferBufferSize = pipeInfo.MaximumPacketSize*64;

#if BMFW
            // TODO FOR USER: Remove this block if not using benchmark firmware.
            // This configures devices running benchmark firmware for streaming DeviceToHost transfers.
            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);
            // This is just a counter/timer for statistics gathering.
            Test.Dcs.Start();
            success = stm.Start();

            long totalTransferCount = 0;
            while (success)
            {
                int 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();
        }
Example #3
0
 protected override void Dispose(bool disposing)
 {
     if (!mbDisposed)
     {
         mStm.Dispose();
         mStm = null;
         mUsb = null;
         mbDisposed = true;
     }
     base.Dispose(disposing);
 }