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 [DllImport(Libraries.Usb, EntryPoint = "usb_host_ref_device")]
25 internal static extern ErrorCode RefDevice(this HostDeviceHandle /* usb_host_device_h */ dev);
27 [DllImport(Libraries.Usb, EntryPoint = "usb_host_unref_device")]
28 internal static extern ErrorCode UnrefDevice(this HostDeviceHandle /* usb_host_device_h */ dev);
30 [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_open")]
31 internal static extern ErrorCode Open(this HostDeviceHandle /* usb_host_device_h */ dev);
33 [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_close")]
34 internal static extern ErrorCode CloseHandle(this HostDeviceHandle /* usb_host_device_h */ dev);
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);
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);
42 [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_port_numbers")]
43 internal static extern ErrorCode GetPortNumbers(this HostDeviceHandle /* usb_host_device_h */ dev, [In, Out] int[] portNumbers, int portNumbersLen, out int portsCount);
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 IntPtr /* usb_host_config_h */ config);
48 [DllImport(Libraries.Usb, EntryPoint = "usb_host_get_active_config")]
49 internal static extern ErrorCode GetActiveConfig(this HostDeviceHandle /* usb_host_device_h */ dev, out IntPtr /* usb_host_config_h */ config);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
93 internal class HostDeviceHandle : SafeUsbHandle
95 internal HostDeviceHandle(IntPtr handle) : base(handle) { }
97 public override void Destroy()
102 internal IEnumerable<int> Ports()
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);