41167528d6ab3043947136bd36bee2320c60b9b8
[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     public class UsbDevice : IDisposable
27     {
28         internal readonly Interop.HostDeviceHandle _handle;
29         private readonly UsbManager _parent;
30         private Dictionary<int, UsbConfiguration> _configurations = new Dictionary<int, UsbConfiguration>();
31
32         internal UsbDevice(UsbManager parent, Interop.HostDeviceHandle handle)
33         {
34             _parent = parent;
35             _handle = handle;
36
37             int count = Interop.NativeGet<int>(_handle.GetNumConfigurations);
38             for (int i = 0; i < count; ++i)
39             {
40                 Interop.UsbConfigHandle configHandle;
41                 _handle.GetConfig(i, out configHandle);
42                 _configurations.Add(i, new UsbConfiguration(this, configHandle));
43             }
44         }
45
46         /// <summary>
47         /// Number of the bus, this device is connected to.
48         /// </summary>
49         /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
50         public int BusId {
51             get
52             {
53                 ThrowIfDisposed();
54                 return Interop.NativeGet<int>(_handle.GetBusNumber);
55             }
56         }
57
58         /// <summary>
59         /// Address of device on the bus.
60         /// </summary>
61         public int Address
62         {
63             get
64             {
65                 ThrowIfDisposed();
66                 return Interop.NativeGet<int>(_handle.GetAddress);
67             }
68         }
69
70         /// <summary>
71         /// List of available port numbers from a device.
72         /// </summary>
73         public IEnumerable<int> Ports
74         {
75             get
76             {
77                 ThrowIfDisposed();
78                 return _handle.Ports();
79             }
80         }
81
82         /// <summary>
83         /// Checks if device is opened.
84         /// </summary>
85         public bool IsOpened
86         {
87             get
88             {
89                 ThrowIfDisposed();
90                 return Interop.NativeGet<bool>(_handle.IsOpened);
91             }
92         }
93
94         /// <summary>
95         /// Control endpoint (endpoint 0).
96         /// </summary>
97         public UsbControlEndpoint ControlEndpoint
98         {
99             get
100             {
101                 ThrowIfDisposed();
102                 return new UsbControlEndpoint(this);
103             }
104         }
105
106         /// <summary>
107         /// Active configuration for the device.
108         /// </summary>
109         /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
110         public UsbConfiguration ActiveConfiguration
111         {
112             get
113             {
114                 ThrowIfDisposed();
115                 Interop.UsbConfigHandle configHandle = Interop.NativeGet<Interop.UsbConfigHandle>(_handle.GetActiveConfig);
116                 return _configurations.Values.Where(config => config._handle == configHandle).First();
117             }
118         }
119
120         /// <summary>
121         /// Dictionary mapping configuration Ids to configuration instances for this device.
122         /// </summary>
123         public IReadOnlyDictionary<int, UsbConfiguration> Configurations
124         {
125             get
126             {
127                 ThrowIfDisposed();
128                 return _configurations;
129             }
130         }
131
132         /// <summary>
133         /// Device information such as version, class, subclass etc.
134         /// </summary>
135         public UsbDeviceInformation DeviceInformation
136         {
137             get
138             {
139                 ThrowIfDisposed();
140                 return new UsbDeviceInformation(this);
141             }
142         }
143
144         /// <summary>
145         /// String associated with device.
146         /// </summary>
147         public UsbDeviceStrings Strings
148         {
149             get
150             {
151                 ThrowIfDisposed();
152                 return new UsbDeviceStrings(this, "us-ascii");
153             }
154         }
155
156         /// <summary>
157         /// Opens device, which allows performing operations on it.
158         /// </summary>
159         /// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
160         /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
161         /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
162         public void Open()
163         {
164             ThrowIfDisposed();
165             _handle.Open().ThrowIfFailed("Failed to open device for use");
166         }
167
168         /// <summary>
169         /// Closes device for operations.
170         /// </summary>
171         /// <exception cref="InvalidOperationException">Throws exception if device is not opened for operation.</exception>
172         public void Close()
173         {
174             ThrowIfDisposed();
175             if (IsOpened == false) throw new InvalidOperationException("Device must be opened for operation first");
176
177             _handle.CloseHandle().ThrowIfFailed("Failed to close device for use");
178         }
179
180         internal void ThrowIfDisposed()
181         {
182             if (disposedValue) throw new ObjectDisposedException("USB Device is already disposed");
183             _parent.ThrowIfDisposed();
184         }
185
186         #region IDisposable Support
187         private bool disposedValue = false;
188
189         protected virtual void Dispose(bool disposing)
190         {
191             if (!disposedValue)
192             {
193                 if (IsOpened)
194                 {
195                     Close();
196                 }
197                 foreach(var config in _configurations.Values) {
198                     config.Dispose();
199                 }
200                 _configurations.Clear();
201                 _handle.Dispose();
202                 disposedValue = true;
203             }
204         }
205
206          ~UsbDevice()
207         {
208             Dispose(false);
209         }
210
211         public void Dispose()
212         {
213             Dispose(true);
214             GC.SuppressFinalize(this);
215         }
216         #endregion
217     }
218 }