Esempio n. 1
0
        internal static ErrorCode SyncTransfer(UsbTransfer transferContext,
                                               IntPtr buffer,
                                               int offset,
                                               int length,
                                               int timeout,
                                               int isoPacketSize,
                                               out int transferLength)
        {
            if (ReferenceEquals(transferContext, null))
            {
                throw new NullReferenceException("Invalid transfer context.");
            }
            if (offset < 0)
            {
                throw new ArgumentException("must be >=0", "offset");
            }
            if (isoPacketSize == 0 && transferContext.EndpointBase.Type == EndpointType.Isochronous)
            {
                Info.UsbEndpointInfo endpointInfo = transferContext.EndpointBase.EndpointInfo;
                if (endpointInfo != null)
                {
                    isoPacketSize = endpointInfo.Descriptor.MaxPacketSize;
                }
            }
            lock (transferContext.mTransferLOCK)
            {
                transferLength = 0;

                int       transferred;
                ErrorCode ec;
                transferContext.Fill(buffer, offset, length, timeout, isoPacketSize);

                while (true)
                {
                    ec = transferContext.Submit();
                    if (ec != ErrorCode.Success)
                    {
                        return(ec);
                    }

                    ec = transferContext.Wait(out transferred);
                    if (ec != ErrorCode.Success)
                    {
                        return(ec);
                    }

                    transferLength += transferred;

                    if ((ec != ErrorCode.None || transferred != UsbEndpointBase.MaxReadWrite) ||
                        !transferContext.IncrementTransfer(transferred))
                    {
                        break;
                    }
                }

                return(ec);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates, fills and submits an asynchronous <see cref="UsbTransfer"/> context.
        /// </summary>
        /// <remarks>
        /// <note type="tip">This is a non-blocking asynchronous transfer function. This function returns immediately after the context is created and submitted.</note>
        /// </remarks>
        /// <param name="buffer">A caller-allocated buffer for the data that is transferred.</param>
        /// <param name="offset">Position in buffer that transferring begins.</param>
        /// <param name="length">Number of bytes, starting from thr offset parameter to transfer.</param>
        /// <param name="timeout">Maximum time to wait for the transfer to complete.</param>
        /// <param name="transferContext">On <see cref="ErrorCode.Success"/>, a new transfer context.</param>
        /// <returns><see cref="ErrorCode.Success"/> if the transfer context was created and <see cref="UsbTransfer.Submit"/> succeeded.</returns>
        /// <seealso cref="SubmitAsyncTransfer(object,int,int,int,out LibUsbDotNet.Main.UsbTransfer)"/>
        /// <seealso cref="NewAsyncTransfer"/>
        public virtual ErrorCode SubmitAsyncTransfer(IntPtr buffer, int offset, int length, int timeout, out UsbTransfer transferContext)
        {
            transferContext = CreateTransferContext();
            transferContext.Fill(buffer, offset, length, timeout);

            ErrorCode ec = transferContext.Submit();

            if (ec != ErrorCode.None)
            {
                transferContext.Dispose();
                transferContext = null;
                UsbError.Error(ec, 0, "SubmitAsyncTransfer Failed", this);
            }

            return(ec);
        }