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