2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
20 using System.Runtime.InteropServices;
22 internal static partial class Interop
24 internal enum HotplugEventType
26 Attach, // USB_HOST_HOTPLUG_EVENT_ATTACH
27 Detach, // USB_HOST_HOTPLUG_EVENT_DETACH
28 Any, // USB_HOST_HOTPLUG_EVENT_ANY
31 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
32 internal delegate void HostHotplugCallback(IntPtr /* usb_host_device_h */ dev, IntPtr /* void */ userData);
34 [DllImport(Libraries.Usb, EntryPoint = "usb_host_set_hotplug_cb")]
35 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);
37 [DllImport(Libraries.Usb, EntryPoint = "usb_host_unset_hotplug_cb")]
38 internal static extern ErrorCode UnsetHotplugCb(this HostHotplugHandle /* usb_host_hotplug_h */ handle);
40 [DllImport(Libraries.Usb, EntryPoint = "usb_host_get_device_list")]
41 internal static extern ErrorCode GetDeviceList(this UsbContextHandle /* usb_host_context_h */ ctx, out IntPtr /* usb_host_device_h */ devs, out int length);
43 [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_open_with_vid_pid")]
44 internal static extern ErrorCode OpenWithVidPid(this UsbContextHandle /* usb_host_context_h */ ctx, int vendorId, int productId, out HostDeviceHandle /* usb_host_device_h */ deviceHandle);
46 internal class UsbContextHandle : SafeUsbHandle
48 private IntPtr nativeDevListPtr = IntPtr.Zero;
50 [DllImport(Libraries.Usb, EntryPoint = "usb_host_create")]
51 internal static extern ErrorCode Create(out IntPtr /* usb_host_context_h */ ctx);
53 [DllImport(Libraries.Usb, EntryPoint = "usb_host_destroy")]
54 internal static extern ErrorCode Destroy(UsbContextHandle /* usb_host_context_h */ ctx);
56 [DllImport(Libraries.Usb, EntryPoint = "usb_host_free_device_list")]
57 internal static extern ErrorCode FreeDeviceList(IntPtr deviceList, bool unrefDevices);
59 internal UsbContextHandle()
61 Create(out handle).ThrowIfFailed("Failed to create native context handle");
64 public override void Destroy()
66 if (nativeDevListPtr != IntPtr.Zero)
68 FreeDeviceList(nativeDevListPtr, true).ThrowIfFailed("Failed to free native device list");
69 nativeDevListPtr = IntPtr.Zero;
71 Destroy(this).ThrowIfFailed("Failed to destroy native context handle");
74 internal List<HostDeviceHandle> GetDeviceList()
76 if (nativeDevListPtr != IntPtr.Zero)
78 FreeDeviceList(nativeDevListPtr, true).ThrowIfFailed("Failed to free native device list");
79 nativeDevListPtr = IntPtr.Zero;
83 ErrorCode err = this.GetDeviceList(out nativeDevListPtr, out length);
84 err.ThrowIfFailed("Failed to get device list for the context");
86 List<HostDeviceHandle> deviceList = new List<HostDeviceHandle>();
87 IntPtr[] nativeDevList = new IntPtr[length];
88 Marshal.Copy(nativeDevListPtr, nativeDevList, 0, length);
90 foreach (var devHandle in nativeDevList)
92 deviceList.Add(new HostDeviceHandle(devHandle));
99 internal class HostHotplugHandle
101 private IntPtr _handle;
102 public HostHotplugHandle(IntPtr handle) { _handle = handle; }