internal static partial class Interop
{
+ internal enum HotplugEventType
+ {
+ Attach, // USB_HOST_HOTPLUG_EVENT_ATTACH
+ Detach, // USB_HOST_HOTPLUG_EVENT_DETACH
+ Any, // USB_HOST_HOTPLUG_EVENT_ANY
+ }
+
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ 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);
+
+ [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);
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+
+namespace Tizen.System.Usb
+{
+ /// <summary>
+ /// Event arguments for Hot Plugged event for USB devices.
+ /// </summary>
+ public class HotPluggedEventArgs : EventArgs
+ {
+ internal HotPluggedEventArgs(UsbDevice device, HotplugEventType type)
+ {
+ Device = device;
+ EventType = type;
+ }
+
+ /// <summary>
+ /// USB Device
+ /// </summary>
+ public UsbDevice Device { get; }
+
+ /// <summary>
+ /// Event Type
+ /// </summary>
+ public HotplugEventType EventType { get; }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace Tizen.System.Usb
+{
+ /// <summary>
+ /// Enumeration of hot plugged event type for USB devices.
+ /// </summary>
+ public enum HotplugEventType
+ {
+ /// <summary>
+ /// USB device is attached
+ /// </summary>
+ Attach = Interop.HotplugEventType.Attach,
+ /// <summary>
+ /// USB device is detached
+ /// </summary>
+ Detach = Interop.HotplugEventType.Detach,
+ }
+}
\ No newline at end of file
public class UsbManager : IDisposable
{
private readonly Interop.UsbContextHandle _context;
+ private readonly Interop.HostHotplugHandle _hotpluggedHandle;
private List<UsbDevice> _devices = new List<UsbDevice>();
/// <summary>
{
_context = new Interop.UsbContextHandle();
_devices = _context.GetDeviceList().Select(devHandle => new UsbDevice(this, devHandle)).ToList();
+ _context.SetHotplugCb(HostHotplugCallback, Interop.HotplugEventType.Any,
+ IntPtr.Zero, out _hotpluggedHandle).ThrowIfFailed("Failed to set hot plugged callback");
}
/// <summary>
}
}
+ /// <summary>
+ /// Event handler for events when a USB device is attached or detached.
+ /// </summary>
+ public event EventHandler<HotPluggedEventArgs> DeviceHotPlugged;
+
+ internal void HostHotplugCallback(IntPtr devHandle, IntPtr userData)
+ {
+ Interop.HostDeviceHandle handle = new Interop.HostDeviceHandle(devHandle);
+ UsbDevice device = _devices.Where(dev => dev._handle == handle).FirstOrDefault();
+ if (device == null)
+ {
+ device = new UsbDevice(this, handle);
+ _devices.Add(device);
+
+ if (DeviceHotPlugged != null)
+ {
+ DeviceHotPlugged.Invoke(this, new HotPluggedEventArgs(device, HotplugEventType.Attach));
+ }
+ }
+ else
+ {
+ if (DeviceHotPlugged != null)
+ {
+ DeviceHotPlugged.Invoke(this, new HotPluggedEventArgs(device, HotplugEventType.Detach));
+ }
+
+ _devices.Remove(device);
+ // do we need to dispose device here ?
+ }
+ }
+
internal void ThrowIfDisposed()
{
if (disposedValue) throw new ObjectDisposedException("USB Context is already disposed");
{
if (!disposedValue)
{
+ _hotpluggedHandle.UnsetHotplugCb();
_context.Dispose();
disposedValue = true;
}
}
#endregion
}
-}
\ No newline at end of file
+}