[USB] Add UsbManager class 56/133856/4
authorDinesh Dwivedi <dinesh.d@samsung.com>
Tue, 11 Jul 2017 15:50:47 +0000 (21:20 +0530)
committerDinesh Dwivedi <dinesh.d@samsung.com>
Tue, 11 Jul 2017 15:50:47 +0000 (21:20 +0530)
Change-Id: I6ca4a8deacca695bcd91f5194a2d55e64b1f081e
Signed-off-by: Dinesh Dwivedi <dinesh.d@samsung.com>
Tizen.System.Usb/Interop/Interop.Context.cs [new file with mode: 0644]
Tizen.System.Usb/Usb/UsbDevice.cs
Tizen.System.Usb/Usb/UsbManager.cs [new file with mode: 0644]

diff --git a/Tizen.System.Usb/Interop/Interop.Context.cs b/Tizen.System.Usb/Interop/Interop.Context.cs
new file mode 100644 (file)
index 0000000..4886d8d
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * 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;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+
+internal static partial class Interop
+{
+    [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_device_open_with_vid_pid")]
+    internal static extern ErrorCode OpenWithVidPid(this UsbContextHandle /* usb_host_context_h */ ctx, int vendorId, int productId, out HostDeviceHandle /* usb_host_device_h */ deviceHandle);
+
+    internal class UsbContextHandle : SafeUsbHandle
+    {
+        private IntPtr nativeDevListPtr = IntPtr.Zero;
+
+        [DllImport(Libraries.Usb, EntryPoint = "usb_host_create")]
+        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);
+
+        [DllImport(Libraries.Usb, EntryPoint = "usb_host_free_device_list")]
+        internal static extern ErrorCode FreeDeviceList(IntPtr deviceList, bool unrefDevices);
+
+        internal UsbContextHandle()
+        {
+            Create(out handle).ThrowIfFailed("Failed to create native context handle");
+        }
+
+        public override void Destroy()
+        {
+            if (nativeDevListPtr != IntPtr.Zero)
+            {
+                FreeDeviceList(nativeDevListPtr, true).ThrowIfFailed("Failed to free native device list");
+                nativeDevListPtr = IntPtr.Zero;
+            }
+            Destroy(this).ThrowIfFailed("Failed to destroy native context handle");
+        }
+
+        internal List<HostDeviceHandle> GetDeviceList()
+        {
+            if (nativeDevListPtr != IntPtr.Zero)
+            {
+                FreeDeviceList(nativeDevListPtr, true).ThrowIfFailed("Failed to free native device list");
+                nativeDevListPtr = IntPtr.Zero;
+            }
+
+            int length;
+            ErrorCode err = this.GetDeviceList(out nativeDevListPtr, out length);
+            err.ThrowIfFailed("Failed to get device list for the context");
+
+            List<HostDeviceHandle> deviceList = new List<HostDeviceHandle>();
+            IntPtr[] nativeDevList = new IntPtr[length];
+            Marshal.Copy(nativeDevListPtr, nativeDevList, 0, length);
+
+            foreach (var devHandle in nativeDevList)
+            {
+                deviceList.Add(new HostDeviceHandle(devHandle));
+            }
+
+            return deviceList;
+        }
+    }
+
+    internal class HostHotplugHandle
+    {
+        private IntPtr _handle;
+        public HostHotplugHandle(IntPtr handle) { _handle = handle; }
+    }
+}
index c2a5903..0f8fd4e 100644 (file)
@@ -26,10 +26,12 @@ namespace Tizen.System.Usb
     public class UsbDevice : IDisposable
     {
         internal readonly Interop.HostDeviceHandle _handle;
+        private readonly UsbManager _parent;
         private Dictionary<int, UsbConfiguration> _configurations;
 
-        internal UsbDevice(Interop.HostDeviceHandle handle)
+        internal UsbDevice(UsbManager parent, Interop.HostDeviceHandle handle)
         {
+            _parent = parent;
             _handle = handle;
         }
 
@@ -181,6 +183,7 @@ namespace Tizen.System.Usb
         internal void ThrowIfDisposed()
         {
             if (disposedValue) throw new ObjectDisposedException("USB Device is already disposed");
+            _parent.ThrowIfDisposed();
         }
 
         #region IDisposable Support
diff --git a/Tizen.System.Usb/Usb/UsbManager.cs b/Tizen.System.Usb/Usb/UsbManager.cs
new file mode 100644 (file)
index 0000000..f01996d
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * 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;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Tizen.System.Usb
+{
+    /// <summary>
+    /// USB Manager class.
+    /// </summary>
+    public class UsbManager : IDisposable
+    {
+        private readonly Interop.UsbContextHandle _context;
+        private List<UsbDevice> _devices = new List<UsbDevice>();
+
+        /// <summary>
+        /// USB Manager Constructor.
+        /// </summary>
+        public UsbManager()
+        {
+            _context = new Interop.UsbContextHandle();
+            _devices = _context.GetDeviceList().Select(devHandle => new UsbDevice(this, devHandle)).ToList();
+        }
+
+        /// <summary>
+        /// This function returns list of USB devices attached to system.
+        /// </summary>
+        /// <exception cref="NotSupportedException">Throws exception if USB host feature is not enabled.</exception>
+        /// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
+        /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
+        public IEnumerable<UsbDevice> AvailableDevices
+        {
+            get
+            {
+                ThrowIfDisposed();
+                return _devices;
+            }
+        }
+
+        internal void ThrowIfDisposed()
+        {
+            if (disposedValue) throw new ObjectDisposedException("USB Context is already disposed");
+        }
+
+        #region IDisposable Support
+        private bool disposedValue = false;
+
+        protected virtual void Dispose(bool disposing)
+        {
+            if (!disposedValue)
+            {
+                _context.Dispose();
+                disposedValue = true;
+            }
+        }
+
+        ~UsbManager()
+        {
+            Dispose(false);
+        }
+
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+        #endregion
+    }
+}
\ No newline at end of file