Merge "[NUI]Remove some unused APIs."
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / Usb / UsbInterface.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
20 namespace Tizen.System.Usb
21 {
22     /// <summary>
23     /// Class to manage USB Interfaces.
24     /// </summary>
25     /// <since_tizen> 4 </since_tizen>
26     public class UsbInterface
27     {
28         internal readonly Interop.UsbInterfaceHandle _handle;
29         private readonly UsbConfiguration _parent;
30         private Dictionary<int, UsbEndpoint> _endpoints;
31         private bool _isClaimed;
32
33         internal UsbInterface(UsbConfiguration parent, Interop.UsbInterfaceHandle handle)
34         {
35             _parent = parent;
36             _handle = handle;
37         }
38
39         /// <summary>
40         /// Gets number of given interface.
41         /// </summary>
42         /// <since_tizen> 4 </since_tizen>
43         public int Id
44         {
45             get
46             {
47                 ThrowIfDisposed();
48                 return Interop.NativeGet<int>(_handle.GetNumber);
49             }
50         }
51
52         /// <summary>
53         /// Sets alternative setting. Use index of new alternative setting for given interface.
54         /// </summary>
55         /// <since_tizen> 4 </since_tizen>
56         public int AlternateSetting
57         {
58             set
59             {
60                 ThrowIfDisposed();
61                 Interop.NativeSet(_handle.SetAltsetting, value);
62             }
63         }
64
65         /// <summary>
66         /// Dictionary mapping endpoint Ids to endpoint instances for given interface.
67         /// </summary>
68         /// <since_tizen> 4 </since_tizen>
69         public IReadOnlyDictionary<int, UsbEndpoint> Endpoints
70         {
71             get
72             {
73                 ThrowIfDisposed();
74                 if (_endpoints == null)
75                 {
76                     _endpoints = new Dictionary<int, UsbEndpoint>();
77                     int count = Interop.NativeGet<int>(_handle.GetNumEndpoints);
78                     for (int i = 0; i < count; ++i)
79                     {
80                         IntPtr handle;
81                         _handle.GetEndpoint(i, out handle);
82                         UsbEndpoint endpoint = UsbEndpoint.EndpointFactory(this, new Interop.UsbEndpointHandle(handle));
83                         _endpoints.Add(endpoint.Id, endpoint);
84                     }
85                 }
86                 return _endpoints;
87             }
88         }
89
90
91         /// <summary>
92         /// Gets string describing an interface.
93         /// </summary>
94         /// <exception cref="InvalidOperationException"> Throws exception if device is disconnected or not opened for operation. </exception>
95         /// <since_tizen> 4 </since_tizen>
96         public string InterfaceString
97         {
98             get
99         {
100             ThrowIfDisposed();
101                 _parent.ThrowIfDeviceNotOpened();
102             return Interop.DescriptorString(_handle.GetStr);
103         }
104         }
105
106         /// <summary>
107         /// Claims interface on a device. Interface must be claimed first to perform I/O operations.
108         /// </summary>
109         /// <param name="force">Set to true to auto detach kernel driver, false otherwise.</param>
110         /// <exception cref="InvalidOperationException">
111         /// Throws exception if device is disconnected or not opened for operation or another program or driver has claimed the interface.
112         /// </exception>
113         /// <since_tizen> 4 </since_tizen>
114         public void Claim(bool force)
115         {
116             ThrowIfDisposed();
117             _parent.ThrowIfDeviceNotOpened();
118
119             if (_isClaimed == true) return;
120             var err = _handle.ClaimInterface(force);
121             if (err.IsFailed() && err != Interop.ErrorCode.ResourceBusy)
122             {
123                 err.ThrowIfFailed("Failed to claim interface");
124             }
125             _isClaimed = true;
126         }
127
128
129         /// <summary>
130         /// Releases previously claimed interface.
131         /// </summary>
132         /// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation.</exception>
133         /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
134         /// <since_tizen> 4 </since_tizen>
135         public void Release()
136         {
137             ThrowIfDisposed();
138             _parent.ThrowIfDeviceNotOpened();
139
140             if (_isClaimed == false) return;
141             _handle.ReleaseInterface().ThrowIfFailed("Failed to release interface");
142             _isClaimed = false;
143         }
144
145         internal void ThrowIfDisposed()
146         {
147             _parent.ThrowIfDisposed();
148         }
149     }
150 }