From 017e1b3646db645160f8b209e221acd65eb292f0 Mon Sep 17 00:00:00 2001 From: Dinesh Dwivedi Date: Fri, 23 Jun 2017 11:19:52 +0530 Subject: [PATCH] [USB] Add UsbControlEndpoint class Change-Id: Id99b25f06dd3297f89a0fb21be133bfd6591f772 Signed-off-by: Dinesh Dwivedi --- Tizen.System.Usb/Usb/UsbControlEndpoint.cs | 94 ++++++++++++++++++++++++++++++ Tizen.System.Usb/Usb/UsbDevice.cs | 12 ++++ Tizen.System.Usb/Usb/UsbEndpoint.cs | 3 +- 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 Tizen.System.Usb/Usb/UsbControlEndpoint.cs diff --git a/Tizen.System.Usb/Usb/UsbControlEndpoint.cs b/Tizen.System.Usb/Usb/UsbControlEndpoint.cs new file mode 100644 index 0000000..2e1e7d4 --- /dev/null +++ b/Tizen.System.Usb/Usb/UsbControlEndpoint.cs @@ -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 +{ + /// + /// USB Control Endpoint class. + /// + public class UsbControlEndpoint : UsbEndpoint + { + private readonly UsbDevice _device; + + internal UsbControlEndpoint(UsbDevice device) : base(null, null) + { + _device = device; + } + + /// + /// Gets number of this endpoint. + /// + public new int Id + { + get + { + return 0; + } + } + + /// + /// Gets direction of this endpoint. + /// + public new EndpointDirection Direction + { + get + { + return EndpointDirection.InOut; + } + } + + /// + /// Gets max packet size of given endpoint. + /// + public new int MaxPacketSize + { + get + { + _device.ThrowIfDisposed(); + return Interop.NativeGet(_device._handle.GetMaxPacketSize0); + } + } + + /// + /// Performs a control transaction on endpoint zero for this device. + /// + /// bmRequestType type field for the setup packet. + /// bRequest field for the setup packet. + /// wValue field for the setup packet. + /// wIndex field for the setup packet. + /// Suitably-sized data buffer for either input or output, (depending on direction bits within bmRequestType). + /// wLength field for the setup packet. The data buffer should be at least this size. + /// + /// 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). + /// + /// Transferred Number of transferred bytes. + /// Throws exception if device is disconnected or not opened for operation. + /// Throws exception if transfer timed-out. + 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; + } + } +} diff --git a/Tizen.System.Usb/Usb/UsbDevice.cs b/Tizen.System.Usb/Usb/UsbDevice.cs index a1c6ca3..c625825 100644 --- a/Tizen.System.Usb/Usb/UsbDevice.cs +++ b/Tizen.System.Usb/Usb/UsbDevice.cs @@ -82,6 +82,18 @@ namespace Tizen.System.Usb } /// + /// Control endpoint (endpoint 0). + /// + public UsbControlEndpoint ControlEndpoint + { + get + { + ThrowIfDisposed(); + return new UsbControlEndpoint(this); + } + } + + /// /// Active configuration for the device. /// /// Throws exception if device is disconnected. diff --git a/Tizen.System.Usb/Usb/UsbEndpoint.cs b/Tizen.System.Usb/Usb/UsbEndpoint.cs index e5dfec8..022fafd 100644 --- a/Tizen.System.Usb/Usb/UsbEndpoint.cs +++ b/Tizen.System.Usb/Usb/UsbEndpoint.cs @@ -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) -- 2.7.4