[USB] Update exception handling code
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / Usb / UsbDevice.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 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20
21 namespace Tizen.System.Usb
22 {
23     /// <summary>
24     /// Class to manage USB host devices. This class contains operations for enumerating, opening and closing devices.
25     /// </summary>
26     /// <since_tizen> 4 </since_tizen>
27     public class UsbDevice : IDisposable
28     {
29         internal readonly Interop.HostDeviceHandle _handle;
30         private readonly UsbManager _parent;
31
32         internal UsbDevice(UsbManager parent, Interop.HostDeviceHandle handle)
33         {
34             _parent = parent;
35             _handle = handle;
36         }
37
38         /// <summary>
39         /// Number of the bus, this device is connected to.
40         /// </summary>
41         /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
42         /// <since_tizen> 4 </since_tizen>
43         public int BusId {
44             get
45             {
46                 ThrowIfDisposed();
47                 return Interop.NativeGet<int>(_handle.GetBusNumber);
48             }
49         }
50
51         /// <summary>
52         /// Address of device on the bus.
53         /// </summary>
54         /// <since_tizen> 4 </since_tizen>
55         public int Address
56         {
57             get
58             {
59                 ThrowIfDisposed();
60                 return Interop.NativeGet<int>(_handle.GetAddress);
61             }
62         }
63
64         /// <summary>
65         /// List of available port numbers from a device.
66         /// </summary>
67         /// <since_tizen> 4 </since_tizen>
68         public IEnumerable<int> Ports
69         {
70             get
71             {
72                 ThrowIfDisposed();
73                 return _handle.Ports();
74             }
75         }
76
77         /// <summary>
78         /// Checks if device is opened.
79         /// </summary>
80         /// <since_tizen> 4 </since_tizen>
81         public bool IsOpened
82         {
83             get
84             {
85                 ThrowIfDisposed();
86                 return Interop.NativeGet<bool>(_handle.IsOpened);
87             }
88         }
89
90         /// <summary>
91         /// Control endpoint (endpoint 0).
92         /// </summary>
93         /// <since_tizen> 4 </since_tizen>
94         public UsbControlEndpoint ControlEndpoint
95         {
96             get
97             {
98                 ThrowIfDisposed();
99                 return new UsbControlEndpoint(this);
100             }
101         }
102
103         /// <summary>
104         /// Active configuration for the device.
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 UsbConfiguration ActiveConfiguration
109         {
110             get
111             {
112                 ThrowIfDisposed();
113                 ThrowIfDeviceNotOpened();
114                 IntPtr handle = Interop.NativeGet<IntPtr>(_handle.GetActiveConfig);
115                 Interop.UsbConfigHandle configHandle = new Interop.UsbConfigHandle(handle);
116                 return new UsbConfiguration(this, configHandle);
117             }
118         }
119
120         /// <summary>
121         /// Dictionary mapping configuration Ids to configuration instances for this device.
122         /// </summary>
123         /// <since_tizen> 4 </since_tizen>
124         public IReadOnlyDictionary<int, UsbConfiguration> Configurations
125         {
126             get
127             {
128                 ThrowIfDisposed();
129                 var configurations = new Dictionary<int, UsbConfiguration>();
130                 int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
131                 for (int i = 0; i < count; ++i)
132                 {
133                     IntPtr configHandle;
134                     _handle.GetConfig(i, out configHandle);
135                     configurations.Add(i, new UsbConfiguration(this, new Interop.UsbConfigHandle(configHandle)));
136                 }
137                 return configurations;
138             }
139         }
140
141         /// <summary>
142         /// Device information such as version, class, subclass etc.
143         /// </summary>
144         /// <since_tizen> 4 </since_tizen>
145         public UsbDeviceInformation DeviceInformation
146         {
147             get
148             {
149                 ThrowIfDisposed();
150                 return new UsbDeviceInformation(this);
151             }
152         }
153
154         /// <summary>
155         /// String associated with device.
156         /// </summary>
157         /// <exception cref="InvalidOperationException"> Throws exception if device is disconnected or not opened for operation. </exception>
158         /// <since_tizen> 4 </since_tizen>
159         public UsbDeviceStrings Strings
160         {
161             get
162             {
163                 ThrowIfDisposed();
164                 ThrowIfDeviceNotOpened();
165                 return new UsbDeviceStrings(this, "us-ascii");
166             }
167         }
168
169         /// <summary>
170         /// Opens device, which allows performing operations on it.
171         /// </summary>
172         /// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
173         /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
174         /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
175         /// <since_tizen> 4 </since_tizen>
176         public void Open()
177         {
178             ThrowIfDisposed();
179             _handle.Open().ThrowIfFailed("Failed to open device for use");
180         }
181
182         /// <summary>
183         /// Closes device for operations.
184         /// </summary>
185         /// <since_tizen> 4 </since_tizen>
186         public void Close()
187         {
188             ThrowIfDisposed();
189             if (IsOpened == false) return;
190
191             _handle.CloseHandle().ThrowIfFailed("Failed to close device for use");
192         }
193
194         internal void ThrowIfDisposed()
195         {
196             if (disposedValue) throw new ObjectDisposedException("USB Device is already disposed");
197             _parent.ThrowIfDisposed();
198         }
199
200         internal void ThrowIfDeviceNotOpened()
201         {
202             if (IsOpened == false) throw new InvalidOperationException("USB Device is should be in open state for this operation");
203         }
204
205         #region IDisposable Support
206         private bool disposedValue = false;
207
208         /// <summary>
209         /// Releases all resources used by the ConnectionProfile.
210         /// It should be called after finished using of the object.</summary>
211         /// <since_tizen> 4 </since_tizen>
212         protected virtual void Dispose(bool disposing)
213         {
214             if (!disposedValue)
215             {
216                 _handle.Dispose();
217                 disposedValue = true;
218             }
219         }
220
221         /// <summary>
222         /// Finalizes an instance of the UsbDevice class.
223         /// </summary>
224         ~UsbDevice()
225         {
226             Dispose(false);
227         }
228
229         /// <summary>
230         /// Releases all resources used by the ConnectionProfile.
231         /// It should be called after finished using of the object.</summary>
232         /// <since_tizen> 4 </since_tizen>
233         public void Dispose()
234         {
235             Dispose(true);
236             GC.SuppressFinalize(this);
237         }
238         #endregion
239     }
240 }