[USB] Update exception handling code
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / Usb / UsbConfiguration.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 using System;
19 using System.Collections.Generic;
20
21 namespace Tizen.System.Usb
22 {
23     /// <summary>
24     /// Class to manage USB Configuration.
25     /// </summary>
26     /// <since_tizen> 4 </since_tizen>
27     public class UsbConfiguration : IDisposable
28     {
29         internal readonly Interop.UsbConfigHandle _handle;
30         private readonly UsbDevice _parent;
31         private Dictionary<int, UsbInterface> _interfaces;
32
33         internal UsbConfiguration(UsbDevice parent, Interop.UsbConfigHandle handle)
34         {
35             _parent = parent;
36             _handle = handle;
37         }
38
39         /// <summary>
40         /// Checks if device is self-powered in given configuration.
41         /// </summary>
42         /// <since_tizen> 4 </since_tizen>
43         public bool IsSelfPowered
44         {
45             get
46             {
47                 ThrowIfDisposed();
48                 return Interop.NativeGet<bool>(_handle.IsSelfPowered);
49             }
50         }
51
52         /// <summary>
53         /// Checks if device in given configuration supports remote wakeup.
54         /// </summary>
55         /// <since_tizen> 4 </since_tizen>
56         public bool SupportRemoteWakeup
57         {
58             get
59             {
60                 ThrowIfDisposed();
61                 return Interop.NativeGet<bool>(_handle.SupportRemoteWakeup);
62             }
63         }
64
65         /// <summary>
66         /// Gets maximum power required in given configuration, in mA.
67         /// </summary>
68         /// <since_tizen> 4 </since_tizen>
69         public int MaximumPowerRequired
70         {
71             get
72             {
73                 ThrowIfDisposed();
74                 return Interop.NativeGet<int>(_handle.GetMaxPower);
75             }
76         }
77
78         /// <summary>
79         /// Dictionary mapping interfaces Ids to interface instances for given configuration.
80         /// </summary>
81         /// <since_tizen> 4 </since_tizen>
82         public IReadOnlyDictionary<int, UsbInterface> Interfaces
83         {
84             get
85             {
86                 ThrowIfDisposed();
87                 if (_interfaces == null)
88                 {
89                     _interfaces = new Dictionary<int, UsbInterface>();
90                     int count = Interop.NativeGet<int>(_handle.GetNumInterfaces);
91                     for (int i = 0; i < count; ++i)
92                     {
93                         IntPtr handle;
94                         _handle.GetInterface(i, out handle);
95                         UsbInterface usbInterface = new UsbInterface(this, new Interop.UsbInterfaceHandle(handle));
96                         _interfaces.Add(usbInterface.Id, usbInterface);
97                     }
98                 }
99                 return _interfaces;
100             }
101         }
102
103         /// <summary>
104         /// Configuration string.
105         /// </summary>
106         /// <exception cref="InvalidOperationException"> Throws exception if device is disconnected or not opened for operation. </exception>
107         /// <since_tizen> 4 </since_tizen>
108         public string ConfigurationString
109         {
110             get
111             {
112                 ThrowIfDisposed();
113                 _parent.ThrowIfDeviceNotOpened();
114                 return Interop.DescriptorString(_handle.GetConfigStr);
115             }
116         }
117
118         /// <summary>
119         /// Set this configuration as active configuration for the device.
120         /// </summary>
121         /// <exception cref="InvalidOperationException"> Throws exception if device is disconnected or not opened for operation. </exception>
122         /// <since_tizen> 4 </since_tizen>
123         public void SetAsActive()
124         {
125             ThrowIfDisposed();
126             _parent.ThrowIfDeviceNotOpened();
127             _handle.SetAsActive().ThrowIfFailed("Failed to activate this configuration");
128         }
129
130         internal void ThrowIfDisposed()
131         {
132             if (disposedValue) throw new ObjectDisposedException("Configuration is already disposed");
133             _parent.ThrowIfDisposed();
134         }
135
136         internal void ThrowIfDeviceNotOpened()
137         {
138             _parent.ThrowIfDeviceNotOpened();
139         }
140
141         #region IDisposable Support
142         private bool disposedValue = false;
143
144         /// <summary>
145         /// Releases all resources used by the ConnectionProfile.
146         /// It should be called after finished using of the object.</summary>
147         /// <since_tizen> 4 </since_tizen>
148         protected virtual void Dispose(bool disposing)
149         {
150             if (!disposedValue)
151             {
152                 _handle.Dispose();
153                 disposedValue = true;
154             }
155         }
156
157         /// <summary>
158         /// Finalizes an instance of the UsbConfiguration class.
159         /// </summary>
160         ~UsbConfiguration()
161         {
162             Dispose(false);
163         }
164
165         /// <summary>
166         /// Releases all resources used by the ConnectionProfile.
167         /// It should be called after finished using of the object.</summary>
168         /// <since_tizen> 4 </since_tizen>
169         public void Dispose()
170         {
171             Dispose(true);
172             GC.SuppressFinalize(this);
173         }
174         #endregion
175     }
176 }