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.
18 using System.Collections.Generic;
20 namespace Tizen.System.Usb
23 /// Class to manage USB Interfaces.
25 /// <since_tizen> 4 </since_tizen>
26 public class UsbInterface
28 internal readonly Interop.UsbInterfaceHandle _handle;
29 private readonly UsbConfiguration _parent;
30 private Dictionary<int, UsbEndpoint> _endpoints;
31 private bool _isClaimed;
33 internal UsbInterface(UsbConfiguration parent, Interop.UsbInterfaceHandle handle)
40 /// Gets number of given interface.
42 /// <since_tizen> 4 </since_tizen>
48 return Interop.NativeGet<int>(_handle.GetNumber);
53 /// Sets alternative setting. Use index of new alternative setting for given interface.
55 /// <since_tizen> 4 </since_tizen>
56 public int AlternateSetting
61 Interop.NativeSet(_handle.SetAltsetting, value);
66 /// Dictionary mapping endpoint Ids to endpoint instances for given interface.
68 /// <since_tizen> 4 </since_tizen>
69 public IReadOnlyDictionary<int, UsbEndpoint> Endpoints
74 if (_endpoints == null)
76 _endpoints = new Dictionary<int, UsbEndpoint>();
77 int count = Interop.NativeGet<int>(_handle.GetNumEndpoints);
78 for (int i = 0; i < count; ++i)
81 _handle.GetEndpoint(i, out handle);
82 UsbEndpoint endpoint = UsbEndpoint.EndpointFactory(this, new Interop.UsbEndpointHandle(handle));
83 _endpoints.Add(endpoint.Id, endpoint);
92 /// Gets string describing an interface.
94 /// <returns></returns>
95 /// <since_tizen> 4 </since_tizen>
96 public string InterfaceString()
99 return Interop.DescriptorString(_handle.GetStr);
103 /// Claims interface on a device. Interface must be claimed first to perform I/O operations.
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.
109 /// <since_tizen> 4 </since_tizen>
110 public void Claim(bool force)
113 if (_isClaimed == true) return;
114 _handle.ClaimInterface(force).ThrowIfFailed("Failed to claim interface");
120 /// Releases previously claimed interface.
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()
128 if (_isClaimed == false) return;
129 _handle.ReleaseInterface().ThrowIfFailed("Failed to release interface");
133 internal void ThrowIfDisposed()
135 _parent.ThrowIfDisposed();