[ElmSharp] Fixed the crash issue of Calendar
[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> 5 </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         /// <feature>http://tizen.org/feature/usb.host</feature>
42         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
43         /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
44         /// <since_tizen> 5 </since_tizen>
45         public int BusId {
46             get
47             {
48                 ThrowIfDisposed();
49                 return Interop.NativeGet<int>(_handle.GetBusNumber);
50             }
51         }
52
53         /// <summary>
54         /// Address of device on the bus.
55         /// </summary>
56         /// <feature>http://tizen.org/feature/usb.host</feature>
57         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
58         /// <since_tizen> 5 </since_tizen>
59         public int Address
60         {
61             get
62             {
63                 ThrowIfDisposed();
64                 return Interop.NativeGet<int>(_handle.GetAddress);
65             }
66         }
67
68         /// <summary>
69         /// List of available port numbers from a device.
70         /// </summary>
71         /// <feature>http://tizen.org/feature/usb.host</feature>
72         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
73         /// <since_tizen> 5 </since_tizen>
74         public IEnumerable<int> Ports
75         {
76             get
77             {
78                 ThrowIfDisposed();
79                 return _handle.Ports();
80             }
81         }
82
83         /// <summary>
84         /// Checks if device is opened.
85         /// </summary>
86         /// <feature>http://tizen.org/feature/usb.host</feature>
87         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
88         /// <since_tizen> 5 </since_tizen>
89         public bool IsOpened
90         {
91             get
92             {
93                 ThrowIfDisposed();
94                 return Interop.NativeGet<bool>(_handle.IsOpened);
95             }
96         }
97
98         /// <summary>
99         /// Control endpoint (endpoint 0).
100         /// </summary>
101         /// <feature>http://tizen.org/feature/usb.host</feature>
102         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
103         /// <since_tizen> 5 </since_tizen>
104         public UsbControlEndpoint ControlEndpoint
105         {
106             get
107             {
108                 ThrowIfDisposed();
109                 return new UsbControlEndpoint(this);
110             }
111         }
112
113         /// <summary>
114         /// Active configuration for the device.
115         /// </summary>
116         /// <feature>http://tizen.org/feature/usb.host</feature>
117         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
118         /// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation. </exception>
119         /// <since_tizen> 5 </since_tizen>
120         public UsbConfiguration ActiveConfiguration
121         {
122             get
123             {
124                 ThrowIfDisposed();
125                 ThrowIfDeviceNotOpened();
126                 IntPtr handle = Interop.NativeGet<IntPtr>(_handle.GetActiveConfig);
127                 Interop.UsbConfigHandle configHandle = new Interop.UsbConfigHandle(handle);
128                 return new UsbConfiguration(this, configHandle);
129             }
130         }
131
132         /// <summary>
133         /// Dictionary mapping configuration Ids to configuration instances for this device.
134         /// </summary>
135         /// <feature>http://tizen.org/feature/usb.host</feature>
136         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
137         /// <since_tizen> 5 </since_tizen>
138         public IReadOnlyDictionary<int, UsbConfiguration> Configurations
139         {
140             get
141             {
142                 ThrowIfDisposed();
143                 var configurations = new Dictionary<int, UsbConfiguration>();
144                 int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
145                 for (int i = 0; i < count; ++i)
146                 {
147                     IntPtr configHandle;
148                     _handle.GetConfig(i, out configHandle);
149                     configurations.Add(i, new UsbConfiguration(this, new Interop.UsbConfigHandle(configHandle)));
150                 }
151                 return configurations;
152             }
153         }
154
155         /// <summary>
156         /// Device information such as version, class, subclass etc.
157         /// </summary>
158         /// <feature>http://tizen.org/feature/usb.host</feature>
159         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
160         /// <since_tizen> 5 </since_tizen>
161         public UsbDeviceInformation DeviceInformation
162         {
163             get
164             {
165                 ThrowIfDisposed();
166                 return new UsbDeviceInformation(this);
167             }
168         }
169
170         /// <summary>
171         /// String associated with device.
172         /// </summary>
173         /// <feature>http://tizen.org/feature/usb.host</feature>
174         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
175         /// <exception cref="InvalidOperationException"> Throws exception if device is disconnected or not opened for operation. </exception>
176         /// <since_tizen> 5 </since_tizen>
177         public UsbDeviceStrings Strings
178         {
179             get
180             {
181                 ThrowIfDisposed();
182                 ThrowIfDeviceNotOpened();
183                 return new UsbDeviceStrings(this, "us-ascii");
184             }
185         }
186
187         /// <summary>
188         /// Opens device, which allows performing operations on it.
189         /// </summary>
190         /// <feature>http://tizen.org/feature/usb.host</feature>
191         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
192         /// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
193         /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
194         /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
195         /// <since_tizen> 5 </since_tizen>
196         public void Open()
197         {
198             ThrowIfDisposed();
199             _handle.Open().ThrowIfFailed("Failed to open device for use");
200         }
201
202         /// <summary>
203         /// Closes device for operations.
204         /// </summary>
205         /// <feature>http://tizen.org/feature/usb.host</feature>
206         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
207         /// <since_tizen> 5 </since_tizen>
208         public void Close()
209         {
210             ThrowIfDisposed();
211             if (IsOpened == false) return;
212
213             _handle.CloseHandle().ThrowIfFailed("Failed to close device for use");
214         }
215
216         internal void ThrowIfDisposed()
217         {
218             if (disposedValue) throw new ObjectDisposedException("USB Device is already disposed");
219             _parent.ThrowIfDisposed();
220         }
221
222         internal void ThrowIfDeviceNotOpened()
223         {
224             if (IsOpened == false) throw new InvalidOperationException("USB Device is should be in open state for this operation");
225         }
226
227         #region IDisposable Support
228         private bool disposedValue = false;
229
230         /// <summary>
231         /// Releases all resources used by the ConnectionProfile.
232         /// It should be called after finished using of the object.</summary>
233         /// <since_tizen> 5 </since_tizen>
234         protected virtual void Dispose(bool disposing)
235         {
236             if (!disposedValue)
237             {
238                 _handle.Dispose();
239                 disposedValue = true;
240             }
241         }
242
243         /// <summary>
244         /// Finalizes an instance of the UsbDevice class.
245         /// </summary>
246         ~UsbDevice()
247         {
248             Dispose(false);
249         }
250
251         /// <summary>
252         /// Releases all resources used by the ConnectionProfile.
253         /// It should be called after finished using of the object.</summary>
254         /// <since_tizen> 5 </since_tizen>
255         public void Dispose()
256         {
257             Dispose(true);
258             GC.SuppressFinalize(this);
259         }
260         #endregion
261     }
262 }