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> 5 </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 /// <feature>http://tizen.org/feature/usb.host</feature>
42 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
43 /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
44 /// <since_tizen> 5 </since_tizen>
49 return Interop.NativeGet<int>(_handle.GetBusNumber);
54 /// Address of device on the bus.
56 /// <feature>http://tizen.org/feature/usb.host</feature>
57 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
58 /// <since_tizen> 5 </since_tizen>
64 return Interop.NativeGet<int>(_handle.GetAddress);
69 /// List of available port numbers from a device.
71 /// <feature>http://tizen.org/feature/usb.host</feature>
72 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
73 /// <since_tizen> 5 </since_tizen>
74 public IEnumerable<int> Ports
79 return _handle.Ports();
84 /// Checks if device is opened.
86 /// <feature>http://tizen.org/feature/usb.host</feature>
87 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
88 /// <since_tizen> 5 </since_tizen>
94 return Interop.NativeGet<bool>(_handle.IsOpened);
99 /// Control endpoint (endpoint 0).
101 /// <feature>http://tizen.org/feature/usb.host</feature>
102 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
103 /// <since_tizen> 5 </since_tizen>
104 public UsbControlEndpoint ControlEndpoint
109 return new UsbControlEndpoint(this);
114 /// Active configuration for the device.
116 /// <feature>http://tizen.org/feature/usb.host</feature>
117 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
118 /// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation. </exception>
119 /// <since_tizen> 5 </since_tizen>
120 public UsbConfiguration ActiveConfiguration
125 ThrowIfDeviceNotOpened();
126 IntPtr handle = Interop.NativeGet<IntPtr>(_handle.GetActiveConfig);
127 Interop.UsbConfigHandle configHandle = new Interop.UsbConfigHandle(handle);
128 return new UsbConfiguration(this, configHandle);
133 /// Dictionary mapping configuration Ids to configuration instances for this device.
135 /// <feature>http://tizen.org/feature/usb.host</feature>
136 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
137 /// <since_tizen> 5 </since_tizen>
138 public IReadOnlyDictionary<int, UsbConfiguration> Configurations
143 var configurations = new Dictionary<int, UsbConfiguration>();
144 int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
145 for (int i = 0; i < count; ++i)
148 _handle.GetConfig(i, out configHandle);
149 configurations.Add(i, new UsbConfiguration(this, new Interop.UsbConfigHandle(configHandle)));
151 return configurations;
156 /// Device information such as version, class, subclass etc.
158 /// <feature>http://tizen.org/feature/usb.host</feature>
159 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
160 /// <since_tizen> 5 </since_tizen>
161 public UsbDeviceInformation DeviceInformation
166 return new UsbDeviceInformation(this);
171 /// String associated with device.
173 /// <feature>http://tizen.org/feature/usb.host</feature>
174 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
175 /// <exception cref="InvalidOperationException"> Throws exception if device is disconnected or not opened for operation. </exception>
176 /// <since_tizen> 5 </since_tizen>
177 public UsbDeviceStrings Strings
182 ThrowIfDeviceNotOpened();
183 return new UsbDeviceStrings(this, "us-ascii");
188 /// Opens device, which allows performing operations on it.
190 /// <feature>http://tizen.org/feature/usb.host</feature>
191 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
192 /// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
193 /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
194 /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
195 /// <since_tizen> 5 </since_tizen>
199 _handle.Open().ThrowIfFailed("Failed to open device for use");
203 /// Closes device for operations.
205 /// <feature>http://tizen.org/feature/usb.host</feature>
206 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
207 /// <since_tizen> 5 </since_tizen>
211 if (IsOpened == false) return;
213 _handle.CloseHandle().ThrowIfFailed("Failed to close device for use");
216 internal void ThrowIfDisposed()
218 if (disposedValue) throw new ObjectDisposedException("USB Device is already disposed");
219 _parent.ThrowIfDisposed();
222 internal void ThrowIfDeviceNotOpened()
224 if (IsOpened == false) throw new InvalidOperationException("USB Device is should be in open state for this operation");
227 #region IDisposable Support
228 private bool disposedValue = false;
231 /// Releases all resources used by the ConnectionProfile.
232 /// It should be called after finished using of the object.</summary>
233 /// <since_tizen> 5 </since_tizen>
234 protected virtual void Dispose(bool disposing)
239 disposedValue = true;
244 /// Finalizes an instance of the UsbDevice class.
252 /// Releases all resources used by the ConnectionProfile.
253 /// It should be called after finished using of the object.</summary>
254 /// <since_tizen> 5 </since_tizen>
255 public void Dispose()
258 GC.SuppressFinalize(this);