Ejemplo n.º 1
0
        ///<summary>
        /// Opens the USB device handle.
        ///</summary>
        ///<returns>
        ///True if the device is already opened or was opened successfully.
        ///False if the device does not exists or is no longer valid.
        ///</returns>
        public override bool Open()
        {
            if (IsOpen)
            {
                return(true);
            }

            SafeFileHandle sfhDev;

            bool bSuccess = WinUsbAPI.OpenDevice(out sfhDev, mDevicePath);

            if (bSuccess)
            {
                SafeWinUsbInterfaceHandle handle = new SafeWinUsbInterfaceHandle();
                if ((bSuccess = WinUsbAPI.WinUsb_Initialize(sfhDev, ref handle)))
                {
                    mSafeDevHandle = sfhDev;
                    mUsbHandle     = handle;
                    mPowerPolicies = new PowerPolicies(this);
                }
                else
                {
                    UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "Open:Initialize", typeof(UsbDevice));
                }
            }
            else
            {
                UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "Open", typeof(UsbDevice));
            }


            return(bSuccess);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Opens a WinUsb directly from the user supplied device path.
        /// </summary>
        /// <param name="devicePath">Device path (symbolic link) of the WinUsb device to open.</param>
        /// <param name="usbDevice">Returns an opened WinUsb device on success, null on failure.</param>
        /// <returns>True on success.</returns>
        public static bool Open(string devicePath, out WinUsbDevice usbDevice)
        {
            usbDevice = null;

            SafeFileHandle sfhDev;

            bool bSuccess = WinUsbAPI.OpenDevice(out sfhDev, devicePath);

            if (bSuccess)
            {
                SafeWinUsbInterfaceHandle handle = new SafeWinUsbInterfaceHandle();
                bSuccess = WinUsbAPI.WinUsb_Initialize(sfhDev, ref handle);
                if (bSuccess)
                {
                    usbDevice = new WinUsbDevice(WinUsbApi, sfhDev, handle, devicePath);
                }
                else
                {
                    UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "Open:Initialize", typeof(UsbDevice));
                }
            }
            else
            {
                UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "Open", typeof(UsbDevice));
            }


            return(bSuccess);
        }
Ejemplo n.º 3
0
        public WinUSBInterfaceStream(WeakReference <WinUSBInterface> parentInterface)
        {
            this.parentInterface = parentInterface;

            if (!parentInterface.TryGetTarget(out var usbInterface))
            {
                throw new InvalidOperationException("Weak reference to parent interface is unexpectedly invalid");
            }

            this.interfaceNum = usbInterface.InterfaceNum;

            winUsbHandle = usbInterface.BorrowHandle();

            if (usbInterface.InputPipe is byte readPipe)
            {
                this.readPipe = readPipe;
                readBuffer    = GC.AllocateArray <byte>(usbInterface.InputReportLength, true);
                readPtr       = (byte *)Unsafe.AsPointer(ref readBuffer[0]);
            }

            if (usbInterface.OutputPipe is byte writePipe)
            {
                this.writePipe = writePipe;
                writeBuffer    = GC.AllocateArray <byte>(usbInterface.OutputReportLength, true);
                writePtr       = (byte *)Unsafe.AsPointer(ref writeBuffer[0]);
            }
        }
Ejemplo n.º 4
0
        // Take reference, so we may easily add multiple interface support in the future
        internal void ReturnHandle(SafeWinUsbInterfaceHandle handle)
        {
            if (activeWinUsbHandle != handle)
            {
                throw new InvalidOperationException("Returning handle is not equal to active handle");
            }

            if (Interlocked.Decrement(ref referenceCount) == 0)
            {
                activeWinUsbHandle.Dispose();
                activeFileHandle.Dispose();

                activeWinUsbHandle = null;
                activeFileHandle   = null;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets an interface associated with this <see cref="WinUsbDevice"/>.
        /// </summary>
        /// <param name="associatedInterfaceIndex">The index to retrieve. (0 = next interface, 1= interface after next, etc.).</param>
        /// <param name="usbDevice">A new <see cref="WinUsbDevice"/> class for the specified AssociatedInterfaceIndex.</param>
        /// <returns>True on success.</returns>
        public bool GetAssociatedInterface(byte associatedInterfaceIndex, out WinUsbDevice usbDevice)
        {
            usbDevice = null;
            IntPtr pHandle  = IntPtr.Zero;
            bool   bSuccess = WinUsbAPI.WinUsb_GetAssociatedInterface(mUsbHandle, associatedInterfaceIndex, ref pHandle);

            if (bSuccess)
            {
                SafeWinUsbInterfaceHandle tempHandle = new SafeWinUsbInterfaceHandle(pHandle);

                usbDevice = new WinUsbDevice(mUsbApi, null, tempHandle, mDevicePath);
            }
            if (!bSuccess)
            {
                UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "GetAssociatedInterface", this);
            }

            return(bSuccess);
        }
Ejemplo n.º 6
0
 internal static extern bool WinUsb_Initialize([In] SafeHandle DeviceHandle, [Out, In] ref SafeWinUsbInterfaceHandle InterfaceHandle);
Ejemplo n.º 7
0
 public static extern bool WinUsb_QueryPipe(SafeWinUsbInterfaceHandle interfaceHandle, byte altInterfaceNum, byte pipeIndex, out PipeInfo pipeInfo);
Ejemplo n.º 8
0
 public static unsafe extern bool WinUsb_QueryInterfaceSettings(SafeWinUsbInterfaceHandle interfaceHandle, byte altInterfaceNum, InterfaceDescriptor *interfaceDescriptor);
Ejemplo n.º 9
0
 public static unsafe extern bool WinUsb_ControlTransfer(SafeWinUsbInterfaceHandle interfaceHandle, SetupPacket setupPacket, void *pBuffer, uint bufferLength, out uint lengthTransferred, NativeOverlapped *pOverlapped);
Ejemplo n.º 10
0
 public static unsafe extern bool WinUsb_WritePipe(SafeWinUsbInterfaceHandle interfaceHandle, byte pipeId, void *pBuffer, uint bufferLength, out uint lengthTransferred, NativeOverlapped *pOverlapped);
Ejemplo n.º 11
0
 public static extern bool WinUsb_Initialize(SafeFileHandle handle, out SafeWinUsbInterfaceHandle interfaceHandle);