Merge "[Multimedia] Replaced the type of the buffer of MediaPacket with the common...
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / Interop / Interop.Device.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     [DllImport(Libraries.Usb, EntryPoint = "usb_host_ref_device")]
25     internal static extern ErrorCode RefDevice(this HostDeviceHandle /* usb_host_device_h */ dev);
26
27     [DllImport(Libraries.Usb, EntryPoint = "usb_host_unref_device")]
28     internal static extern ErrorCode UnrefDevice(this HostDeviceHandle /* usb_host_device_h */ dev);
29
30     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_open")]
31     internal static extern ErrorCode Open(this HostDeviceHandle /* usb_host_device_h */ dev);
32
33     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_close")]
34     internal static extern ErrorCode Close(this HostDeviceHandle /* usb_host_device_h */ dev);
35
36     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_bus_number")]
37     internal static extern ErrorCode GetBusNumber(this HostDeviceHandle /* usb_host_device_h */ dev, out int busNumber);
38
39     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_address")]
40     internal static extern ErrorCode GetAddress(this HostDeviceHandle /* usb_host_device_h */ dev, out int deviceAddress);
41
42     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_port_numbers")]
43     internal static extern ErrorCode GetPortNumbers(this HostDeviceHandle /* usb_host_device_h */ dev, [MarshalAs(UnmanagedType.SysInt, SizeParamIndex = 2)] [In, Out] int[] portNumbers, int portNumbersLen, out int portsCount);
44
45     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_config")]
46     internal static extern ErrorCode GetConfig(this HostDeviceHandle /* usb_host_device_h */ dev, int configIndex, out UsbConfigHandle /* usb_host_config_h */ config);
47
48     [DllImport(Libraries.Usb, EntryPoint = "usb_host_get_active_config")]
49     internal static extern ErrorCode GetActiveConfig(this HostDeviceHandle /* usb_host_device_h */ dev, out UsbConfigHandle /* usb_host_config_h */ config);
50
51     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_bcd_usb")]
52     internal static extern ErrorCode GetBcdUsb(this HostDeviceHandle /* usb_host_device_h */ dev, out int bcdUsb);
53
54     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_class")]
55     internal static extern ErrorCode GetClass(this HostDeviceHandle /* usb_host_device_h */ dev, out int deviceClass);
56
57     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_sub_class")]
58     internal static extern ErrorCode GetSubClass(this HostDeviceHandle /* usb_host_device_h */ dev, out int subclass);
59
60     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_protocol")]
61     internal static extern ErrorCode GetProtocol(this HostDeviceHandle /* usb_host_device_h */ dev, out int protocol);
62
63     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_max_packet_size_0")]
64     internal static extern ErrorCode GetMaxPacketSize0(this HostDeviceHandle /* usb_host_device_h */ dev, out int maxPacketSize);
65
66     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_id_vendor")]
67     internal static extern ErrorCode GetIdVendor(this HostDeviceHandle /* usb_host_device_h */ dev, out int vendorId);
68
69     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_id_product")]
70     internal static extern ErrorCode GetIdProduct(this HostDeviceHandle /* usb_host_device_h */ dev, out int productId);
71
72     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_bcd_device")]
73     internal static extern ErrorCode GetBcdDevice(this HostDeviceHandle /* usb_host_device_h */ dev, out int deviceBcd);
74
75     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_num_configurations")]
76     internal static extern ErrorCode GetNumConfigurations(this HostDeviceHandle /* usb_host_device_h */ dev, out int numConfigurations);
77
78     [DllImport(Libraries.Usb, EntryPoint = "usb_host_is_device_opened")]
79     internal static extern ErrorCode IsOpened(this HostDeviceHandle /* usb_host_device_h */ dev, out bool isOpened);
80
81     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_manufacturer_str")]
82     internal static extern ErrorCode GetManufacturerStr(this HostDeviceHandle /* usb_host_device_h */ dev, ref int length, byte[] data);
83
84     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_product_str")]
85     internal static extern ErrorCode GetProductStr(this HostDeviceHandle /* usb_host_device_h */ dev, ref int length, byte[] data);
86
87     [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_serial_number_str")]
88     internal static extern ErrorCode GetSerialNumberStr(this HostDeviceHandle /* usb_host_device_h */ dev, ref int length, byte[] data);
89
90     [DllImport(Libraries.Usb, EntryPoint = "usb_host_control_transfer")]
91     internal static extern ErrorCode ControlTransfer(this HostDeviceHandle /* usb_host_device_h */ dev, byte requestType, byte request, ushort value, ushort index, byte[] data, ushort length, uint timeout, out int transferred);
92
93     internal class HostDeviceHandle : SafeUsbHandle
94     {
95         internal HostDeviceHandle(IntPtr handle) : base(handle) { }
96
97         public override void Destroy()
98         {
99             //this.UnrefDevice();
100         }
101
102         internal IEnumerable<int> Ports()
103         {
104             int actualPortsCount;
105             int[] portList = new int[MaxPortNumberCount];
106             var err = this.GetPortNumbers(portList, portList.Length, out actualPortsCount);
107             err.ThrowIfFailed("Failed to get device port list");
108             return portList.Take(actualPortsCount);
109         }
110     }
111 }