--- /dev/null
+/*
+ * 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
namespace Tizen.System.Usb
{
/// <summary>
- /// Enumeration of transfer direction for Endpoints
+ /// Enumeration of transfer direction for Endpoints.
/// </summary>
public enum EndpointDirection
{
namespace Tizen.System.Usb
{
/// <summary>
- /// Enumeration of isochronous endpoint's synchronization type
+ /// Enumeration of isochronous endpoint's synchronization type.
/// </summary>
public enum SynchronizationType
{
namespace Tizen.System.Usb
{
/// <summary>
- /// Enumeration of an endpoint's usage type
+ /// Enumeration of an endpoint's usage type.
/// </summary>
public enum UsageType
{
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);
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);
}
}
}
--- /dev/null
+/*
+ * 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()
+ {
+ }
+ }
+}
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);
}
}
/// <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);
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);
}
}