[USB] Add UsbControlEndpoint class 53/133853/4
authorDinesh Dwivedi <dinesh.d@samsung.com>
Fri, 23 Jun 2017 05:49:52 +0000 (11:19 +0530)
committerDinesh Dwivedi <dinesh.d@samsung.com>
Tue, 11 Jul 2017 15:36:46 +0000 (21:06 +0530)
Change-Id: Id99b25f06dd3297f89a0fb21be133bfd6591f772
Signed-off-by: Dinesh Dwivedi <dinesh.d@samsung.com>
Tizen.System.Usb/Usb/UsbControlEndpoint.cs [new file with mode: 0644]
Tizen.System.Usb/Usb/UsbDevice.cs
Tizen.System.Usb/Usb/UsbEndpoint.cs

diff --git a/Tizen.System.Usb/Usb/UsbControlEndpoint.cs b/Tizen.System.Usb/Usb/UsbControlEndpoint.cs
new file mode 100644 (file)
index 0000000..2e1e7d4
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * 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;
+
+namespace Tizen.System.Usb
+{
+    /// <summary>
+    /// USB Control Endpoint class.
+    /// </summary>
+    public class UsbControlEndpoint : UsbEndpoint
+    {
+        private readonly UsbDevice _device;
+
+        internal UsbControlEndpoint(UsbDevice device) : base(null, null)
+        {
+            _device = device;
+        }
+
+        /// <summary>
+        /// Gets number of this endpoint.
+        /// </summary>
+        public new int Id
+        {
+            get
+            {
+                return 0;
+            }
+        }
+
+        /// <summary>
+        /// Gets direction of this endpoint.
+        /// </summary>
+        public new EndpointDirection Direction
+        {
+            get
+            {
+                return EndpointDirection.InOut;
+            }
+        }
+
+        /// <summary>
+        /// Gets max packet size of given endpoint.
+        /// </summary>
+        public new int MaxPacketSize
+        {
+            get
+            {
+                _device.ThrowIfDisposed();
+                return Interop.NativeGet<int>(_device._handle.GetMaxPacketSize0);
+            }
+        }
+
+        /// <summary>
+        /// Performs a control transaction on endpoint zero for this device.
+        /// </summary>
+        /// <param name="requestType">bmRequestType type field for the setup packet.</param>
+        /// <param name="request">bRequest field for the setup packet.</param>
+        /// <param name="value">wValue field for the setup packet.</param>
+        /// <param name="index">wIndex field for the setup packet.</param>
+        /// <param name="data">Suitably-sized data buffer for either input or output, (depending on direction bits within bmRequestType).</param>
+        /// <param name="length">wLength field for the setup packet. The data buffer should be at least this size.</param>
+        /// <param name="timeout">
+        /// Time (in milliseconds) that this function should wait for, before giving up due to no response being received
+        /// (for an unlimited timeout 0 value should be used).
+        /// </param>
+        /// <returns>Transferred Number of transferred bytes.</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 requestType, byte request, ushort value, ushort index, byte[] data, ushort length, uint timeout)
+        {
+            _device.ThrowIfDisposed();
+            if (_device.IsOpened == false) throw new InvalidOperationException("Device must be opened for operation first");
+
+            int transferred;
+            var err = _device._handle.ControlTransfer(requestType, request, value, index, data, length, timeout, out transferred);
+            err.ThrowIfFailed("Failed during control transfer");
+            return transferred;
+        }
+    }
+}
index a1c6ca3..c625825 100644 (file)
@@ -82,6 +82,18 @@ namespace Tizen.System.Usb
         }
 
         /// <summary>
+        /// Control endpoint (endpoint 0).
+        /// </summary>
+        public UsbControlEndpoint ControlEndpoint
+        {
+            get
+            {
+                ThrowIfDisposed();
+                return new UsbControlEndpoint(this);
+            }
+        }
+
+        /// <summary>
         /// Active configuration for the device.
         /// </summary>
         /// <exception cref="InvalidOperationException">Throws exception if device is disconnected.</exception>
index e5dfec8..022fafd 100644 (file)
@@ -70,7 +70,8 @@ namespace Tizen.System.Usb
 
         internal void ThrowIfDisposed()
         {
-            _parent.ThrowIfDisposed();
+            if (_handle == null) throw new InvalidOperationException("Incompatible endpoint handle");
+            _parent?.ThrowIfDisposed();
         }
 
         protected int TransferImpl(byte[] buffer, int length, uint timeout)