/* WinUSBNet library
* (C) 2010 Thomas Bleeker (www.madwizard.org)
*
* Licensed under the MIT license, see license.txt or:
* http://www.opensource.org/licenses/mit-license.php
*/
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
namespace MadWizard.WinUSBNet
{
///
/// Collection of UsbInterface objects
///
public class USBInterfaceCollection : IEnumerable
{
private USBInterface[] _interfaces;
internal USBInterfaceCollection(USBInterface[] interfaces)
{
_interfaces = interfaces;
}
private class USBInterfaceEnumerator : IEnumerator
{
private int _index;
private USBInterface[] _interfaces;
public USBInterfaceEnumerator(USBInterface[] interfaces)
{
_interfaces = interfaces;
_index = -1;
}
public void Dispose()
{
// Intentionally empty
}
private USBInterface GetCurrent()
{
try
{
return _interfaces[_index];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
public USBInterface Current
{
get
{
return GetCurrent();
}
}
object IEnumerator.Current
{
get
{
return GetCurrent();
}
}
public bool MoveNext()
{
_index++;
return _index < _interfaces.Length;
}
public void Reset()
{
_index = -1;
}
}
///
/// Finds the first interface with that matches the device class
/// given by the parameter.
///
/// The device class the interface should match
/// The first interface with the given interface class, or null
/// if no such interface exists.
public USBInterface Find(USBBaseClass interfaceClass)
{
for (int i = 0; i < _interfaces.Length; i++)
{
USBInterface iface = _interfaces[i];
if (iface.BaseClass == interfaceClass)
return iface;
}
return null;
}
///
/// Finds all interfaces matching the device class given by the
/// parameter.
///
/// The device class the interface should match
/// An array of USBInterface objects matching the device class, or an empty
/// array if no interface matches.
public USBInterface[] FindAll(USBBaseClass interfaceClass)
{
List matchingInterfaces = new List();
for (int i = 0; i < _interfaces.Length; i++)
{
USBInterface iface = _interfaces[i];
if (iface.BaseClass == interfaceClass)
matchingInterfaces.Add(iface);
}
return matchingInterfaces.ToArray();
}
///
/// Returns a typed enumerator that iterates through a collection.
///
/// The enumerator object that can be used to iterate through the collection.
public IEnumerator GetEnumerator()
{
return new USBInterfaceEnumerator(_interfaces);
}
///
/// Get interface by interface number
///
/// Number of the interface to return. Note: this is the number from the interface descriptor, which
/// is not necessarily the same as the interface index.
/// Thrown when the given interface number does not exist in the collection.
///
public USBInterface this[ int interfaceNumber ]
{
get
{
for (int i = 0; i < _interfaces.Length; i++)
{
USBInterface iface = _interfaces[i];
if (iface.Number == interfaceNumber)
return iface;
}
throw new IndexOutOfRangeException(string.Format("No interface with number {0} exists.", interfaceNumber));
}
}
///
/// Returns an enumerator that iterates through a collection.
///
/// An IEnumerator object that can be used to iterate through the collection.
IEnumerator IEnumerable.GetEnumerator()
{
return new USBInterfaceEnumerator(_interfaces);
}
}
}