Setting since_tizen 3/4 on Tizen.NET API
[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         /// <returns></returns>
95         /// <since_tizen> 4 </since_tizen>
96         public string InterfaceString()
97         {
98             ThrowIfDisposed();
99             return Interop.DescriptorString(_handle.GetStr);
100         }
101
102         /// <summary>
103         /// Claims interface on a device. Interface must be claimed first to perform I/O operations.
104         /// </summary>
105         /// <param name="force">Set to true to auto detach kernel driver, false otherwise.</param>
106         /// <exception cref="InvalidOperationException">
107         /// Throws exception if device is disconnected or not opened for operation or another program or driver has claimed the interface.
108         /// </exception>
109         /// <since_tizen> 4 </since_tizen>
110         public void Claim(bool force)
111         {
112             ThrowIfDisposed();
113             if (_isClaimed == true) return;
114             _handle.ClaimInterface(force).ThrowIfFailed("Failed to claim interface");
115             _isClaimed = true;
116         }
117
118
119         /// <summary>
120         /// Releases previously claimed interface.
121         /// </summary>
122         /// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation.</exception>
123         /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
124         /// <since_tizen> 4 </since_tizen>
125         public void Release()
126         {
127             ThrowIfDisposed();
128             if (_isClaimed == false) return;
129             _handle.ReleaseInterface().ThrowIfFailed("Failed to release interface");
130             _isClaimed = false;
131         }
132
133         internal void ThrowIfDisposed()
134         {
135             _parent.ThrowIfDisposed();
136         }
137     }
138 }