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;
32 internal UsbDevice(UsbManager parent, Interop.HostDeviceHandle handle)
39 /// Number of the bus, this device is connected to.
41 /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
46 return Interop.NativeGet<int>(_handle.GetBusNumber);
51 /// Address of device on the bus.
58 return Interop.NativeGet<int>(_handle.GetAddress);
63 /// List of available port numbers from a device.
65 public IEnumerable<int> Ports
70 return _handle.Ports();
75 /// Checks if device is opened.
82 return Interop.NativeGet<bool>(_handle.IsOpened);
87 /// Control endpoint (endpoint 0).
89 public UsbControlEndpoint ControlEndpoint
94 return new UsbControlEndpoint(this);
99 /// Active configuration for the device.
101 /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
102 public UsbConfiguration ActiveConfiguration
107 Interop.UsbConfigHandle configHandle = Interop.NativeGet<Interop.UsbConfigHandle>(_handle.GetActiveConfig);
108 return _configurations.Values.Where(config => config._handle == configHandle).First();
113 /// Dictionary mapping configuration Ids to configuration instances for this device.
115 public IReadOnlyDictionary<int, UsbConfiguration> Configurations
120 if (_configurations == null)
122 _configurations = new Dictionary<int, UsbConfiguration>();
123 int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
124 for (int i = 0; i < count; ++i)
126 Interop.UsbConfigHandle configHandle;
127 _handle.GetConfig(i, out configHandle);
128 _configurations.Add(i, new UsbConfiguration(this, configHandle));
131 return _configurations;
136 /// Device information such as version, class, subclass etc.
138 public UsbDeviceInformation DeviceInformation
143 return new UsbDeviceInformation(this);
148 /// String associated with device.
150 public UsbDeviceStrings Strings
155 return new UsbDeviceStrings(this, "us-ascii");
160 /// Opens device, which allows performing operations on it.
162 /// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
163 /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
164 /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
168 _handle.Open().ThrowIfFailed("Failed to open device for use");
172 /// Closes device for operations.
174 /// <exception cref="InvalidOperationException">Throws exception if device is not opened for operation.</exception>
178 if (IsOpened == false) throw new InvalidOperationException("Device must be opened for operation first");
183 internal void ThrowIfDisposed()
185 if (disposedValue) throw new ObjectDisposedException("USB Device is already disposed");
186 _parent.ThrowIfDisposed();
189 #region IDisposable Support
190 private bool disposedValue = false;
193 /// Releases all resources used by the ConnectionProfile.
194 /// It should be called after finished using of the object.</summary>
195 protected virtual void Dispose(bool disposing)
203 foreach(var config in _configurations.Values) {
206 _configurations.Clear();
208 disposedValue = true;
213 /// Finalizes an instance of the UsbDevice class.
221 /// Releases all resources used by the ConnectionProfile.
222 /// It should be called after finished using of the object.</summary>
223 public void Dispose()
226 GC.SuppressFinalize(this);