/// <summary> /// Creates and initialize a <a href="http://libusb.sourceforge.net/api-1.0/index.html">Libusb-1.0</a> USB session handle. /// </summary> /// <remarks> /// <para>A <see cref="LibUsbSessionHandle"/> instance must be created before calling any other <a href="http://libusb.sourceforge.net/api-1.0/index.html">Libusb-1.0 API</a> function.</para> /// </remarks> public LibUsbSessionHandle() : base(IntPtr.Zero, true) { lock (sessionLOCK) { IntPtr pNewSession = IntPtr.Zero; try { mLastReturnCode = (LibUsbError)LibUsbApi.Init(ref pNewSession); } catch (DllNotFoundException dllNotFound) { if (Helper.IsLinux) { throw new DllNotFoundException(DLL_NOT_FOUND_LINUX, dllNotFound); } else { throw new DllNotFoundException(DLL_NOT_FOUND_WINDOWS, dllNotFound); } } if ((int)mLastReturnCode < 0) { mLastReturnString = LibUsbApi.StrError(mLastReturnCode); SetHandleAsInvalid(); } else { SetHandle(pNewSession); mSessionCount++; } } }
private static IntPtr Init() { IntPtr ctx = IntPtr.Zero; LibUsbError error = NativeMethods.init(ref ctx); if (error != LibUsbError.Success) { throw new LibUsbException("Can't initialize libusb", error); } return(ctx); }
/// <summary>Open a device handle from <paramref name="profileHandle"/>.</summary> /// <remarks> /// <para>A handle allows you to perform I/O on the device in question.</para> /// <para>To close a device handle call its <see cref="SafeHandle.Close"/> method.</para> /// <para>This is a non-blocking function; no requests are sent over the bus.</para> /// <note title="Libusb-1.0 API Note:" type="cpp">The <see cref="LibUsbDeviceHandle(LibUsbProfileHandle)"/> constructor is roughly equivalent to <a href="http://libusb.sourceforge.net/api-1.0/group__dev.html#ga8163100afdf933fabed0db7fa81c89d1">libusb_open()</a>.</note> /// </remarks> /// <param name="profileHandle">A device profile handle.</param> public LibUsbDeviceHandle(LibUsbProfileHandle profileHandle) : base(IntPtr.Zero) { IntPtr pDeviceHandle = IntPtr.Zero; int ret = LibUsbApi.Open(profileHandle, ref pDeviceHandle); if (ret < 0 || pDeviceHandle==IntPtr.Zero) { lock (handleLOCK) { mLastReturnCode = (LibUsbError) ret; mLastReturnString = LibUsbApi.StrError(mLastReturnCode); } SetHandleAsInvalid(); } else { SetHandle(pDeviceHandle); } }
public LibUsbException(string message, LibUsbError error) : base(message) { this.Error = error; }
/// <summary> /// Get a string describing a <see cref="LibUsbError"/>. /// </summary> /// <param name="errcode">The <see cref="LibUsbError"/> code to retrieve a description for.</param> /// <returns>A string describing the <see cref="LibUsbError"/> code.</returns> public static string StrError(LibUsbError errcode) { switch (errcode) { case LibUsbError.Success: return "Success"; case LibUsbError.ErrorIO: return "Input/output error"; case LibUsbError.ErrorInvalidParam: return "Invalid parameter"; case LibUsbError.ErrorAccess: return "Access denied (insufficient permissions)"; case LibUsbError.ErrorNoDevice: return "No such device (it may have been disconnected)"; case LibUsbError.ErrorBusy: return "Resource busy"; case LibUsbError.ErrorTimeout: return "Operation timed out"; case LibUsbError.ErrorOverflow: return "Overflow"; case LibUsbError.ErrorPipe: return "Pipe error or endpoint halted"; case LibUsbError.ErrorInterrupted: return "System call interrupted (perhaps due to signal)"; case LibUsbError.ErrorNoMem: return "Insufficient memory"; case LibUsbError.ErrorIOCancelled: return "Transfer was canceled"; case LibUsbError.ErrorNotSupported: return "Operation not supported or unimplemented on this platform"; default: return "Unknown error:" + errcode; } }