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;
21 namespace Tizen.System.Usb
24 /// Class to manage USB host devices. This class contains operations for enumerating, opening and closing devices.
26 public class UsbDevice : IDisposable
28 internal readonly Interop.HostDeviceHandle _handle;
29 private readonly UsbManager _parent;
30 private Dictionary<int, UsbConfiguration> _configurations = new Dictionary<int, UsbConfiguration>();
32 internal UsbDevice(UsbManager parent, Interop.HostDeviceHandle handle)
37 int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
38 for (int i = 0; i < count; ++i)
40 Interop.UsbConfigHandle configHandle;
41 _handle.GetConfig(i, out configHandle);
42 _configurations.Add(i, new UsbConfiguration(this, configHandle));
47 /// Number of the bus, this device is connected to.
49 /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
54 return Interop.NativeGet<int>(_handle.GetBusNumber);
59 /// Address of device on the bus.
66 return Interop.NativeGet<int>(_handle.GetAddress);
71 /// List of available port numbers from a device.
73 public IEnumerable<int> Ports
78 return _handle.Ports();
83 /// Checks if device is opened.
90 return Interop.NativeGet<bool>(_handle.IsOpened);
95 /// Control endpoint (endpoint 0).
97 public UsbControlEndpoint ControlEndpoint
102 return new UsbControlEndpoint(this);
107 /// Active configuration for the device.
109 /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
110 public UsbConfiguration ActiveConfiguration
115 Interop.UsbConfigHandle configHandle = Interop.NativeGet<Interop.UsbConfigHandle>(_handle.GetActiveConfig);
116 return _configurations.Values.Where(config => config._handle == configHandle).First();
121 /// Dictionary mapping configuration Ids to configuration instances for this device.
123 public IReadOnlyDictionary<int, UsbConfiguration> Configurations
128 return _configurations;
133 /// Device information such as version, class, subclass etc.
135 public UsbDeviceInformation DeviceInformation
140 return new UsbDeviceInformation(this);
145 /// String associated with device.
147 public UsbDeviceStrings Strings
152 return new UsbDeviceStrings(this, "us-ascii");
157 /// Opens device, which allows performing operations on it.
159 /// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
160 /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
161 /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
165 _handle.Open().ThrowIfFailed("Failed to open device for use");
169 /// Closes device for operations.
171 /// <exception cref="InvalidOperationException">Throws exception if device is not opened for operation.</exception>
175 if (IsOpened == false) throw new InvalidOperationException("Device must be opened for operation first");
177 _handle.CloseHandle().ThrowIfFailed("Failed to close device for use");
180 internal void ThrowIfDisposed()
182 if (disposedValue) throw new ObjectDisposedException("USB Device is already disposed");
183 _parent.ThrowIfDisposed();
186 #region IDisposable Support
187 private bool disposedValue = false;
190 /// Releases all resources used by the ConnectionProfile.
191 /// It should be called after finished using of the object.</summary>
192 protected virtual void Dispose(bool disposing)
200 foreach(var config in _configurations.Values) {
203 _configurations.Clear();
205 disposedValue = true;
210 /// Finalizes an instance of the UsbDevice class.
218 /// Releases all resources used by the ConnectionProfile.
219 /// It should be called after finished using of the object.</summary>
220 public void Dispose()
223 GC.SuppressFinalize(this);