Exemplo n.º 1
0
        internal int setupAsync(TransferContext transferContext)
        {
            int ret = -1;

            lock (oLockTransferContext)
            {
                transferContext.Reset();
                switch (mEpType)
                {
                case EndpointTypes.Bulk:
                    ret = LibUsbAPI.usb_bulk_setup_async(mUsbDevice.Handle, ref transferContext.mContext, EpNum);
                    break;

                case EndpointTypes.Interrupt:
                    ret = LibUsbAPI.usb_interrupt_setup_async(mUsbDevice.Handle, ref transferContext.mContext, EpNum);
                    break;

                case EndpointTypes.Isochronous:
                    ret = LibUsbAPI.usb_isochronous_setup_async(mUsbDevice.Handle, ref transferContext.mContext, EpNum, mPacketSize);
                    break;
                }
                if (ret < 0 || !transferContext.mContext.IsValid)
                {
                    UsbGlobals.Error(this, UsbGlobals.LastError, "setupAsync", ret);
                }

                return(ret);
            }
        }
Exemplo n.º 2
0
        internal int reapAsyncNoCancel(TransferContext transferContext)
        {
            int ret = -1;

            lock (oLockTransferContext)
            {
                if (transferContext.mbAsyncCancelled)
                {
                    return((int)ErrorCodes.ETHREADABORT);
                }
            }
            ret = LibUsbAPI.usb_reap_async_nocancel(transferContext.mContext, transferContext.mTimeout);

            lock (oLockTransferContext)
            {
                if (ret < 0 && ret != (int)ErrorCodes.ETIMEDOUT)
                {
                    transferContext.mbAsyncCancelled = true;
                    UsbGlobals.Error(this, UsbGlobals.LastError, "reapAsyncNoCancel", ret);
                }
                else if (ret >= 0)
                {
                    transferContext.mbAsyncCancelled = true;
                    transferContext.IncrementTransfer(ret);
                }

                return(ret);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends/receives a control message to/from the current <see cref="UsbDevice"/>.
        /// </summary>
        /// <param name="requestType">USB request type.</param>
        /// <param name="request">USB request.</param>
        /// <param name="value">USB value.</param>
        /// <param name="index">USB index.</param>
        /// <param name="bytes">Buffer to send/recv from device.</param>
        /// <param name="timeout">Maximum amount of time to wait for the function to complete.</param>
        /// <returns>On Success, the number of bytes transmitted. Less than zero on failure.</returns>
        public int IOControlMessage(int requestType, int request, int value, int index, Byte[] bytes, int timeout)
        {
            if (!Open())
            {
                return((int)ErrorCodes.EFAULT);
            }
            int ret = LibUsbAPI.usb_control_msg(mHandle, requestType, request, value, index, bytes, bytes == null ? 0 : bytes.Length, timeout);

            if (ret < 0)
            {
                UsbGlobals.Error(this, UsbGlobals.LastError, "IOControlMessage", ret);
            }
            return(ret);
        }
Exemplo n.º 4
0
        /// <summary>Sets the active configuration of the opened device.</summary>
        /// <remarks>The <paramref name="iConfig" /> parameter is the value as specified in the descriptor field <see cref="InfoConfig.ConfigurationValue" /></remarks>
        /// <returns>0 on success or less than 0 on error.</returns>
        public int SetConfiguration(int iConfig)
        {
            if (!Open())
            {
                return((int)ErrorCodes.EFAULT);
            }

            int ret = LibUsbAPI.usb_set_configuration(mHandle, iConfig);

            if (ret < 0)
            {
                UsbGlobals.Error(this, UsbGlobals.LastError, "SetConfiguration", ret);
            }

            return(ret);
        }
Exemplo n.º 5
0
        ///<summary>Sets the alternate interface to use for the claimed interface.</summary>
        ///<remarks>The <paramref name="iAltInterface"/> parameter is the value as specified in the descriptor field <see cref="InfoInterface.AlternateSetting"/>.</remarks>
        ///<returns>0 on success or less than 0 on error.</returns>
        public int SetAltInterface(int iAltInterface)
        {
            if (!Open())
            {
                return((int)ErrorCodes.EFAULT);
            }

            int ret = LibUsbAPI.usb_set_altinterface(mHandle, iAltInterface);

            if (ret < 0)
            {
                UsbGlobals.Error(this, UsbGlobals.LastError, "SetAltInterface", ret);
            }

            return(ret);
        }
Exemplo n.º 6
0
        ///<summary>Releases a claimed interface.  See <see cref="ClaimInterface"/>.</summary>
        ///<returns>0 on success or less than 0 on error.</returns>
        public int ReleaseInterface(int iInterface)
        {
            if (!Open())
            {
                return((int)ErrorCodes.EFAULT);
            }

            int ret = LibUsbAPI.usb_release_interface(mHandle, iInterface);

            if (ret < 0)
            {
                UsbGlobals.Error(this, UsbGlobals.LastError, "ReleaseInterface", ret);
            }

            return(ret);
        }
Exemplo n.º 7
0
        /// <summary>
        /// All functions that call this function must first lock the <see cref="oLockTransferContext"/> object to be thread safe.
        /// </summary>
        /// <param name="transferContext"></param>
        /// <returns></returns>
        private int cancelAsync_NL(TransferContext transferContext)
        {
            int ret = 0;

            if (transferContext.mbAsyncCancelled)
            {
                return(ret);
            }

            transferContext.mbAsyncCancelled = true;
            ret = LibUsbAPI.usb_cancel_async(transferContext.mContext);

            if (ret < 0)
            {
                UsbGlobals.Error(this, UsbGlobals.LastError, "cancelAsync", ret);
            }
            return(ret);
        }
Exemplo n.º 8
0
        internal int submitAsync(TransferContext transferContext)
        {
            int ret = -1;

            lock (oLockTransferContext)
                ret = LibUsbAPI.usb_submit_async(transferContext.mContext, transferContext.PtrBuf, transferContext.RequestCount);

            if (ret < 0)
            {
                UsbGlobals.Error(this, UsbGlobals.LastError, "submitAsync", ret);
            }
            else
            {
                lock (oLockTransferContext)
                    transferContext.mbAsyncCancelled = false;
            }

            return(ret);
        }
Exemplo n.º 9
0
        internal int reapAsync(TransferContext transferContext)
        {
            int ret = -1;

            ret = LibUsbAPI.usb_reap_async(transferContext.mContext, transferContext.mTimeout);
            lock (oLockTransferContext)
                transferContext.mbAsyncCancelled = true;

            if (ret < 0 && ret != (int)ErrorCodes.ETIMEDOUT)
            {
                UsbGlobals.Error(this, UsbGlobals.LastError, "reapAsync", ret);
            }
            else if (ret >= 0)
            {
                lock (oLockTransferContext)
                    transferContext.IncrementTransfer(ret);
            }
            return(ret);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Retrieves a string descriptor.
        /// </summary>
        /// <param name="stringIndex">The index of the string to be retrieved.</param>
        /// <param name="returnString">On success, the <see cref="System.String"/> for the specified <paramref name="stringIndex"/>.</param>
        /// <returns>On success, the length of the string.  On failure, Less than zero.</returns>
        public int GetString(int stringIndex, ref String returnString)
        {
            returnString = "";
            if (!Open())
            {
                return((int)ErrorCodes.EFAULT);
            }

            StringBuilder sb  = new StringBuilder(255);
            int           ret = LibUsbAPI.usb_get_string_simple(mHandle, stringIndex, sb, sb.Capacity);

            if (ret > 0)
            {
                returnString = sb.ToString(0, ret);
            }

            Debug.WriteIf(ret < 0, "Failed getting string index #" + stringIndex);
            return(ret);
        }
Exemplo n.º 11
0
        internal int freeAsync(TransferContext transferContext)
        {
            lock (oLockTransferContext)
            {
                int ret = 0;
                if (!transferContext.mContext.IsValid)
                {
                    return(ret);
                }

                cancelAsync_NL(transferContext);

                ret = LibUsbAPI.usb_free_async(ref transferContext.mContext);

                if (ret < 0 || transferContext.mContext.IsValid)
                {
                    UsbGlobals.Error(this, UsbGlobals.LastError, "freeAsync", ret);
                }

                return(ret);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Retrieves a string descriptor.
        /// </summary>
        /// <param name="stringIndex">The index of the string to be retrieved.</param>
        /// <param name="langID">The language id of the string.  Default is 0x409.</param>
        /// <param name="returnString">On success, the <see cref="System.String"/> for the specified <paramref name="stringIndex"/> and <paramref name="langID"/>.</param>
        /// <returns></returns>
        public int GetString(int stringIndex, int langID, ref String returnString)
        {
            if (langID == 0)
            {
                langID = 0x409;
            }
            returnString = "";
            if (!Open())
            {
                return((int)ErrorCodes.EFAULT);
            }

            StringBuilder sb  = new StringBuilder(255);
            int           ret = LibUsbAPI.usb_get_string(mHandle, stringIndex, langID, sb, sb.Capacity);

            if (ret > 0)
            {
                returnString = sb.ToString(0, ret);
            }

            Debug.WriteIf(ret < 0, "Failed getting string index #" + stringIndex + " langID 0x" + langID.ToString("X4"));
            return(ret);
        }