--- /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;
+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; }
+ }
+}
--- /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;
+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