/
UsbConfiguration.cs
executable file
·193 lines (177 loc) · 6.83 KB
/
UsbConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
namespace Tizen.System.Usb
{
/// <summary>
/// A class to manage the USB configuration.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public class UsbConfiguration : IDisposable
{
internal readonly Interop.UsbConfigHandle _handle;
private readonly UsbDevice _parent;
private Dictionary<int, UsbInterface> _interfaces;
internal UsbConfiguration(UsbDevice parent, Interop.UsbConfigHandle handle)
{
_parent = parent;
_handle = handle;
}
/// <summary>
/// Checks if the device is self-powered in a given configuration.
/// </summary>
/// <feature>http://tizen.org/feature/usb.host</feature>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <since_tizen> 4 </since_tizen>
public bool IsSelfPowered
{
get
{
ThrowIfDisposed();
return Interop.NativeGet<bool>(_handle.IsSelfPowered);
}
}
/// <summary>
/// Checks if the device in a given configuration supports remote wakeup.
/// </summary>
/// <feature>http://tizen.org/feature/usb.host</feature>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <since_tizen> 4 </since_tizen>
public bool SupportRemoteWakeup
{
get
{
ThrowIfDisposed();
return Interop.NativeGet<bool>(_handle.SupportRemoteWakeup);
}
}
/// <summary>
/// Gets the maximum power required in a given configuration, in mA.
/// </summary>
/// <feature>http://tizen.org/feature/usb.host</feature>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <since_tizen> 4 </since_tizen>
public int MaximumPowerRequired
{
get
{
ThrowIfDisposed();
return Interop.NativeGet<int>(_handle.GetMaxPower);
}
}
/// <summary>
/// A dictionary for mapping the interface IDs to interface instances for a given configuration.
/// </summary>
/// <feature>http://tizen.org/feature/usb.host</feature>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <since_tizen> 4 </since_tizen>
public IReadOnlyDictionary<int, UsbInterface> Interfaces
{
get
{
ThrowIfDisposed();
if (_interfaces == null)
{
_interfaces = new Dictionary<int, UsbInterface>();
int count = Interop.NativeGet<int>(_handle.GetNumInterfaces);
for (int i = 0; i < count; ++i)
{
IntPtr handle;
_handle.GetInterface(i, out handle);
UsbInterface usbInterface = new UsbInterface(this, new Interop.UsbInterfaceHandle(handle));
_interfaces.Add(usbInterface.Id, usbInterface);
}
}
return _interfaces;
}
}
/// <summary>
/// The configuration string.
/// </summary>
/// <feature>http://tizen.org/feature/usb.host</feature>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <exception cref="InvalidOperationException">
/// Throws exception if device is disconnected or not opened for operation or busy as its interfaces are currently claimed.
/// </exception>
/// <since_tizen> 4 </since_tizen>
public string ConfigurationString
{
get
{
ThrowIfDisposed();
_parent.ThrowIfDeviceNotOpened();
return Interop.DescriptorString(_handle.GetConfigStr);
}
}
/// <summary>
/// Sets this configuration as an active configuration for the device.
/// </summary>
/// <feature>http://tizen.org/feature/usb.host</feature>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <exception cref="InvalidOperationException">
/// Throws an exception if the device is disconnected, or not opened for an operation, or busy as its interfaces are currently claimed.
/// </exception>
/// <since_tizen> 4 </since_tizen>
public void SetAsActive()
{
ThrowIfDisposed();
_parent.ThrowIfDeviceNotOpened();
_handle.SetAsActive().ThrowIfFailed("Failed to activate this configuration");
}
internal void ThrowIfDisposed()
{
if (disposedValue) throw new ObjectDisposedException("Configuration is already disposed");
_parent.ThrowIfDisposed();
}
internal void ThrowIfDeviceNotOpened()
{
_parent.ThrowIfDeviceNotOpened();
}
#region IDisposable Support
private bool disposedValue = false;
/// <summary>
/// Releases all the resources used by the ConnectionProfile.
/// It should be called after it has finished using the object.</summary>
/// <since_tizen> 4 </since_tizen>
internal virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
_handle.Dispose();
disposedValue = true;
}
}
/// <summary>
/// Finalizes an instance of the UsbConfiguration class.
/// </summary>
/// <since_tizen> 4 </since_tizen>
~UsbConfiguration()
{
Dispose(false);
}
/// <summary>
/// Releases all the resources used by the ConnectionProfile.
/// It should be called after it has finished using the object.</summary>
/// <since_tizen> 4 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}