Release 4.0.0-preview1-00301
[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     public class UsbConfiguration : IDisposable
27     {
28         internal readonly Interop.UsbConfigHandle _handle;
29         private readonly UsbDevice _parent;
30         private Dictionary<int, UsbInterface> _interfaces;
31
32         internal UsbConfiguration(UsbDevice parent, Interop.UsbConfigHandle handle)
33         {
34             _parent = parent;
35             _handle = handle;
36         }
37
38         /// <summary>
39         /// Checks if device is self-powered in given configuration.
40         /// </summary>
41         public bool IsSelfPowered
42         {
43             get
44             {
45                 ThrowIfDisposed();
46                 return Interop.NativeGet<bool>(_handle.IsSelfPowered);
47             }
48         }
49
50         /// <summary>
51         /// Checks if device in given configuration supports remote wakeup.
52         /// </summary>
53         public bool SupportRemoteWakeup
54         {
55             get
56             {
57                 ThrowIfDisposed();
58                 return Interop.NativeGet<bool>(_handle.SupportRemoteWakeup);
59             }
60         }
61
62         /// <summary>
63         /// Gets maximum power required in given configuration, in mA.
64         /// </summary>
65         public int MaximumPowerRequired
66         {
67             get
68             {
69                 ThrowIfDisposed();
70                 return Interop.NativeGet<int>(_handle.GetMaxPower);
71             }
72         }
73
74         /// <summary>
75         /// Dictionary mapping interfaces Ids to interface instances for given configuration.
76         /// </summary>
77         public IReadOnlyDictionary<int, UsbInterface> Interfaces
78         {
79             get
80             {
81                 ThrowIfDisposed();
82                 if (_interfaces == null)
83                 {
84                     _interfaces = new Dictionary<int, UsbInterface>();
85                     int count = Interop.NativeGet<int>(_handle.GetNumInterfaces);
86                     for (int i = 0; i < count; ++i)
87                     {
88                         Interop.UsbInterfaceHandle handle;
89                         _handle.GetInterface(i, out handle);
90                         UsbInterface usbInterface = new UsbInterface(this, handle);
91                         _interfaces.Add(usbInterface.Id, usbInterface);
92                     }
93                 }
94                 return _interfaces;
95             }
96         }
97
98         /// <summary>
99         /// Configuration string.
100         /// </summary>
101         public string ConfigurationString
102         {
103             get
104             {
105                 ThrowIfDisposed();
106                 return Interop.DescriptorString(_handle.GetConfigStr);
107             }
108         }
109
110         /// <summary>
111         /// Set this configuration as active configuration for the device.
112         /// </summary>
113         /// <exception cref="InvalidOperationException">
114         /// Throws exception if device is disconnected or not opened for operation or busy as its interfaces are currently claimed.
115         /// </exception>
116         public void SetAsActive()
117         {
118             ThrowIfDisposed();
119             _handle.SetAsActive();
120         }
121
122         internal void ThrowIfDisposed()
123         {
124             if (disposedValue) throw new ObjectDisposedException("Configuration is already disposed");
125             _parent.ThrowIfDisposed();
126         }
127
128         #region IDisposable Support
129         private bool disposedValue = false;
130
131         /// <summary>
132         /// Releases all resources used by the ConnectionProfile.
133         /// It should be called after finished using of the object.</summary>
134         protected virtual void Dispose(bool disposing)
135         {
136             if (!disposedValue)
137             {
138                 _handle.Dispose();
139                 disposedValue = true;
140             }
141         }
142
143         /// <summary>
144         /// Finalizes an instance of the UsbConfiguration class.
145         /// </summary>
146         ~UsbConfiguration()
147         {
148             Dispose(false);
149         }
150
151         /// <summary>
152         /// Releases all resources used by the ConnectionProfile.
153         /// It should be called after finished using of the object.</summary>
154         public void Dispose()
155         {
156             Dispose(true);
157             GC.SuppressFinalize(this);
158         }
159         #endregion
160     }
161 }