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.
19 using System.Collections.Generic;
21 namespace Tizen.System.Usb
24 /// Class to manage USB Configuration.
26 /// <since_tizen> 4 </since_tizen>
27 public class UsbConfiguration : IDisposable
29 internal readonly Interop.UsbConfigHandle _handle;
30 private readonly UsbDevice _parent;
31 private Dictionary<int, UsbInterface> _interfaces;
33 internal UsbConfiguration(UsbDevice parent, Interop.UsbConfigHandle handle)
40 /// Checks if device is self-powered in given configuration.
42 /// <feature>http://tizen.org/feature/usb.host</feature>
43 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
44 /// <since_tizen> 4 </since_tizen>
45 public bool IsSelfPowered
50 return Interop.NativeGet<bool>(_handle.IsSelfPowered);
55 /// Checks if device in given configuration supports remote wakeup.
57 /// <feature>http://tizen.org/feature/usb.host</feature>
58 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
59 /// <since_tizen> 4 </since_tizen>
60 public bool SupportRemoteWakeup
65 return Interop.NativeGet<bool>(_handle.SupportRemoteWakeup);
70 /// Gets maximum power required in given configuration, in mA.
72 /// <feature>http://tizen.org/feature/usb.host</feature>
73 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
74 /// <since_tizen> 4 </since_tizen>
75 public int MaximumPowerRequired
80 return Interop.NativeGet<int>(_handle.GetMaxPower);
85 /// Dictionary mapping interfaces Ids to interface instances for given configuration.
87 /// <feature>http://tizen.org/feature/usb.host</feature>
88 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
89 /// <since_tizen> 4 </since_tizen>
90 public IReadOnlyDictionary<int, UsbInterface> Interfaces
95 if (_interfaces == null)
97 _interfaces = new Dictionary<int, UsbInterface>();
98 int count = Interop.NativeGet<int>(_handle.GetNumInterfaces);
99 for (int i = 0; i < count; ++i)
102 _handle.GetInterface(i, out handle);
103 UsbInterface usbInterface = new UsbInterface(this, new Interop.UsbInterfaceHandle(handle));
104 _interfaces.Add(usbInterface.Id, usbInterface);
112 /// Configuration string.
114 /// <feature>http://tizen.org/feature/usb.host</feature>
115 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
116 /// <exception cref="InvalidOperationException">
117 /// Throws exception if device is disconnected or not opened for operation or busy as its interfaces are currently claimed.
119 /// <since_tizen> 4 </since_tizen>
120 public string ConfigurationString
125 _parent.ThrowIfDeviceNotOpened();
126 return Interop.DescriptorString(_handle.GetConfigStr);
131 /// Set this configuration as active configuration for the device.
133 /// <feature>http://tizen.org/feature/usb.host</feature>
134 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
135 /// <exception cref="InvalidOperationException"> Throws exception if device is disconnected or not opened for operation. </exception>
136 /// <since_tizen> 4 </since_tizen>
137 public void SetAsActive()
140 _parent.ThrowIfDeviceNotOpened();
141 _handle.SetAsActive().ThrowIfFailed("Failed to activate this configuration");
144 internal void ThrowIfDisposed()
146 if (disposedValue) throw new ObjectDisposedException("Configuration is already disposed");
147 _parent.ThrowIfDisposed();
150 internal void ThrowIfDeviceNotOpened()
152 _parent.ThrowIfDeviceNotOpened();
155 #region IDisposable Support
156 private bool disposedValue = false;
159 /// Releases all resources used by the ConnectionProfile.
160 /// It should be called after finished using of the object.</summary>
161 /// <since_tizen> 4 </since_tizen>
162 protected virtual void Dispose(bool disposing)
167 disposedValue = true;
172 /// Finalizes an instance of the UsbConfiguration class.
180 /// Releases all resources used by the ConnectionProfile.
181 /// It should be called after finished using of the object.</summary>
182 /// <since_tizen> 4 </since_tizen>
183 public void Dispose()
186 GC.SuppressFinalize(this);