From: Dinesh Dwivedi Date: Wed, 21 Jun 2017 11:11:15 +0000 (+0530) Subject: [USB] Add UsbInterface class and related changes in UsbEndpoint X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1357184e8a7f5c80a1b0b58e31e680a72e34333b;p=platform%2Fcore%2Fcsapi%2Fusb.git [USB] Add UsbInterface class and related changes in UsbEndpoint Change-Id: I47dc10ba903e16e61c7e235ec024e8f8e3e0f2fa Signed-off-by: Dinesh Dwivedi --- diff --git a/Tizen.System.Usb/Interop/Interop.Interface.cs b/Tizen.System.Usb/Interop/Interop.Interface.cs new file mode 100644 index 0000000..5cae2f3 --- /dev/null +++ b/Tizen.System.Usb/Interop/Interop.Interface.cs @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + + [DllImport(Libraries.Usb, EntryPoint = "usb_host_claim_interface")] + internal static extern ErrorCode ClaimInterface(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, bool force); + + [DllImport(Libraries.Usb, EntryPoint = "usb_host_release_interface")] + internal static extern ErrorCode ReleaseInterface(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface); + + [DllImport(Libraries.Usb, EntryPoint = "usb_host_interface_get_number")] + internal static extern ErrorCode GetNumber(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, out int number); + + [DllImport(Libraries.Usb, EntryPoint = "usb_host_interface_get_num_endpoints")] + internal static extern ErrorCode GetNumEndpoints(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, out int numEndpoints); + + [DllImport(Libraries.Usb, EntryPoint = "usb_host_interface_get_endpoint")] + internal static extern ErrorCode GetEndpoint(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, int epIndex, out UsbEndpointHandle /* usb_host_endpoint_h */ ep); + + [DllImport(Libraries.Usb, EntryPoint = "usb_host_interface_set_altsetting")] + internal static extern ErrorCode SetAltsetting(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, int altsetting); + + [DllImport(Libraries.Usb, EntryPoint = "usb_host_interface_get_str")] + internal static extern ErrorCode GetStr(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, ref int length, byte[] data); + + + internal class UsbInterfaceHandle + { + private IntPtr _handle; + public UsbInterfaceHandle(IntPtr handle) { _handle = handle; } + } +} \ No newline at end of file diff --git a/Tizen.System.Usb/Usb/EndpointDirection.cs b/Tizen.System.Usb/Usb/EndpointDirection.cs index 234054c..3405fdd 100644 --- a/Tizen.System.Usb/Usb/EndpointDirection.cs +++ b/Tizen.System.Usb/Usb/EndpointDirection.cs @@ -17,7 +17,7 @@ namespace Tizen.System.Usb { /// - /// Enumeration of transfer direction for Endpoints + /// Enumeration of transfer direction for Endpoints. /// public enum EndpointDirection { diff --git a/Tizen.System.Usb/Usb/SynchronizationType.cs b/Tizen.System.Usb/Usb/SynchronizationType.cs index 4dcda11..b523087 100644 --- a/Tizen.System.Usb/Usb/SynchronizationType.cs +++ b/Tizen.System.Usb/Usb/SynchronizationType.cs @@ -17,7 +17,7 @@ namespace Tizen.System.Usb { /// - /// Enumeration of isochronous endpoint's synchronization type + /// Enumeration of isochronous endpoint's synchronization type. /// public enum SynchronizationType { diff --git a/Tizen.System.Usb/Usb/UsageType.cs b/Tizen.System.Usb/Usb/UsageType.cs index e45ff55..eb4af52 100644 --- a/Tizen.System.Usb/Usb/UsageType.cs +++ b/Tizen.System.Usb/Usb/UsageType.cs @@ -17,7 +17,7 @@ namespace Tizen.System.Usb { /// - /// Enumeration of an endpoint's usage type + /// Enumeration of an endpoint's usage type. /// public enum UsageType { diff --git a/Tizen.System.Usb/Usb/UsbBulkEndpoint.cs b/Tizen.System.Usb/Usb/UsbBulkEndpoint.cs index f2b8a42..a9b0bf8 100644 --- a/Tizen.System.Usb/Usb/UsbBulkEndpoint.cs +++ b/Tizen.System.Usb/Usb/UsbBulkEndpoint.cs @@ -19,29 +19,29 @@ using System; namespace Tizen.System.Usb { /// - /// USB Bulk Endpoint class + /// USB Bulk Endpoint class. /// public class UsbBulkEndpoint : UsbEndpoint { - internal UsbBulkEndpoint(Interop.UsbEndpointHandle handle) : base(handle) + internal UsbBulkEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle) : base(parent, handle) { } /// - /// Performs a USB transfer on given endpoint. Direction of transfer is determined by the endpoint + /// Performs a USB transfer on given endpoint. Direction of transfer is determined by the endpoint. /// - /// Suitably-sized data buffer for either input or output (depending on endpoint) + /// Suitably-sized data buffer for either input or output (depending on endpoint). /// /// For writes, the number of bytes from data to be sent, for reads the maximum number of bytes to receive - /// into the data buffer + /// into the data buffer. /// /// /// Timeout (in milliseconds) that this function should wait before giving up due to no response being - /// received(for an unlimited timeout 0 value should be used) + /// received(for an unlimited timeout 0 value should be used). /// - /// transferred number of bytes actually transferred - /// Throws excetion if device is disconnected or not opened for operation - /// Throws excetion if transfer timed-out + /// Number of bytes actually transferred. + /// Throws exception if device is disconnected or not opened for operation. + /// Throws exception if transfer timed-out. public int Transfer(byte[] buffer, int length, uint timeout) { return TransferImpl(buffer, length, timeout); diff --git a/Tizen.System.Usb/Usb/UsbEndpoint.cs b/Tizen.System.Usb/Usb/UsbEndpoint.cs index 914bf2d..e5dfec8 100644 --- a/Tizen.System.Usb/Usb/UsbEndpoint.cs +++ b/Tizen.System.Usb/Usb/UsbEndpoint.cs @@ -19,67 +19,78 @@ using System; namespace Tizen.System.Usb { /// - /// USB Endpoint class + /// USB Endpoint class. /// public class UsbEndpoint { internal readonly Interop.UsbEndpointHandle _handle; + protected readonly UsbInterface _parent; - internal UsbEndpoint(Interop.UsbEndpointHandle handle) + internal UsbEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle) { + _parent = parent; _handle = handle; } /// - /// Gets number of this endpoint + /// Gets number of this endpoint. /// public int Id { get { + ThrowIfDisposed(); return Interop.NativeGet(_handle.GetNumber); } } /// - /// Gets direction of this endpoint + /// Gets direction of this endpoint. /// public EndpointDirection Direction { get { + ThrowIfDisposed(); return (EndpointDirection)Interop.NativeGet(_handle.GetDirection); } } /// - /// Gets max packet size of given endpoint + /// Gets max packet size of given endpoint. /// public int MaxPacketSize { get { + ThrowIfDisposed(); return Interop.NativeGet(_handle.GetMaxPacketSize); } } + internal void ThrowIfDisposed() + { + _parent.ThrowIfDisposed(); + } + protected int TransferImpl(byte[] buffer, int length, uint timeout) { + ThrowIfDisposed(); int transferred; _handle.Transfer(buffer, length, out transferred, timeout).ThrowIfFailed("Transfer failed"); return transferred; } - internal static UsbEndpoint EndpointFactory(Interop.UsbEndpointHandle handle) + internal static UsbEndpoint EndpointFactory(UsbInterface parent, Interop.UsbEndpointHandle handle) { Interop.TransferType transferType; handle.GetTransferType(out transferType).ThrowIfFailed("Failed to get transfer type from endpoint"); switch(transferType) { - case Interop.TransferType.Bulk: return new UsbBulkEndpoint(handle); - case Interop.TransferType.Interrupt: return new UsbInterruptEndpoint(handle); - case Interop.TransferType.Isochronous: return new UsbIsochronousEndpoint(handle); - default: return new UsbEndpoint(handle); + case Interop.TransferType.Bulk: return new UsbBulkEndpoint(parent, handle); + case Interop.TransferType.Interrupt: return new UsbInterruptEndpoint(parent, handle); + case Interop.TransferType.Isochronous: return new UsbIsochronousEndpoint(parent, handle); + default: return new UsbEndpoint(parent, handle); } } } diff --git a/Tizen.System.Usb/Usb/UsbInterface.cs b/Tizen.System.Usb/Usb/UsbInterface.cs new file mode 100644 index 0000000..961b04e --- /dev/null +++ b/Tizen.System.Usb/Usb/UsbInterface.cs @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System.Collections.Generic; + +namespace Tizen.System.Usb +{ + /// + /// Class to manage USB Interfaces. + /// + public class UsbInterface + { + internal readonly Interop.UsbInterfaceHandle _handle; + private Dictionary _endpoints; + private bool _isClaimed; + + internal UsbInterface(Interop.UsbInterfaceHandle handle) + { + _handle = handle; + } + + /// + /// Gets number of given interface. + /// + public int Id + { + get + { + ThrowIfDisposed(); + return Interop.NativeGet(_handle.GetNumber); + } + } + + /// + /// Sets alternative setting. Use index of new alternative setting for given interface. + /// + public int AlternateSetting + { + set + { + ThrowIfDisposed(); + Interop.NativeSet(_handle.SetAltsetting, value); + } + } + + /// + /// Dictionary mapping endpoint Ids to endpoint instances for given interface. + /// + public IReadOnlyDictionary Endpoints + { + get + { + ThrowIfDisposed(); + if (_endpoints == null) + { + _endpoints = new Dictionary(); + int count = Interop.NativeGet(_handle.GetNumEndpoints); + for (int i = 0; i < count; ++i) + { + Interop.UsbEndpointHandle handle; + _handle.GetEndpoint(i, out handle); + UsbEndpoint endpoint = UsbEndpoint.EndpointFactory(this, handle); + _endpoints.Add(endpoint.Id, endpoint); + } + } + return _endpoints; + } + } + + + /// + /// Gets string describing an interface. + /// + /// + public string InterfaceString() + { + ThrowIfDisposed(); + return Interop.DescriptorString(_handle.GetStr); + } + + /// + /// Claims interface on a device. Interface must be claimed first to perform I/O operations. + /// + /// Set to true to auto detach kernel driver, false otherwise. + /// + /// Throws exception if device is disconnected or not opened for operation or another program or driver has claimed the interface. + /// + public void Claim(bool force) + { + ThrowIfDisposed(); + if (_isClaimed == true) return; + _handle.ClaimInterface(force).ThrowIfFailed("Failed to claim interface"); + _isClaimed = true; + } + + + /// + /// Releases previously claimed interface. + /// + /// Throws exception if device is disconnected or not opened for operation. + /// Throws exception if user has insufficient permission on device. + public void Release() + { + ThrowIfDisposed(); + if (_isClaimed == false) return; + _handle.ReleaseInterface().ThrowIfFailed("Failed to release interface"); + _isClaimed = false; + } + + internal void ThrowIfDisposed() + { + } + } +} diff --git a/Tizen.System.Usb/Usb/UsbInterruptEndpoint.cs b/Tizen.System.Usb/Usb/UsbInterruptEndpoint.cs index eb4e5c6..29b48d5 100644 --- a/Tizen.System.Usb/Usb/UsbInterruptEndpoint.cs +++ b/Tizen.System.Usb/Usb/UsbInterruptEndpoint.cs @@ -17,21 +17,22 @@ namespace Tizen.System.Usb { /// - /// USB Interrupt Endpoint class + /// USB Interrupt Endpoint class. /// public class UsbInterruptEndpoint : UsbEndpoint { - internal UsbInterruptEndpoint(Interop.UsbEndpointHandle handle) : base(handle) + internal UsbInterruptEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle) : base(parent, handle) { } /// - /// Gets interval for polling endpoint for data transfers, in frame counts (refer to USB protocol specification) + /// Gets interval for polling endpoint for data transfers, in frame counts (refer to USB protocol specification). /// public int PollingInterval { get { + ThrowIfDisposed(); return Interop.NativeGet(_handle.GetInterval); } } @@ -39,18 +40,18 @@ namespace Tizen.System.Usb /// /// Performs a USB transfer on given endpoint. Direction of transfer is determined by the endpoint. /// - /// Suitably-sized data buffer for either input or output (depending on endpoint) + /// Suitably-sized data buffer for either input or output (depending on endpoint). /// /// For writes, the number of bytes from data to be sent, for reads the maximum number of bytes to receive - /// into the data buffer + /// into the data buffer. /// /// /// Timeout (in milliseconds) that this function should wait before giving up due to no response being - /// received(for an unlimited timeout 0 value should be used) + /// received(for an unlimited timeout 0 value should be used). /// - /// transferred number of bytes actually transferred - /// Throws excetion if device is disconnected or not opened for operation - /// Throws excetion if transfer timed-out + /// Number of bytes actually transferred. + /// Throws exception if device is disconnected or not opened for operation. + /// Throws exception if transfer timed-out. public int Transfer(byte[] buffer, int length, uint timeout) { return TransferImpl(buffer, length, timeout); diff --git a/Tizen.System.Usb/Usb/UsbIsochronousEndpoint.cs b/Tizen.System.Usb/Usb/UsbIsochronousEndpoint.cs index 680e184..1544c43 100644 --- a/Tizen.System.Usb/Usb/UsbIsochronousEndpoint.cs +++ b/Tizen.System.Usb/Usb/UsbIsochronousEndpoint.cs @@ -17,32 +17,34 @@ namespace Tizen.System.Usb { /// - /// USB Isochronous Endpoint class + /// USB Isochronous Endpoint class. /// public class UsbIsochronousEndpoint : UsbEndpoint { - internal UsbIsochronousEndpoint(Interop.UsbEndpointHandle handle) : base(handle) + internal UsbIsochronousEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle) : base(parent, handle) { } /// - /// Gets synchronization type of this endpoint + /// Gets synchronization type of this endpoint. /// public SynchronizationType SynchronizationType { get { + ThrowIfDisposed(); return (SynchronizationType)Interop.NativeGet(_handle.GetSynchType); } } /// - /// Gets usage type of this endpoint + /// Gets usage type of this endpoint. /// public UsageType UsageType { get { + ThrowIfDisposed(); return (UsageType)Interop.NativeGet(_handle.GetUsageType); } }