--- /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_set_config")]
+ internal static extern ErrorCode SetAsActive(this UsbConfigHandle /* usb_host_config_h */ configuration);
+
+ [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_get_num_interfaces")]
+ internal static extern ErrorCode GetNumInterfaces(this UsbConfigHandle /* usb_host_config_h */ config, out int numInterfaces);
+
+ [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_is_self_powered")]
+ internal static extern ErrorCode IsSelfPowered(this UsbConfigHandle /* usb_host_config_h */ config, out bool selfPowered);
+
+ [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_support_remote_wakeup")]
+ internal static extern ErrorCode SupportRemoteWakeup(this UsbConfigHandle /* usb_host_config_h */ config, out bool remoteWakeup);
+
+ [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_get_max_power")]
+ internal static extern ErrorCode GetMaxPower(this UsbConfigHandle /* usb_host_config_h */ config, out int maxPower);
+
+ [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_config_str")]
+ internal static extern ErrorCode GetConfigStr(this UsbConfigHandle /* usb_host_config_h */ config, ref int length, byte[] data);
+
+ [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_get_interface")]
+ internal static extern ErrorCode GetInterface(this UsbConfigHandle /* usb_host_config_h */ config, int interfaceIndex, out UsbInterfaceHandle /* usb_host_interface_h */ usbInterface);
+
+ internal class UsbConfigHandle : SafeUsbHandle
+ {
+ [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_destroy")]
+ internal static extern ErrorCode ConfigDestroy(UsbConfigHandle /* usb_host_config_h */ config);
+
+ public UsbConfigHandle(IntPtr handle) : base(handle) { }
+ public override void Destroy()
+ {
+ ConfigDestroy(this).ThrowIfFailed("Failed to destroy native HostConfig handle");
+ }
+ }
+}
\ No newline at end of file
/// into the data buffer.
/// </param>
/// <param name="timeout">
- /// Timeout (in milliseconds) that this function should wait before giving up due to no response being
+ /// 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>Number of bytes actually transferred.</returns>
--- /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.Collections.Generic;
+
+namespace Tizen.System.Usb
+{
+ /// <summary>
+ /// Class to manage USB Configuration.
+ /// </summary>
+ public class UsbConfiguration : IDisposable
+ {
+ internal readonly Interop.UsbConfigHandle _handle;
+ private Dictionary<int, UsbInterface> _interfaces;
+
+ internal UsbConfiguration(Interop.UsbConfigHandle handle)
+ {
+ _handle = handle;
+ }
+
+ /// <summary>
+ /// Checks if device is self-powered in given configuration.
+ /// </summary>
+ public bool IsSelfPowered
+ {
+ get
+ {
+ ThrowIfDisposed();
+ return Interop.NativeGet<bool>(_handle.IsSelfPowered);
+ }
+ }
+
+ /// <summary>
+ /// Checks if device in given configuration supports remote wakeup.
+ /// </summary>
+ public bool SupportRemoteWakeup
+ {
+ get
+ {
+ ThrowIfDisposed();
+ return Interop.NativeGet<bool>(_handle.SupportRemoteWakeup);
+ }
+ }
+
+ /// <summary>
+ /// Gets maximum power required in given configuration, in mA.
+ /// </summary>
+ public int MaximumPowerRequired
+ {
+ get
+ {
+ ThrowIfDisposed();
+ return Interop.NativeGet<int>(_handle.GetMaxPower);
+ }
+ }
+
+ /// <summary>
+ /// Dictionary mapping interfaces Ids to interface instances for given configuration.
+ /// </summary>
+ public IReadOnlyDictionary<int, UsbInterface> Interfaces
+ {
+ get
+ {
+ ThrowIfDisposed();
+ if (_interfaces == null)
+ {
+ _interfaces = new Dictionary<int, UsbInterface>();
+ int count = Interop.NativeGet<int>(_handle.GetNumInterfaces);
+ for (int i = 0; i < count; ++i)
+ {
+ Interop.UsbInterfaceHandle handle;
+ _handle.GetInterface(i, out handle);
+ UsbInterface usbInterface = new UsbInterface(this, handle);
+ _interfaces.Add(usbInterface.Id, usbInterface);
+ }
+ }
+ return _interfaces;
+ }
+ }
+
+ /// <summary>
+ /// Configuration string.
+ /// </summary>
+ public string ConfigurationString
+ {
+ get
+ {
+ ThrowIfDisposed();
+ return Interop.DescriptorString(_handle.GetConfigStr);
+ }
+ }
+
+ /// <summary>
+ /// Set this configuration as active configuration for the device.
+ /// </summary>
+ /// <exception cref="InvalidOperationException">
+ /// Throws exception if device is disconnected or not opened for operation or busy as its interfaces are currently claimed.
+ /// </exception>
+ public void SetAsActive()
+ {
+ ThrowIfDisposed();
+ _handle.SetAsActive();
+ }
+
+ internal void ThrowIfDisposed()
+ {
+ if (disposedValue) throw new ObjectDisposedException("Configuration is already disposed");
+ }
+
+ #region IDisposable Support
+ private bool disposedValue = false;
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (!disposedValue)
+ {
+ _handle.Dispose();
+ disposedValue = true;
+ }
+ }
+
+ ~UsbConfiguration()
+ {
+ Dispose(false);
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+ #endregion
+ }
+}
\ No newline at end of file
public class UsbInterface
{
internal readonly Interop.UsbInterfaceHandle _handle;
+ private readonly UsbConfiguration _parent;
private Dictionary<int, UsbEndpoint> _endpoints;
private bool _isClaimed;
- internal UsbInterface(Interop.UsbInterfaceHandle handle)
+ internal UsbInterface(UsbConfiguration parent, Interop.UsbInterfaceHandle handle)
{
+ _parent = parent;
_handle = handle;
}
internal void ThrowIfDisposed()
{
+ _parent.ThrowIfDisposed();
}
}
}
/// into the data buffer.
/// </param>
/// <param name="timeout">
- /// Timeout (in milliseconds) that this function should wait before giving up due to no response being
+ /// 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>Number of bytes actually transferred.</returns>