[TCSACR-93] Add USB Host C# API
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / Interop / Interop.Context.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Runtime.InteropServices;
21
22 internal static partial class Interop
23 {
24     internal enum HotplugEventType
25     {
26         Attach, // USB_HOST_HOTPLUG_EVENT_ATTACH
27         Detach, // USB_HOST_HOTPLUG_EVENT_DETACH
28         Any, // USB_HOST_HOTPLUG_EVENT_ANY
29     }
30
31     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
32     internal delegate void HostHotplugCallback(IntPtr /* usb_host_device_h */ dev, IntPtr /* void */ userData);
33
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);
36
37     [DllImport(Libraries.Usb, EntryPoint = "usb_host_unset_hotplug_cb")]
38     internal static extern ErrorCode UnsetHotplugCb(this HostHotplugHandle /* usb_host_hotplug_h */ handle);
39
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);
42
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);
45
46     internal class UsbContextHandle : SafeUsbHandle
47     {
48         private IntPtr nativeDevListPtr = IntPtr.Zero;
49
50         [DllImport(Libraries.Usb, EntryPoint = "usb_host_create")]
51         internal static extern ErrorCode Create(out IntPtr /* usb_host_context_h */ ctx);
52
53         [DllImport(Libraries.Usb, EntryPoint = "usb_host_destroy")]
54         internal static extern ErrorCode Destroy(UsbContextHandle /* usb_host_context_h */ ctx);
55
56         [DllImport(Libraries.Usb, EntryPoint = "usb_host_free_device_list")]
57         internal static extern ErrorCode FreeDeviceList(IntPtr deviceList, bool unrefDevices);
58
59         internal UsbContextHandle()
60         {
61             Create(out handle).ThrowIfFailed("Failed to create native context handle");
62         }
63
64         public override void Destroy()
65         {
66             if (nativeDevListPtr != IntPtr.Zero)
67             {
68                 FreeDeviceList(nativeDevListPtr, true).ThrowIfFailed("Failed to free native device list");
69                 nativeDevListPtr = IntPtr.Zero;
70             }
71             Destroy(this).ThrowIfFailed("Failed to destroy native context handle");
72         }
73
74         internal List<HostDeviceHandle> GetDeviceList()
75         {
76             if (nativeDevListPtr != IntPtr.Zero)
77             {
78                 FreeDeviceList(nativeDevListPtr, true).ThrowIfFailed("Failed to free native device list");
79                 nativeDevListPtr = IntPtr.Zero;
80             }
81
82             int length;
83             ErrorCode err = this.GetDeviceList(out nativeDevListPtr, out length);
84             err.ThrowIfFailed("Failed to get device list for the context");
85
86             List<HostDeviceHandle> deviceList = new List<HostDeviceHandle>();
87             IntPtr[] nativeDevList = new IntPtr[length];
88             Marshal.Copy(nativeDevListPtr, nativeDevList, 0, length);
89
90             foreach (var devHandle in nativeDevList)
91             {
92                 deviceList.Add(new HostDeviceHandle(devHandle));
93             }
94
95             return deviceList;
96         }
97     }
98
99     internal class HostHotplugHandle
100     {
101         private IntPtr _handle;
102         public HostHotplugHandle(IntPtr handle) { _handle = handle; }
103     }
104 }