internal class UsbConfigHandle : SafeUsbHandle
{
[DllImport(Libraries.Usb, EntryPoint = "usb_host_config_destroy")]
- internal static extern ErrorCode ConfigDestroy(UsbConfigHandle /* usb_host_config_h */ config);
+ internal static extern ErrorCode ConfigDestroy(IntPtr /* usb_host_config_h */ config);
public UsbConfigHandle(IntPtr handle) : base(handle) { }
public override void Destroy()
{
- ConfigDestroy(this).ThrowIfFailed("Failed to destroy native HostConfig handle");
+ ConfigDestroy(handle).ThrowIfFailed("Failed to destroy native HostConfig handle");
}
}
}
\ No newline at end of file
[DllImport(Libraries.Usb, EntryPoint = "usb_host_set_hotplug_cb")]
internal static extern ErrorCode SetHotplugCb(this UsbContextHandle /* usb_host_context_h */ ctx, HostHotplugCallback cb, HotplugEventType /* usb_host_hotplug_event_e */ hostEvent, IntPtr /* void */ userData, out HostHotplugHandle /* usb_host_hotplug_h */ handle);
- [DllImport(Libraries.Usb, EntryPoint = "usb_host_unset_hotplug_cb")]
- internal static extern ErrorCode UnsetHotplugCb(this HostHotplugHandle /* usb_host_hotplug_h */ handle);
-
[DllImport(Libraries.Usb, EntryPoint = "usb_host_get_device_list")]
internal static extern ErrorCode GetDeviceList(this UsbContextHandle /* usb_host_context_h */ ctx, out IntPtr /* usb_host_device_h */ devs, out int length);
internal static extern ErrorCode Create(out IntPtr /* usb_host_context_h */ ctx);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_destroy")]
- internal static extern ErrorCode Destroy(UsbContextHandle /* usb_host_context_h */ ctx);
+ internal static extern ErrorCode Destroy(IntPtr /* usb_host_context_h */ ctx);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_free_device_list")]
internal static extern ErrorCode FreeDeviceList(IntPtr deviceList, bool unrefDevices);
FreeDeviceList(nativeDevListPtr, true).ThrowIfFailed("Failed to free native device list");
nativeDevListPtr = IntPtr.Zero;
}
- Destroy(this).ThrowIfFailed("Failed to destroy native context handle");
+ Destroy(handle).ThrowIfFailed("Failed to destroy native context handle");
}
internal List<HostDeviceHandle> GetDeviceList()
}
}
- internal class HostHotplugHandle
+ internal class HostHotplugHandle : SafeUsbHandle
{
- private IntPtr _handle;
- public HostHotplugHandle(IntPtr handle) { _handle = handle; }
+ [DllImport(Libraries.Usb, EntryPoint = "usb_host_unset_hotplug_cb")]
+ internal static extern ErrorCode UnsetHotplugCb(IntPtr /* usb_host_hotplug_h */ handle);
+
+ public HostHotplugHandle(IntPtr handle) : base(handle) { }
+ public override void Destroy()
+ {
+ UnsetHotplugCb(handle).ThrowIfFailed($"Failed to unset hot plug callback");
+ }
}
}
internal static extern ErrorCode Open(this HostDeviceHandle /* usb_host_device_h */ dev);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_device_close")]
- internal static extern ErrorCode Close(this HostDeviceHandle /* usb_host_device_h */ dev);
+ internal static extern ErrorCode CloseHandle(this HostDeviceHandle /* usb_host_device_h */ dev);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_bus_number")]
internal static extern ErrorCode GetBusNumber(this HostDeviceHandle /* usb_host_device_h */ dev, out int busNumber);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_transfer")]
internal static extern ErrorCode Transfer(this UsbEndpointHandle /* usb_host_endpoint_h */ ep, byte[] data, int length, out int transferred, uint timeout);
- internal class UsbEndpointHandle
+ internal class UsbEndpointHandle : SafeUsbHandle
{
- private IntPtr _handle;
- public UsbEndpointHandle(IntPtr handle){ _handle = handle; }
+ public UsbEndpointHandle(IntPtr handle) : base(handle) { }
+ public override void Destroy() { }
}
}
\ No newline at end of file
default: return new InvalidOperationException(errMessage);
}
}
-}
\ No newline at end of file
+}
internal static extern ErrorCode GetStr(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, ref int length, byte[] data);
- internal class UsbInterfaceHandle
+ internal class UsbInterfaceHandle : SafeUsbHandle
{
- private IntPtr _handle;
- public UsbInterfaceHandle(IntPtr handle) { _handle = handle; }
+ public UsbInterfaceHandle(IntPtr handle) : base(handle) { }
+ public override void Destroy() { }
}
}
\ No newline at end of file
{
internal readonly Interop.HostDeviceHandle _handle;
private readonly UsbManager _parent;
- private Dictionary<int, UsbConfiguration> _configurations;
+ private Dictionary<int, UsbConfiguration> _configurations = new Dictionary<int, UsbConfiguration>();
internal UsbDevice(UsbManager parent, Interop.HostDeviceHandle handle)
{
_parent = parent;
_handle = handle;
+
+ int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
+ for (int i = 0; i < count; ++i)
+ {
+ Interop.UsbConfigHandle configHandle;
+ _handle.GetConfig(i, out configHandle);
+ _configurations.Add(i, new UsbConfiguration(this, configHandle));
+ }
}
/// <summary>
get
{
ThrowIfDisposed();
- if (_configurations == null)
- {
- _configurations = new Dictionary<int, UsbConfiguration>();
- int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
- for (int i = 0; i < count; ++i)
- {
- Interop.UsbConfigHandle configHandle;
- _handle.GetConfig(i, out configHandle);
- _configurations.Add(i, new UsbConfiguration(this, configHandle));
- }
- }
return _configurations;
}
}
ThrowIfDisposed();
if (IsOpened == false) throw new InvalidOperationException("Device must be opened for operation first");
- _handle.Close();
+ _handle.CloseHandle().ThrowIfFailed("Failed to close device for use");
}
internal void ThrowIfDisposed()
* limitations under the License.
*/
-using System.Collections.Generic;
using System;
+using System.Collections.Generic;
namespace Tizen.System.Usb
{
/// </summary>
public class UsbManager : IDisposable
{
- private readonly Interop.UsbContextHandle _context;
- private readonly Interop.HostHotplugHandle _hotpluggedHandle;
+ private readonly Interop.UsbContextHandle _context = null;
+ private readonly Interop.HostHotplugHandle _hotpluggedHandle = null;
private List<UsbDevice> _devices = new List<UsbDevice>();
/// <summary>
{
if (!disposedValue)
{
- _hotpluggedHandle.UnsetHotplugCb();
- _context.Dispose();
+ if (_hotpluggedHandle != null) _hotpluggedHandle.Dispose();
+ if (_context != null) _context.Dispose();
disposedValue = true;
}
}