[USB] Add code to support HotPluggedEvents 57/133857/4 devel
authorDinesh Dwivedi <dinesh.d@samsung.com>
Tue, 11 Jul 2017 15:52:19 +0000 (21:22 +0530)
committerDinesh Dwivedi <dinesh.d@samsung.com>
Tue, 11 Jul 2017 15:52:19 +0000 (21:22 +0530)
Change-Id: Ib8cb0da842c9c26b8f4e5e32418d3a894a47d889
Signed-off-by: Dinesh Dwivedi <dinesh.d@samsung.com>
Tizen.System.Usb/Interop/Interop.Context.cs
Tizen.System.Usb/Usb/HotPluggedEventArgs.cs [new file with mode: 0644]
Tizen.System.Usb/Usb/HotplugEventType.cs [new file with mode: 0644]
Tizen.System.Usb/Usb/UsbManager.cs

index 4886d8d..52a2c7e 100644 (file)
@@ -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 (file)
index 0000000..89c7504
--- /dev/null
@@ -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
+{
+    /// <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; }
+    }
+}
diff --git a/Tizen.System.Usb/Usb/HotplugEventType.cs b/Tizen.System.Usb/Usb/HotplugEventType.cs
new file mode 100644 (file)
index 0000000..358b513
--- /dev/null
@@ -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
+{
+    /// <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
index f01996d..aba4987 100644 (file)
@@ -26,6 +26,7 @@ namespace Tizen.System.Usb
     public class UsbManager : IDisposable
     {
         private readonly Interop.UsbContextHandle _context;
+        private readonly Interop.HostHotplugHandle _hotpluggedHandle;
         private List<UsbDevice> _devices = new List<UsbDevice>();
 
         /// <summary>
@@ -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");
         }
 
         /// <summary>
@@ -52,6 +55,37 @@ namespace Tizen.System.Usb
             }
         }
 
+        /// <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");
@@ -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
+}