2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 using System.Collections.Generic;
20 namespace Tizen.System.Usb
23 /// Class to manage USB Interfaces.
25 public class UsbInterface
27 internal readonly Interop.UsbInterfaceHandle _handle;
28 private readonly UsbConfiguration _parent;
29 private Dictionary<int, UsbEndpoint> _endpoints;
30 private bool _isClaimed;
32 internal UsbInterface(UsbConfiguration parent, Interop.UsbInterfaceHandle handle)
39 /// Gets number of given interface.
46 return Interop.NativeGet<int>(_handle.GetNumber);
51 /// Sets alternative setting. Use index of new alternative setting for given interface.
53 public int AlternateSetting
58 Interop.NativeSet(_handle.SetAltsetting, value);
63 /// Dictionary mapping endpoint Ids to endpoint instances for given interface.
65 public IReadOnlyDictionary<int, UsbEndpoint> Endpoints
70 if (_endpoints == null)
72 _endpoints = new Dictionary<int, UsbEndpoint>();
73 int count = Interop.NativeGet<int>(_handle.GetNumEndpoints);
74 for (int i = 0; i < count; ++i)
76 Interop.UsbEndpointHandle handle;
77 _handle.GetEndpoint(i, out handle);
78 UsbEndpoint endpoint = UsbEndpoint.EndpointFactory(this, handle);
79 _endpoints.Add(endpoint.Id, endpoint);
88 /// Gets string describing an interface.
90 /// <returns></returns>
91 public string InterfaceString()
94 return Interop.DescriptorString(_handle.GetStr);
98 /// Claims interface on a device. Interface must be claimed first to perform I/O operations.
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.
104 public void Claim(bool force)
107 if (_isClaimed == true) return;
108 _handle.ClaimInterface(force).ThrowIfFailed("Failed to claim interface");
114 /// Releases previously claimed interface.
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()
121 if (_isClaimed == false) return;
122 _handle.ReleaseInterface().ThrowIfFailed("Failed to release interface");
126 internal void ThrowIfDisposed()
128 _parent.ThrowIfDisposed();