[ElmSharp] Fixed the crash issue of Calendar
[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> 5 </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         /// <feature>http://tizen.org/feature/usb.host</feature>
43         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
44         /// <since_tizen> 5 </since_tizen>
45         public bool IsSelfPowered
46         {
47             get
48             {
49                 ThrowIfDisposed();
50                 return Interop.NativeGet<bool>(_handle.IsSelfPowered);
51             }
52         }
53
54         /// <summary>
55         /// Checks if device in given configuration supports remote wakeup.
56         /// </summary>
57         /// <feature>http://tizen.org/feature/usb.host</feature>
58         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
59         /// <since_tizen> 5 </since_tizen>
60         public bool SupportRemoteWakeup
61         {
62             get
63             {
64                 ThrowIfDisposed();
65                 return Interop.NativeGet<bool>(_handle.SupportRemoteWakeup);
66             }
67         }
68
69         /// <summary>
70         /// Gets maximum power required in given configuration, in mA.
71         /// </summary>
72         /// <feature>http://tizen.org/feature/usb.host</feature>
73         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
74         /// <since_tizen> 5 </since_tizen>
75         public int MaximumPowerRequired
76         {
77             get
78             {
79                 ThrowIfDisposed();
80                 return Interop.NativeGet<int>(_handle.GetMaxPower);
81             }
82         }
83
84         /// <summary>
85         /// Dictionary mapping interfaces Ids to interface instances for given configuration.
86         /// </summary>
87         /// <feature>http://tizen.org/feature/usb.host</feature>
88         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
89         /// <since_tizen> 5 </since_tizen>
90         public IReadOnlyDictionary<int, UsbInterface> Interfaces
91         {
92             get
93             {
94                 ThrowIfDisposed();
95                 if (_interfaces == null)
96                 {
97                     _interfaces = new Dictionary<int, UsbInterface>();
98                     int count = Interop.NativeGet<int>(_handle.GetNumInterfaces);
99                     for (int i = 0; i < count; ++i)
100                     {
101                         IntPtr handle;
102                         _handle.GetInterface(i, out handle);
103                         UsbInterface usbInterface = new UsbInterface(this, new Interop.UsbInterfaceHandle(handle));
104                         _interfaces.Add(usbInterface.Id, usbInterface);
105                     }
106                 }
107                 return _interfaces;
108             }
109         }
110
111         /// <summary>
112         /// Configuration string.
113         /// </summary>
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.
118         /// </exception>
119         /// <since_tizen> 5 </since_tizen>
120         public string ConfigurationString
121         {
122             get
123             {
124                 ThrowIfDisposed();
125                 _parent.ThrowIfDeviceNotOpened();
126                 return Interop.DescriptorString(_handle.GetConfigStr);
127             }
128         }
129
130         /// <summary>
131         /// Set this configuration as active configuration for the device.
132         /// </summary>
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> 5 </since_tizen>
137         public void SetAsActive()
138         {
139             ThrowIfDisposed();
140             _parent.ThrowIfDeviceNotOpened();
141             _handle.SetAsActive().ThrowIfFailed("Failed to activate this configuration");
142         }
143
144         internal void ThrowIfDisposed()
145         {
146             if (disposedValue) throw new ObjectDisposedException("Configuration is already disposed");
147             _parent.ThrowIfDisposed();
148         }
149
150         internal void ThrowIfDeviceNotOpened()
151         {
152             _parent.ThrowIfDeviceNotOpened();
153         }
154
155         #region IDisposable Support
156         private bool disposedValue = false;
157
158         /// <summary>
159         /// Releases all resources used by the ConnectionProfile.
160         /// It should be called after finished using of the object.</summary>
161         /// <since_tizen> 5 </since_tizen>
162         protected virtual void Dispose(bool disposing)
163         {
164             if (!disposedValue)
165             {
166                 _handle.Dispose();
167                 disposedValue = true;
168             }
169         }
170
171         /// <summary>
172         /// Finalizes an instance of the UsbConfiguration class.
173         /// </summary>
174         /// <since_tizen> 5 </since_tizen>
175         ~UsbConfiguration()
176         {
177             Dispose(false);
178         }
179
180         /// <summary>
181         /// Releases all resources used by the ConnectionProfile.
182         /// It should be called after finished using of the object.</summary>
183         /// <since_tizen> 5 </since_tizen>
184         public void Dispose()
185         {
186             Dispose(true);
187             GC.SuppressFinalize(this);
188         }
189         #endregion
190     }
191 }