internal static extern ErrorCode GetConfigStr(this UsbConfigHandle /* usb_host_config_h */ config, ref int length, byte[] data);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_config_get_interface")]
- internal static extern ErrorCode GetInterface(this UsbConfigHandle /* usb_host_config_h */ config, int interfaceIndex, out UsbInterfaceHandle /* usb_host_interface_h */ usbInterface);
+ internal static extern ErrorCode GetInterface(this UsbConfigHandle /* usb_host_config_h */ config, int interfaceIndex, out IntPtr /* usb_host_interface_h */ usbInterface);
internal class UsbConfigHandle : SafeUsbHandle
{
internal delegate void HostHotplugCallback(IntPtr /* usb_host_device_h */ dev, IntPtr /* void */ userData);
[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);
+ 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 IntPtr /* 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);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_free_device_list")]
internal static extern ErrorCode FreeDeviceList(IntPtr deviceList, bool unrefDevices);
- internal UsbContextHandle()
+ internal UsbContextHandle(IntPtr handle) : base(handle) { }
+
+ internal static UsbContextHandle GetContextHandle()
{
- Create(out handle).ThrowIfFailed("Failed to create native context handle");
+ IntPtr nativeHandle;
+ Create(out nativeHandle).ThrowIfFailed("Failed to create native context handle");
+ return new UsbContextHandle(nativeHandle);
}
public override void Destroy()
internal static extern ErrorCode GetAddress(this HostDeviceHandle /* usb_host_device_h */ dev, out int deviceAddress);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_port_numbers")]
- internal static extern ErrorCode GetPortNumbers(this HostDeviceHandle /* usb_host_device_h */ dev, [MarshalAs(UnmanagedType.SysInt, SizeParamIndex = 2)] [In, Out] int[] portNumbers, int portNumbersLen, out int portsCount);
+ internal static extern ErrorCode GetPortNumbers(this HostDeviceHandle /* usb_host_device_h */ dev, [In, Out] int[] portNumbers, int portNumbersLen, out int portsCount);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_config")]
- internal static extern ErrorCode GetConfig(this HostDeviceHandle /* usb_host_device_h */ dev, int configIndex, out UsbConfigHandle /* usb_host_config_h */ config);
+ internal static extern ErrorCode GetConfig(this HostDeviceHandle /* usb_host_device_h */ dev, int configIndex, out IntPtr /* usb_host_config_h */ config);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_get_active_config")]
- internal static extern ErrorCode GetActiveConfig(this HostDeviceHandle /* usb_host_device_h */ dev, out UsbConfigHandle /* usb_host_config_h */ config);
+ internal static extern ErrorCode GetActiveConfig(this HostDeviceHandle /* usb_host_device_h */ dev, out IntPtr /* usb_host_config_h */ config);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_bcd_usb")]
internal static extern ErrorCode GetBcdUsb(this HostDeviceHandle /* usb_host_device_h */ dev, out int bcdUsb);
internal static extern ErrorCode GetNumEndpoints(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, out int numEndpoints);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_interface_get_endpoint")]
- internal static extern ErrorCode GetEndpoint(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, int epIndex, out UsbEndpointHandle /* usb_host_endpoint_h */ ep);
+ internal static extern ErrorCode GetEndpoint(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, int epIndex, out IntPtr /* usb_host_endpoint_h */ ep);
[DllImport(Libraries.Usb, EntryPoint = "usb_host_interface_set_altsetting")]
internal static extern ErrorCode SetAltsetting(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, int altsetting);
internal abstract class SafeUsbHandle : SafeHandle
{
public abstract void Destroy();
- public SafeUsbHandle() : base(IntPtr.Zero, true) { }
+
+ private SafeUsbHandle() : base(IntPtr.Zero, true) { }
public SafeUsbHandle(IntPtr handle) : base(handle, true) { }
public override bool IsInvalid { get { return handle == IntPtr.Zero; } }
protected override bool ReleaseHandle()
int count = Interop.NativeGet<int>(_handle.GetNumInterfaces);
for (int i = 0; i < count; ++i)
{
- Interop.UsbInterfaceHandle handle;
+ IntPtr handle;
_handle.GetInterface(i, out handle);
- UsbInterface usbInterface = new UsbInterface(this, handle);
+ UsbInterface usbInterface = new UsbInterface(this, new Interop.UsbInterfaceHandle(handle));
_interfaces.Add(usbInterface.Id, usbInterface);
}
}
int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
for (int i = 0; i < count; ++i)
{
- Interop.UsbConfigHandle configHandle;
+ IntPtr configHandle;
_handle.GetConfig(i, out configHandle);
- _configurations.Add(i, new UsbConfiguration(this, configHandle));
+ _configurations.Add(i, new UsbConfiguration(this, new Interop.UsbConfigHandle(configHandle)));
}
}
get
{
ThrowIfDisposed();
- Interop.UsbConfigHandle configHandle = Interop.NativeGet<Interop.UsbConfigHandle>(_handle.GetActiveConfig);
+ IntPtr handle = Interop.NativeGet<IntPtr>(_handle.GetActiveConfig);
+ Interop.UsbConfigHandle configHandle = new Interop.UsbConfigHandle(handle);
return _configurations.Values.Where(config => config._handle == configHandle).First();
}
}
public class UsbEndpoint
{
internal readonly Interop.UsbEndpointHandle _handle;
- protected readonly UsbInterface _parent;
+ internal readonly UsbInterface _parent;
internal UsbEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle)
{
_parent?.ThrowIfDisposed();
}
- protected int TransferImpl(byte[] buffer, int length, uint timeout)
+ internal int TransferImpl(byte[] buffer, int length, uint timeout)
{
ThrowIfDisposed();
int transferred;
int count = Interop.NativeGet<int>(_handle.GetNumEndpoints);
for (int i = 0; i < count; ++i)
{
- Interop.UsbEndpointHandle handle;
+ IntPtr handle;
_handle.GetEndpoint(i, out handle);
- UsbEndpoint endpoint = UsbEndpoint.EndpointFactory(this, handle);
+ UsbEndpoint endpoint = UsbEndpoint.EndpointFactory(this, new Interop.UsbEndpointHandle(handle));
_endpoints.Add(endpoint.Id, endpoint);
}
}
/// </summary>
public UsbManager()
{
- _context = new Interop.UsbContextHandle();
+ _context = Interop.UsbContextHandle.GetContextHandle();
_devices = _context.GetDeviceList().Select(devHandle => new UsbDevice(this, devHandle)).ToList();
+
+ IntPtr hotpluggedHandle;
_context.SetHotplugCb(HostHotplugCallback, Interop.HotplugEventType.Any,
- IntPtr.Zero, out _hotpluggedHandle).ThrowIfFailed("Failed to set hot plugged callback");
+ IntPtr.Zero, out hotpluggedHandle).ThrowIfFailed("Failed to set hot plugged callback");
+
+ _hotpluggedHandle = new Interop.HostHotplugHandle(hotpluggedHandle);
}
/// <summary>
#region IDisposable Support
private bool disposedValue = false;
- protected virtual void Dispose(bool disposing)
+ internal virtual void Dispose(bool disposing)
{
if (!disposedValue)
{