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