[USB] Add UsbInterface class and related changes in UsbEndpoint 50/133850/4
authorDinesh Dwivedi <dinesh.d@samsung.com>
Wed, 21 Jun 2017 11:11:15 +0000 (16:41 +0530)
committerDinesh Dwivedi <dinesh.d@samsung.com>
Wed, 21 Jun 2017 14:35:30 +0000 (20:05 +0530)
Change-Id: I47dc10ba903e16e61c7e235ec024e8f8e3e0f2fa
Signed-off-by: Dinesh Dwivedi <dinesh.d@samsung.com>
Tizen.System.Usb/Interop/Interop.Interface.cs [new file with mode: 0644]
Tizen.System.Usb/Usb/EndpointDirection.cs
Tizen.System.Usb/Usb/SynchronizationType.cs
Tizen.System.Usb/Usb/UsageType.cs
Tizen.System.Usb/Usb/UsbBulkEndpoint.cs
Tizen.System.Usb/Usb/UsbEndpoint.cs
Tizen.System.Usb/Usb/UsbInterface.cs [new file with mode: 0644]
Tizen.System.Usb/Usb/UsbInterruptEndpoint.cs
Tizen.System.Usb/Usb/UsbIsochronousEndpoint.cs

diff --git a/Tizen.System.Usb/Interop/Interop.Interface.cs b/Tizen.System.Usb/Interop/Interop.Interface.cs
new file mode 100644 (file)
index 0000000..5cae2f3
--- /dev/null
@@ -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
index 234054c..3405fdd 100644 (file)
@@ -17,7 +17,7 @@
 namespace Tizen.System.Usb
 {
     /// <summary>
-    /// Enumeration of transfer direction for Endpoints
+    /// Enumeration of transfer direction for Endpoints.
     /// </summary>
     public enum EndpointDirection
     {
index 4dcda11..b523087 100644 (file)
@@ -17,7 +17,7 @@
 namespace Tizen.System.Usb
 {
     /// <summary>
-    /// Enumeration of isochronous endpoint's synchronization type
+    /// Enumeration of isochronous endpoint's synchronization type.
     /// </summary>
     public enum SynchronizationType
     {
index e45ff55..eb4af52 100644 (file)
@@ -17,7 +17,7 @@
 namespace Tizen.System.Usb
 {
     /// <summary>
-    /// Enumeration of an endpoint's usage type
+    /// Enumeration of an endpoint's usage type.
     /// </summary>
     public enum UsageType
     {
index f2b8a42..a9b0bf8 100644 (file)
@@ -19,29 +19,29 @@ using System;
 namespace Tizen.System.Usb
 {
     /// <summary>
-    /// USB Bulk Endpoint class
+    /// USB Bulk Endpoint class.
     /// </summary>
     public class UsbBulkEndpoint : UsbEndpoint
     {
-        internal UsbBulkEndpoint(Interop.UsbEndpointHandle handle) : base(handle)
+        internal UsbBulkEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle) : base(parent, handle)
         {
         }
 
         /// <summary>
-        /// 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.
         /// </summary>
-        /// <param name="buffer">Suitably-sized data buffer for either input or output (depending on endpoint)</param>
+        /// <param name="buffer">Suitably-sized data buffer for either input or output (depending on endpoint).</param>
         /// <param name="length">
         /// 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.
         /// </param>
         /// <param name="timeout">
         /// 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).
         /// </param>
-        /// <returns>transferred number of bytes actually transferred</returns>
-        /// <exception cref="InvalidOperationException">Throws excetion if device is disconnected or not opened for operation</exception>
-        /// <exception cref="TimeoutException">Throws excetion if transfer timed-out</exception>
+        /// <returns>Number of bytes actually transferred.</returns>
+        /// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation.</exception>
+        /// <exception cref="TimeoutException">Throws exception if transfer timed-out.</exception>
         public int Transfer(byte[] buffer, int length, uint timeout)
         {
             return TransferImpl(buffer, length, timeout);
index 914bf2d..e5dfec8 100644 (file)
@@ -19,67 +19,78 @@ using System;
 namespace Tizen.System.Usb
 {
     /// <summary>
-    /// USB Endpoint class
+    /// USB Endpoint class.
     /// </summary>
     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;
         }
 
         /// <summary>
-        /// Gets number of this endpoint
+        /// Gets number of this endpoint.
         /// </summary>
         public int Id
         {
             get
             {
+                ThrowIfDisposed();
                 return Interop.NativeGet<int>(_handle.GetNumber);
             }
         }
 
         /// <summary>
-        /// Gets direction of this endpoint
+        /// Gets direction of this endpoint.
         /// </summary>
         public EndpointDirection Direction
         {
             get
             {
+                ThrowIfDisposed();
                 return (EndpointDirection)Interop.NativeGet<Interop.EndpointDirection>(_handle.GetDirection);
             }
         }
 
         /// <summary>
-        /// Gets max packet size of given endpoint
+        /// Gets max packet size of given endpoint.
         /// </summary>
         public int MaxPacketSize
         {
             get
             {
+                ThrowIfDisposed();
                 return Interop.NativeGet<int>(_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 (file)
index 0000000..961b04e
--- /dev/null
@@ -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
+{
+    /// <summary>
+    /// Class to manage USB Interfaces.
+    /// </summary>
+    public class UsbInterface
+    {
+        internal readonly Interop.UsbInterfaceHandle _handle;
+        private Dictionary<int, UsbEndpoint> _endpoints;
+        private bool _isClaimed;
+
+        internal UsbInterface(Interop.UsbInterfaceHandle handle)
+        {
+            _handle = handle;
+        }
+
+        /// <summary>
+        /// Gets number of given interface.
+        /// </summary>
+        public int Id
+        {
+            get
+            {
+                ThrowIfDisposed();
+                return Interop.NativeGet<int>(_handle.GetNumber);
+            }
+        }
+
+        /// <summary>
+        /// Sets alternative setting. Use index of new alternative setting for given interface.
+        /// </summary>
+        public int AlternateSetting
+        {
+            set
+            {
+                ThrowIfDisposed();
+                Interop.NativeSet(_handle.SetAltsetting, value);
+            }
+        }
+
+        /// <summary>
+        /// Dictionary mapping endpoint Ids to endpoint instances for given interface.
+        /// </summary>
+        public IReadOnlyDictionary<int, UsbEndpoint> Endpoints
+        {
+            get
+            {
+                ThrowIfDisposed();
+                if (_endpoints == null)
+                {
+                    _endpoints = new Dictionary<int, UsbEndpoint>();
+                    int count = Interop.NativeGet<int>(_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;
+            }
+        }
+
+
+        /// <summary>
+        /// Gets string describing an interface.
+        /// </summary>
+        /// <returns></returns>
+        public string InterfaceString()
+        {
+            ThrowIfDisposed();
+            return Interop.DescriptorString(_handle.GetStr);
+        }
+
+        /// <summary>
+        /// Claims interface on a device. Interface must be claimed first to perform I/O operations.
+        /// </summary>
+        /// <param name="force">Set to true to auto detach kernel driver, false otherwise.</param>
+        /// <exception cref="InvalidOperationException">
+        /// Throws exception if device is disconnected or not opened for operation or another program or driver has claimed the interface.
+        /// </exception>
+        public void Claim(bool force)
+        {
+            ThrowIfDisposed();
+            if (_isClaimed == true) return;
+            _handle.ClaimInterface(force).ThrowIfFailed("Failed to claim interface");
+            _isClaimed = true;
+        }
+
+
+        /// <summary>
+        /// Releases previously claimed interface.
+        /// </summary>
+        /// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation.</exception>
+        /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
+        public void Release()
+        {
+            ThrowIfDisposed();
+            if (_isClaimed == false) return;
+            _handle.ReleaseInterface().ThrowIfFailed("Failed to release interface");
+            _isClaimed = false;
+        }
+
+        internal void ThrowIfDisposed()
+        {
+        }
+    }
+}
index eb4e5c6..29b48d5 100644 (file)
 namespace Tizen.System.Usb
 {
     /// <summary>
-    /// USB Interrupt Endpoint class
+    /// USB Interrupt Endpoint class.
     /// </summary>
     public class UsbInterruptEndpoint : UsbEndpoint
     {
-        internal UsbInterruptEndpoint(Interop.UsbEndpointHandle handle) : base(handle)
+        internal UsbInterruptEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle) : base(parent, handle)
         {
         }
 
         /// <summary>
-        /// 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).
         /// </summary>
         public int PollingInterval
         {
             get
             {
+                ThrowIfDisposed();
                 return Interop.NativeGet<int>(_handle.GetInterval);
             }
         }
@@ -39,18 +40,18 @@ namespace Tizen.System.Usb
         /// <summary>
         /// Performs a USB transfer on given endpoint. Direction of transfer is determined by the endpoint.
         /// </summary>
-        /// <param name="buffer">Suitably-sized data buffer for either input or output (depending on endpoint)</param>
+        /// <param name="buffer">Suitably-sized data buffer for either input or output (depending on endpoint).</param>
         /// <param name="length">
         /// 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.
         /// </param>
         /// <param name="timeout">
         /// 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).
         /// </param>
-        /// <returns>transferred number of bytes actually transferred</returns>
-        /// <exception cref="InvalidOperationException">Throws excetion if device is disconnected or not opened for operation</exception>
-        /// <exception cref="TimeoutException">Throws excetion if transfer timed-out</exception>
+        /// <returns>Number of bytes actually transferred.</returns>
+        /// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation.</exception>
+        /// <exception cref="TimeoutException">Throws exception if transfer timed-out.</exception>
         public int Transfer(byte[] buffer, int length, uint timeout)
         {
             return TransferImpl(buffer, length, timeout);
index 680e184..1544c43 100644 (file)
 namespace Tizen.System.Usb
 {
     /// <summary>
-    /// USB Isochronous Endpoint class
+    /// USB Isochronous Endpoint class.
     /// </summary>
     public class UsbIsochronousEndpoint : UsbEndpoint
     {
-        internal UsbIsochronousEndpoint(Interop.UsbEndpointHandle handle) : base(handle)
+        internal UsbIsochronousEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle) : base(parent, handle)
         {
         }
 
         /// <summary>
-        /// Gets synchronization type of this endpoint
+        /// Gets synchronization type of this endpoint.
         /// </summary>
         public SynchronizationType SynchronizationType
         {
             get
             {
+                ThrowIfDisposed();
                 return (SynchronizationType)Interop.NativeGet<Interop.SynchronizationType>(_handle.GetSynchType);
             }
         }
 
         /// <summary>
-        /// Gets usage type of this endpoint
+        /// Gets usage type of this endpoint.
         /// </summary>
         public UsageType UsageType
         {
             get
             {
+                ThrowIfDisposed();
                 return (UsageType)Interop.NativeGet<Interop.UsageType>(_handle.GetUsageType);
             }
         }