From: Dinesh Dwivedi Date: Tue, 11 Jul 2017 15:50:47 +0000 (+0530) Subject: [USB] Add UsbManager class X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d36c226e653d1ded793d0d75a32a8a42e835b837;p=platform%2Fcore%2Fcsapi%2Fusb.git [USB] Add UsbManager class Change-Id: I6ca4a8deacca695bcd91f5194a2d55e64b1f081e Signed-off-by: Dinesh Dwivedi --- diff --git a/Tizen.System.Usb/Interop/Interop.Context.cs b/Tizen.System.Usb/Interop/Interop.Context.cs new file mode 100644 index 0000000..4886d8d --- /dev/null +++ b/Tizen.System.Usb/Interop/Interop.Context.cs @@ -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 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 deviceList = new List(); + 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; } + } +} diff --git a/Tizen.System.Usb/Usb/UsbDevice.cs b/Tizen.System.Usb/Usb/UsbDevice.cs index c2a5903..0f8fd4e 100644 --- a/Tizen.System.Usb/Usb/UsbDevice.cs +++ b/Tizen.System.Usb/Usb/UsbDevice.cs @@ -26,10 +26,12 @@ namespace Tizen.System.Usb public class UsbDevice : IDisposable { internal readonly Interop.HostDeviceHandle _handle; + private readonly UsbManager _parent; private Dictionary _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 index 0000000..f01996d --- /dev/null +++ b/Tizen.System.Usb/Usb/UsbManager.cs @@ -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 +{ + /// + /// USB Manager class. + /// + public class UsbManager : IDisposable + { + private readonly Interop.UsbContextHandle _context; + private List _devices = new List(); + + /// + /// USB Manager Constructor. + /// + public UsbManager() + { + _context = new Interop.UsbContextHandle(); + _devices = _context.GetDeviceList().Select(devHandle => new UsbDevice(this, devHandle)).ToList(); + } + + /// + /// This function returns list of USB devices attached to system. + /// + /// Throws exception if USB host feature is not enabled. + /// Throws exception in case of insufficient memory. + /// Throws exception if user has insufficient permission on device. + public IEnumerable 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