// Copyright © 2006-2010 Travis Robinson. All rights reserved.
//
// website: http://sourceforge.net/projects/libusbdotnet
// e-mail: libusbdotnet@gmail.com
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. or
// visit www.gnu.org.
//
//
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using LibUsbDotNet.Main;
using MonoLibUsb.Profile;
namespace MonoLibUsb
{
///
/// Represents a Libusb-1.0 device handle.
///
///
/// To close a device, see the method.
/// A is roughly equivalent to a libusb_device_handle.
///
///
/// MonoUsbDeviceHandle deviceHandle = new MonoUsbDeviceHandle(profileHandle);
/// if (deviceHandle.IsInvalid) throw new Exception("Invalid device context.");
///
public class MonoUsbDeviceHandle : SafeContextHandle
{
private static Object handleLOCK = new object();
private static MonoUsbError mLastReturnCode;
private static String mLastReturnString = String.Empty;
///
/// If the device handle is , gets a descriptive string for the .
///
public static string LastErrorString
{
get
{
lock (handleLOCK)
{
return mLastReturnString;
}
}
}
///
/// If the device handle is , gets the status code indicating the reason.
///
public static MonoUsbError LastErrorCode
{
get
{
lock (handleLOCK)
{
return mLastReturnCode;
}
}
}
/// Open a device handle from .
///
/// A handle allows you to perform I/O on the device in question.
/// To close a device handle call its method.
/// This is a non-blocking function; no requests are sent over the bus.
/// The constructor is roughly equivalent to libusb_open().
///
/// A device profile handle.
public MonoUsbDeviceHandle(MonoUsbProfileHandle profileHandle)
: base(IntPtr.Zero)
{
IntPtr pDeviceHandle = IntPtr.Zero;
int ret = MonoUsbApi.Open(profileHandle, ref pDeviceHandle);
if (ret < 0 || pDeviceHandle==IntPtr.Zero)
{
lock (handleLOCK)
{
mLastReturnCode = (MonoUsbError) ret;
mLastReturnString = MonoUsbApi.StrError(mLastReturnCode);
}
SetHandleAsInvalid();
}
else
{
SetHandle(pDeviceHandle);
}
}
internal MonoUsbDeviceHandle(IntPtr pDeviceHandle)
: base(pDeviceHandle)
{
}
///
///Closes the .
///
///
///true if the is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a ReleaseHandleFailed Managed Debugging Assistant.
///
protected override bool ReleaseHandle()
{
if (!IsInvalid)
{
Debug.WriteLine(GetType().Name + ".ReleaseHandle() Before", "Libusb-1.0");
MonoUsbApi.Close(handle);
Debug.WriteLine(GetType().Name + ".ReleaseHandle() After", "Libusb-1.0");
SetHandleAsInvalid();
}
return true;
}
///
/// Closes the reference. When all references are no longer is use, the device
/// is closed in the finalizer.
///
///
/// The method is roughly equivalent to libusb_close().
///
public new void Close()
{
base.Close();
}
}
}