internal UsbConfigDescriptor(MonoUsbConfigDescriptor descriptor) { Attributes = descriptor.bmAttributes; ConfigID = descriptor.bConfigurationValue; DescriptorType = descriptor.bDescriptorType; InterfaceCount = descriptor.bNumInterfaces; Length = descriptor.bLength; MaxPower = descriptor.MaxPower; StringIndex = descriptor.iConfiguration; TotalLength = (short) descriptor.wTotalLength; }
internal UsbConfigInfo(MonoUsbDevice usbDevice, MonoUsbConfigDescriptor configDescriptor) { mUsbDevice = usbDevice; mUsbConfigDescriptor = new UsbConfigDescriptor(configDescriptor); List<MonoUsbInterface> monoUSBInterfaces = configDescriptor.InterfaceList; foreach (MonoUsbInterface usbInterface in monoUSBInterfaces) { List<MonoUsbAltInterfaceDescriptor> monoUSBAltInterfaces = usbInterface.AltInterfaceList; foreach (MonoUsbAltInterfaceDescriptor monoUSBAltInterface in monoUSBAltInterfaces) { UsbInterfaceInfo usbInterfaceInfo = new UsbInterfaceInfo(mUsbDevice, monoUSBAltInterface); mInterfaceList.Add(usbInterfaceInfo); } } }
public static void Main(string[] args) { // Initialize the context. sessionHandle = new MonoUsbSessionHandle(); if (sessionHandle.IsInvalid) throw new Exception(String.Format("Failed intialized libusb context.\n{0}:{1}", MonoUsbSessionHandle.LastErrorCode, MonoUsbSessionHandle.LastErrorString)); MonoUsbProfileList profileList = new MonoUsbProfileList(); // The list is initially empty. // Each time refresh is called the list contents are updated. int ret = profileList.Refresh(sessionHandle); if (ret < 0) throw new Exception("Failed to retrieve device list."); Console.WriteLine("{0} device(s) found.", ret); // Use the GetList() method to get a generic List of MonoUsbProfiles // Find all profiles that match in the MyVidPidPredicate. List<MonoUsbProfile> myVidPidList = profileList.GetList().FindAll(MyVidPidPredicate); // myVidPidList reresents a list of connected USB devices that matched // in MyVidPidPredicate. foreach (MonoUsbProfile profile in myVidPidList) { // Write the VendorID and ProductID to console output. Console.WriteLine("[Device] Vid:{0:X4} Pid:{1:X4}", profile.DeviceDescriptor.VendorID, profile.DeviceDescriptor.ProductID); // Loop through all of the devices configurations. for (byte i = 0; i < profile.DeviceDescriptor.ConfigurationCount; i++) { // Get a handle to the configuration. MonoUsbConfigHandle configHandle; if (MonoUsbApi.GetConfigDescriptor(profile.ProfileHandle, i, out configHandle) < 0) continue; if (configHandle.IsInvalid) continue; // Create a MonoUsbConfigDescriptor instance for this config handle. MonoUsbConfigDescriptor configDescriptor = new MonoUsbConfigDescriptor(configHandle); // Write the bConfigurationValue to console output. Console.WriteLine(" [Config] bConfigurationValue:{0}", configDescriptor.bConfigurationValue); // Interate through the InterfaceList foreach (MonoUsbInterface usbInterface in configDescriptor.InterfaceList) { // Interate through the AltInterfaceList foreach (MonoUsbAltInterfaceDescriptor usbAltInterface in usbInterface.AltInterfaceList) { // Write the bInterfaceNumber and bAlternateSetting to console output. Console.WriteLine(" [Interface] bInterfaceNumber:{0} bAlternateSetting:{1}", usbAltInterface.bInterfaceNumber, usbAltInterface.bAlternateSetting); // Interate through the EndpointList foreach (MonoUsbEndpointDescriptor endpoint in usbAltInterface.EndpointList) { // Write the bEndpointAddress, EndpointType, and wMaxPacketSize to console output. Console.WriteLine(" [Endpoint] bEndpointAddress:{0:X2} EndpointType:{1} wMaxPacketSize:{2}", endpoint.bEndpointAddress, (EndpointType) (endpoint.bmAttributes & 0x3), endpoint.wMaxPacketSize); } } } // Not neccessary, but good programming practice. configHandle.Close(); } } // Not neccessary, but good programming practice. profileList.Close(); // Not neccessary, but good programming practice. sessionHandle.Close(); }
private static ErrorCode GetConfigs(MonoUsbDevice usbDevice, out List<UsbConfigInfo> configInfoListRtn) { configInfoListRtn = new List<UsbConfigInfo>(); UsbError usbError = null; List<MonoUsbConfigDescriptor> configList = new List<MonoUsbConfigDescriptor>(); int iConfigs = usbDevice.Info.Descriptor.ConfigurationCount; for (int iConfig = 0; iConfig < iConfigs; iConfig++) { MonoUsbConfigHandle nextConfigHandle; int ret = MonoUsbApi.GetConfigDescriptor(usbDevice.mMonoUSBProfile.ProfileHandle, (byte) iConfig, out nextConfigHandle); Debug.WriteLine(string.Format("GetConfigDescriptor:{0}", ret)); if (ret != 0 || nextConfigHandle.IsInvalid) { usbError = UsbError.Error(ErrorCode.MonoApiError, ret, String.Format("GetConfigDescriptor Failed at index:{0}", iConfig), usbDevice); return usbError.ErrorCode; } try { MonoUsbConfigDescriptor nextConfig = new MonoUsbConfigDescriptor(); Marshal.PtrToStructure(nextConfigHandle.DangerousGetHandle(), nextConfig); UsbConfigInfo nextConfigInfo = new UsbConfigInfo(usbDevice, nextConfig); configInfoListRtn.Add(nextConfigInfo); } catch (Exception ex) { UsbError.Error(ErrorCode.InvalidConfig, Marshal.GetLastWin32Error(), ex.ToString(), usbDevice); } finally { if (!nextConfigHandle.IsInvalid) nextConfigHandle.Dispose(); } } return ErrorCode.Success; }