/*
* 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
{
///
/// Class to manage USB Configuration.
///
/// 5
public class UsbConfiguration : IDisposable
{
internal readonly Interop.UsbConfigHandle _handle;
private readonly UsbDevice _parent;
private Dictionary _interfaces;
internal UsbConfiguration(UsbDevice parent, Interop.UsbConfigHandle handle)
{
_parent = parent;
_handle = handle;
}
///
/// Checks if device is self-powered in given configuration.
///
/// http://tizen.org/feature/usb.host
/// The required feature is not supported.
/// 5
public bool IsSelfPowered
{
get
{
ThrowIfDisposed();
return Interop.NativeGet(_handle.IsSelfPowered);
}
}
///
/// Checks if device in given configuration supports remote wakeup.
///
/// http://tizen.org/feature/usb.host
/// The required feature is not supported.
/// 5
public bool SupportRemoteWakeup
{
get
{
ThrowIfDisposed();
return Interop.NativeGet(_handle.SupportRemoteWakeup);
}
}
///
/// Gets maximum power required in given configuration, in mA.
///
/// http://tizen.org/feature/usb.host
/// The required feature is not supported.
/// 5
public int MaximumPowerRequired
{
get
{
ThrowIfDisposed();
return Interop.NativeGet(_handle.GetMaxPower);
}
}
///
/// Dictionary mapping interfaces Ids to interface instances for given configuration.
///
/// http://tizen.org/feature/usb.host
/// The required feature is not supported.
/// 5
public IReadOnlyDictionary Interfaces
{
get
{
ThrowIfDisposed();
if (_interfaces == null)
{
_interfaces = new Dictionary();
int count = Interop.NativeGet(_handle.GetNumInterfaces);
for (int i = 0; i < count; ++i)
{
IntPtr handle;
_handle.GetInterface(i, out handle);
UsbInterface usbInterface = new UsbInterface(this, new Interop.UsbInterfaceHandle(handle));
_interfaces.Add(usbInterface.Id, usbInterface);
}
}
return _interfaces;
}
}
///
/// Configuration string.
///
/// http://tizen.org/feature/usb.host
/// The required feature is not supported.
///
/// Throws exception if device is disconnected or not opened for operation or busy as its interfaces are currently claimed.
///
/// 5
public string ConfigurationString
{
get
{
ThrowIfDisposed();
_parent.ThrowIfDeviceNotOpened();
return Interop.DescriptorString(_handle.GetConfigStr);
}
}
///
/// Set this configuration as active configuration for the device.
///
/// http://tizen.org/feature/usb.host
/// The required feature is not supported.
/// Throws exception if device is disconnected or not opened for operation.
/// 5
public void SetAsActive()
{
ThrowIfDisposed();
_parent.ThrowIfDeviceNotOpened();
_handle.SetAsActive().ThrowIfFailed("Failed to activate this configuration");
}
internal void ThrowIfDisposed()
{
if (disposedValue) throw new ObjectDisposedException("Configuration is already disposed");
_parent.ThrowIfDisposed();
}
internal void ThrowIfDeviceNotOpened()
{
_parent.ThrowIfDeviceNotOpened();
}
#region IDisposable Support
private bool disposedValue = false;
///
/// Releases all resources used by the ConnectionProfile.
/// It should be called after finished using of the object.
/// 5
internal virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
_handle.Dispose();
disposedValue = true;
}
}
///
/// Finalizes an instance of the UsbConfiguration class.
///
/// 5
~UsbConfiguration()
{
Dispose(false);
}
///
/// Releases all resources used by the ConnectionProfile.
/// It should be called after finished using of the object.
/// 5
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}