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 /// <since_tizen> 4 </since_tizen>
27 public class UsbDevice : IDisposable
29 internal readonly Interop.HostDeviceHandle _handle;
30 private readonly UsbManager _parent;
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>
42 /// <since_tizen> 4 </since_tizen>
47 return Interop.NativeGet<int>(_handle.GetBusNumber);
52 /// Address of device on the bus.
54 /// <since_tizen> 4 </since_tizen>
60 return Interop.NativeGet<int>(_handle.GetAddress);
65 /// List of available port numbers from a device.
67 /// <since_tizen> 4 </since_tizen>
68 public IEnumerable<int> Ports
73 return _handle.Ports();
78 /// Checks if device is opened.
80 /// <since_tizen> 4 </since_tizen>
86 return Interop.NativeGet<bool>(_handle.IsOpened);
91 /// Control endpoint (endpoint 0).
93 /// <since_tizen> 4 </since_tizen>
94 public UsbControlEndpoint ControlEndpoint
99 return new UsbControlEndpoint(this);
104 /// Active configuration for the device.
106 /// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation. </exception>
107 /// <since_tizen> 4 </since_tizen>
108 public UsbConfiguration ActiveConfiguration
113 ThrowIfDeviceNotOpened();
114 IntPtr handle = Interop.NativeGet<IntPtr>(_handle.GetActiveConfig);
115 Interop.UsbConfigHandle configHandle = new Interop.UsbConfigHandle(handle);
116 return new UsbConfiguration(this, configHandle);
121 /// Dictionary mapping configuration Ids to configuration instances for this device.
123 /// <since_tizen> 4 </since_tizen>
124 public IReadOnlyDictionary<int, UsbConfiguration> Configurations
129 var configurations = new Dictionary<int, UsbConfiguration>();
130 int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
131 for (int i = 0; i < count; ++i)
134 _handle.GetConfig(i, out configHandle);
135 configurations.Add(i, new UsbConfiguration(this, new Interop.UsbConfigHandle(configHandle)));
137 return configurations;
142 /// Device information such as version, class, subclass etc.
144 /// <since_tizen> 4 </since_tizen>
145 public UsbDeviceInformation DeviceInformation
150 return new UsbDeviceInformation(this);
155 /// String associated with device.
157 /// <exception cref="InvalidOperationException"> Throws exception if device is disconnected or not opened for operation. </exception>
158 /// <since_tizen> 4 </since_tizen>
159 public UsbDeviceStrings Strings
164 ThrowIfDeviceNotOpened();
165 return new UsbDeviceStrings(this, "us-ascii");
170 /// Opens device, which allows performing operations on it.
172 /// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
173 /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
174 /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
175 /// <since_tizen> 4 </since_tizen>
179 _handle.Open().ThrowIfFailed("Failed to open device for use");
183 /// Closes device for operations.
185 /// <since_tizen> 4 </since_tizen>
189 if (IsOpened == false) return;
191 _handle.CloseHandle().ThrowIfFailed("Failed to close device for use");
194 internal void ThrowIfDisposed()
196 if (disposedValue) throw new ObjectDisposedException("USB Device is already disposed");
197 _parent.ThrowIfDisposed();
200 internal void ThrowIfDeviceNotOpened()
202 if (IsOpened == false) throw new InvalidOperationException("USB Device is should be in open state for this operation");
205 #region IDisposable Support
206 private bool disposedValue = false;
209 /// Releases all resources used by the ConnectionProfile.
210 /// It should be called after finished using of the object.</summary>
211 /// <since_tizen> 4 </since_tizen>
212 protected virtual void Dispose(bool disposing)
217 disposedValue = true;
222 /// Finalizes an instance of the UsbDevice class.
230 /// Releases all resources used by the ConnectionProfile.
231 /// It should be called after finished using of the object.</summary>
232 /// <since_tizen> 4 </since_tizen>
233 public void Dispose()
236 GC.SuppressFinalize(this);