Setting since_tizen 3/4 on Tizen.NET API
[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         /// <since_tizen> 4 </since_tizen>
107         public string ConfigurationString
108         {
109             get
110             {
111                 ThrowIfDisposed();
112                 return Interop.DescriptorString(_handle.GetConfigStr);
113             }
114         }
115
116         /// <summary>
117         /// Set this configuration as active configuration for the device.
118         /// </summary>
119         /// <exception cref="InvalidOperationException">
120         /// Throws exception if device is disconnected or not opened for operation or busy as its interfaces are currently claimed.
121         /// </exception>
122         /// <since_tizen> 4 </since_tizen>
123         public void SetAsActive()
124         {
125             ThrowIfDisposed();
126             _handle.SetAsActive();
127         }
128
129         internal void ThrowIfDisposed()
130         {
131             if (disposedValue) throw new ObjectDisposedException("Configuration is already disposed");
132             _parent.ThrowIfDisposed();
133         }
134
135         #region IDisposable Support
136         private bool disposedValue = false;
137
138         /// <summary>
139         /// Releases all resources used by the ConnectionProfile.
140         /// It should be called after finished using of the object.</summary>
141         /// <since_tizen> 4 </since_tizen>
142         protected virtual void Dispose(bool disposing)
143         {
144             if (!disposedValue)
145             {
146                 _handle.Dispose();
147                 disposedValue = true;
148             }
149         }
150
151         /// <summary>
152         /// Finalizes an instance of the UsbConfiguration class.
153         /// </summary>
154         ~UsbConfiguration()
155         {
156             Dispose(false);
157         }
158
159         /// <summary>
160         /// Releases all resources used by the ConnectionProfile.
161         /// It should be called after finished using of the object.</summary>
162         /// <since_tizen> 4 </since_tizen>
163         public void Dispose()
164         {
165             Dispose(true);
166             GC.SuppressFinalize(this);
167         }
168         #endregion
169     }
170 }