From 3e54bb64a4df5fa269c9767952ce654512044b9c Mon Sep 17 00:00:00 2001 From: Dinesh Dwivedi Date: Tue, 11 Jul 2017 21:22:19 +0530 Subject: [PATCH] [USB] Add code to support HotPluggedEvents Change-Id: Ib8cb0da842c9c26b8f4e5e32418d3a894a47d889 Signed-off-by: Dinesh Dwivedi --- Tizen.System.Usb/Interop/Interop.Context.cs | 16 ++++++++ Tizen.System.Usb/Usb/HotPluggedEventArgs.cs | 42 +++++++++++++++++++++ Tizen.System.Usb/Usb/HotplugEventType.cs | 33 ++++++++++++++++ Tizen.System.Usb/Usb/UsbManager.cs | 37 +++++++++++++++++- 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 Tizen.System.Usb/Usb/HotPluggedEventArgs.cs create mode 100644 Tizen.System.Usb/Usb/HotplugEventType.cs diff --git a/Tizen.System.Usb/Interop/Interop.Context.cs b/Tizen.System.Usb/Interop/Interop.Context.cs index 4886d8d..52a2c7e 100644 --- a/Tizen.System.Usb/Interop/Interop.Context.cs +++ b/Tizen.System.Usb/Interop/Interop.Context.cs @@ -21,6 +21,22 @@ using System.Runtime.InteropServices; 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); diff --git a/Tizen.System.Usb/Usb/HotPluggedEventArgs.cs b/Tizen.System.Usb/Usb/HotPluggedEventArgs.cs new file mode 100644 index 0000000..89c7504 --- /dev/null +++ b/Tizen.System.Usb/Usb/HotPluggedEventArgs.cs @@ -0,0 +1,42 @@ +/* + * 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 +{ + /// + /// Event arguments for Hot Plugged event for USB devices. + /// + public class HotPluggedEventArgs : EventArgs + { + internal HotPluggedEventArgs(UsbDevice device, HotplugEventType type) + { + Device = device; + EventType = type; + } + + /// + /// USB Device + /// + public UsbDevice Device { get; } + + /// + /// Event Type + /// + public HotplugEventType EventType { get; } + } +} diff --git a/Tizen.System.Usb/Usb/HotplugEventType.cs b/Tizen.System.Usb/Usb/HotplugEventType.cs new file mode 100644 index 0000000..358b513 --- /dev/null +++ b/Tizen.System.Usb/Usb/HotplugEventType.cs @@ -0,0 +1,33 @@ +/* + * 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 +{ + /// + /// Enumeration of hot plugged event type for USB devices. + /// + public enum HotplugEventType + { + /// + /// USB device is attached + /// + Attach = Interop.HotplugEventType.Attach, + /// + /// USB device is detached + /// + Detach = Interop.HotplugEventType.Detach, + } +} \ No newline at end of file diff --git a/Tizen.System.Usb/Usb/UsbManager.cs b/Tizen.System.Usb/Usb/UsbManager.cs index f01996d..aba4987 100644 --- a/Tizen.System.Usb/Usb/UsbManager.cs +++ b/Tizen.System.Usb/Usb/UsbManager.cs @@ -26,6 +26,7 @@ namespace Tizen.System.Usb public class UsbManager : IDisposable { private readonly Interop.UsbContextHandle _context; + private readonly Interop.HostHotplugHandle _hotpluggedHandle; private List _devices = new List(); /// @@ -35,6 +36,8 @@ namespace Tizen.System.Usb { _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"); } /// @@ -52,6 +55,37 @@ namespace Tizen.System.Usb } } + /// + /// Event handler for events when a USB device is attached or detached. + /// + public event EventHandler 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"); @@ -64,6 +98,7 @@ namespace Tizen.System.Usb { if (!disposedValue) { + _hotpluggedHandle.UnsetHotplugCb(); _context.Dispose(); disposedValue = true; } @@ -81,4 +116,4 @@ namespace Tizen.System.Usb } #endregion } -} \ No newline at end of file +} -- 2.34.1