Base class for async transfer context.
Inheritance: IDisposable, IAsyncResult
Example #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);
            }
        }
Example #2
0
 internal static ErrorCode SyncTransfer(UsbTransfer transferContext,
                                        IntPtr buffer,
                                        int offset,
                                        int length,
                                        int timeout,
                                        out int transferLength)
 {
     return(SyncTransfer(transferContext, buffer, offset, length, timeout, 0, out transferLength));
 }
Example #3
0
        /// <summary>
        /// Creates a <see cref="UsbTransfer"/> context for asynchronous transfers.
        /// </summary>
        /// <remarks>
        /// <para> This method returns a new, empty transfer context.  Unlike <see cref="SubmitAsyncTransfer(object,int,int,int,out LibUsbDotNet.Main.UsbTransfer)">SubmitAsyncTransfer</see>, this context is <c>not</c> filled and submitted.</para>
        /// <note type="tip">This is a non-blocking asynchronous transfer function. This function returns immediately after the context created.</note>
        /// </remarks>
        /// <returns>A new <see cref="UsbTransfer"/> context.</returns>
        /// <seealso cref="SubmitAsyncTransfer(System.IntPtr,int,int,int,out LibUsbDotNet.Main.UsbTransfer)"/>
        /// <seealso cref="SubmitAsyncTransfer(object,int,int,int,out LibUsbDotNet.Main.UsbTransfer)"/>
        public UsbTransfer NewAsyncTransfer()
        {
            UsbTransfer transfer = CreateTransferContext();

            return(transfer);
        }
Example #4
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);
        }
Example #5
0
 /// <summary>
 /// Synchronous bulk/interrupt transfer function.
 /// </summary>
 /// <param name="buffer">An <see cref="IntPtr"/> to a caller-allocated buffer.</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="transferLength">Number of bytes actually transferred.</param>
 /// <returns>True on success.</returns>
 public virtual ErrorCode Transfer(IntPtr buffer, int offset, int length, int timeout, out int transferLength)
 {
     return(UsbTransfer.SyncTransfer(TransferContext, buffer, offset, length, timeout, out transferLength));
 }
        /// <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;
        }
Example #7
0
 internal Handle(UsbTransfer context, byte[] data)
 {
     Context = context;
     Data    = data;
 }
 internal Handle(UsbTransfer context, byte[] data)
 {
     Context = context;
     Data = data;
 }
        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;
            }
        }
 internal static ErrorCode SyncTransfer(UsbTransfer transferContext,
                                IntPtr buffer,
                                int offset,
                                int length,
                                int timeout,
                                out int transferLength)
 {
     return SyncTransfer(transferContext, buffer, offset, length, timeout, 0, out transferLength);
 }
Example #11
0
 /// <summary>
 /// Fonction raccourci permettant de vérifier qu'il n'y ait pas d'erreurs
 /// </summary>
 /// <param name="ec">Code d'erreur utilisé</param>
 /// <param name="bytes">Bits lus</param>
 /// <param name="theoricLength">Longueur théorique du buffer</param>
 public void checkErrors(ErrorCode ec, UsbTransfer bytes, int theoricLength)
 {
     if (bytes.Transmitted != theoricLength && ec != ErrorCode.None) {
         throw new Exception("Erreur de lecture !");
     }
 }