Exemple #1
0
        public async Task <bool> OpenAsync()
        {
            deviceHandle =
                Kernel32.CreateFile(DevicePath,
                                    NativeFileAccess.FILE_GENERIC_WRITE | NativeFileAccess.FILE_GENERIC_READ,
                                    NativeFileShare.FILE_SHARE_WRITE | NativeFileShare.FILE_SHARE_READ,
                                    IntPtr.Zero,
                                    NativeFileMode.OPEN_EXISTING,
                                    NativeFileFlag.FILE_ATTRIBUTE_NORMAL | NativeFileFlag.FILE_FLAG_OVERLAPPED,
                                    IntPtr.Zero);

            if (deviceHandle.IsInvalid || deviceHandle.IsClosed)
            {
                return(false);
            }
            if (UseOverlappedTransfers)
            {
                ThreadPool.BindHandle(deviceHandle); // needed for overlapped I/O
            }
            winUsbHandle = new SafeWinUsbHandle();
            if (WinUsb.NativeMethods.WinUsb_Initialize(deviceHandle, ref winUsbHandle) == false)
            {
                return(false);
            }

            int trueVal      = 1;
            int timeout      = 500;
            int timeoutShort = 50;

            WinUsb.NativeMethods.WinUsb_SetPipePolicy(winUsbHandle, peripheralResponseEndpoint,
                                                      PipePolicy.AutoClearStall, 4, ref trueVal);
            WinUsb.NativeMethods.WinUsb_SetPipePolicy(winUsbHandle, peripheralResponseEndpoint,
                                                      PipePolicy.PipeTransferTimeout, 4, ref timeoutShort);

            WinUsb.NativeMethods.WinUsb_SetPipePolicy(winUsbHandle, pinReportEndpoint,
                                                      PipePolicy.AutoClearStall, 4, ref trueVal);
            WinUsb.NativeMethods.WinUsb_SetPipePolicy(winUsbHandle, pinReportEndpoint,
                                                      PipePolicy.PipeTransferTimeout, 4, ref timeout);

            WinUsb.NativeMethods.WinUsb_SetPipePolicy(winUsbHandle, peripheralConfigEndpoint,
                                                      PipePolicy.AutoClearStall, 4, ref trueVal);
            WinUsb.NativeMethods.WinUsb_SetPipePolicy(winUsbHandle, peripheralConfigEndpoint,
                                                      PipePolicy.PipeTransferTimeout, 4, ref timeout);

            WinUsb.NativeMethods.WinUsb_SetPipePolicy(winUsbHandle, pinConfigEndpoint,
                                                      PipePolicy.AutoClearStall, 4, ref trueVal);
            WinUsb.NativeMethods.WinUsb_SetPipePolicy(winUsbHandle, pinConfigEndpoint,
                                                      PipePolicy.PipeTransferTimeout, 4, ref timeout);

            IsOpen = true;
            BeginRead(pinReportEndpoint, pinReportBuffer, pinReportBuffer.Length, pinStateCallback,
                      null); // kick off our first pin read
            return(true);
        }
Exemple #2
0
 internal static extern bool WinUsb_Initialize([In] SafeHandle DeviceHandle,
                                               [Out][In] ref SafeWinUsbHandle InterfaceHandle);