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.Print("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.Close(); } } } return(ErrorCode.Success); }
void testComms() { if (profiles.Count < 1) { logger.info("testComms: no profiles"); return; } foreach (MonoUsbProfile profile in profiles) { MonoUsbDeviceHandle device = null; try { logger.info("Opening {0:x4}:{1:x4}", profile.DeviceDescriptor.VendorID, profile.DeviceDescriptor.ProductID); device = profile.OpenDeviceHandle(); if (device.IsInvalid) { logger.error("Failed opening device handle: {0} ({1})", MonoUsbDeviceHandle.LastErrorCode, MonoUsbDeviceHandle.LastErrorString); continue; } // re-confirm configurations for this profile for (byte i = 0; i < profile.DeviceDescriptor.ConfigurationCount; i++) { MonoUsbConfigHandle configHandle; if (MonoUsbApi.GetConfigDescriptor(profile.ProfileHandle, i, out configHandle) < 0) { continue; } if (configHandle.IsInvalid) { continue; } MonoUsbConfigDescriptor configDescriptor = new MonoUsbConfigDescriptor(configHandle); logger.info(" configuration #{0}: {1} ({2})", i, configDescriptor.bConfigurationValue, configDescriptor); logger.info(" type = {0}", configDescriptor.bDescriptorType); logger.info(" length = {0}", configDescriptor.bLength); logger.info(" attributes = {0:x2}", configDescriptor.bmAttributes); logger.info(" interfaces = {0}", configDescriptor.bNumInterfaces); logger.info(" extLength = {0}", configDescriptor.ExtraLength); logger.info(" iConfig = {0}", configDescriptor.iConfiguration); logger.info(" maxPower = {0}", configDescriptor.MaxPower); logger.info(" totalLength= {0}", configDescriptor.wTotalLength); } int result = 0; // Set Configuration // Note: http://libusb.sourceforge.net/api-1.0/caveats.html#configsel MonoUsbError error = (MonoUsbError)(result = MonoUsbApi.SetConfiguration(device, 1)); if (false && result < 0) { logger.error("Failed SetConfiguration: {0} ({1}) (result = {2})", error, MonoUsbApi.StrError(error), result); continue; } // Claim Interface error = (MonoUsbError)(result = MonoUsbApi.ClaimInterface(device, 0)); if (result < 0) { logger.error("Failed ClaimInterface: {0} ({1})", error, MonoUsbApi.StrError(error)); continue; } // retain device handles devices.Add(device); byte DEVICE_TO_HOST = 0xc0; byte SECOND_TIER_COMMAND = 0xff; byte GET_MODEL_CONFIG = 0x01; byte PAGE_SIZE = 64; byte PAGE_ID = 0; int TIMEOUT_MS = 1000; // Create a vendor specific control setup, allocate 1 byte for return control data. // byte requestType = (byte)(UsbCtrlFlags.Direction_In | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.RequestType_Vendor); // byte request = 0x0F; // MonoUsbControlSetupHandle controlSetupHandle = new MonoUsbControlSetupHandle(requestType, request, 0, 0, 1); MonoUsbControlSetupHandle controlSetupHandle = new MonoUsbControlSetupHandle(DEVICE_TO_HOST, SECOND_TIER_COMMAND, GET_MODEL_CONFIG, PAGE_ID, PAGE_SIZE); // Transfer the control setup packet int len = libusb_control_transfer(device, controlSetupHandle, TIMEOUT_MS); if (len > 0) { logger.info("Success"); byte[] ctrlDataBytes = controlSetupHandle.ControlSetup.GetData(len); string ctrlDataString = Helper.HexString(ctrlDataBytes, String.Empty, "h "); logger.info("Return Length: {0}", len); logger.info("DATA (hex) : [ {0} ]", ctrlDataString.Trim()); } MonoUsbApi.ReleaseInterface(device, 0); } finally { if (device != null) { logger.info("closing device"); device.Close(); } } } }
public static void ShowConfig(RichTextBox rtb) { // 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."); } rtb.AppendText(string.Format("{0} device(s) found.\r\n", 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) { MonoUsbDeviceHandle h = profile.OpenDeviceHandle();// Usb.OpenDeviceWithVidPid(sessionHandle, 0x1915, 0x007B); if (h.IsInvalid) { throw new Exception(string.Format("Failed opening device handle.\r\n{0}: {1}", MonoUsbDeviceHandle.LastErrorCode, MonoUsbDeviceHandle.LastErrorString)); } Usb.SetConfiguration(h, 1); Usb.ClaimInterface(h, 1); MonoUsbProfileHandle ph = Usb.GetDevice(h); int packetSize = Usb.GetMaxIsoPacketSize(ph, 0x88); // Write the VendorID and ProductID to console output. rtb.AppendText(string.Format("[Device] Vid:{0:X4} Pid:{1:X4}\r\n", 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. rtb.AppendText(string.Format(" [Config] bConfigurationValue:{0}\r\n", 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. rtb.AppendText(string.Format(" [Interface] bInterfaceNumber:{0} bAlternateSetting:{1}\r\n", usbAltInterface.bInterfaceNumber, usbAltInterface.bAlternateSetting)); // Interate through the EndpointList foreach (MonoUsbEndpointDescriptor endpoint in usbAltInterface.EndpointList) { // Write the bEndpointAddress, EndpointType, and wMaxPacketSize to console output. rtb.AppendText(string.Format(" [Endpoint] bEndpointAddress:{0:X2} EndpointType:{1} wMaxPacketSize:{2}\r\n", endpoint.bEndpointAddress, (EndpointType)(endpoint.bmAttributes & 0x3), endpoint.wMaxPacketSize)); if (endpoint.bEndpointAddress == 0x88) { } } } } // Not neccessary, but good programming practice. configHandle.Close(); } } // Not neccessary, but good programming practice. profileList.Close(); // Not neccessary, but good programming practice. sessionHandle.Close(); }
void findWasatchProfiles() { if (monoUsbSession == null || monoUsbSession.IsInvalid) { return; } MonoUsbProfileList profileList = new MonoUsbProfileList(); int deviceCount = profileList.Refresh(monoUsbSession); if (deviceCount < 0) { logger.error("init: Failed to retrieve device list"); return; } logger.info("{0} device(s) found.", deviceCount); profiles = profileList.GetList().FindAll(predicateWasatchVid); foreach (MonoUsbProfile profile in profiles) { logger.info("[Device] Vid:{0:X4} Pid:{1:X4}", profile.DeviceDescriptor.VendorID, profile.DeviceDescriptor.ProductID); logger.info(" Descriptors: {0}", profile.DeviceDescriptor); for (byte i = 0; i < profile.DeviceDescriptor.ConfigurationCount; i++) { MonoUsbConfigHandle configHandle; if (MonoUsbApi.GetConfigDescriptor(profile.ProfileHandle, i, out configHandle) < 0) { continue; } if (configHandle.IsInvalid) { continue; } MonoUsbConfigDescriptor configDescriptor = new MonoUsbConfigDescriptor(configHandle); logger.info(" [Config] bConfigurationValue: {0}", configDescriptor.bConfigurationValue); foreach (MonoUsbInterface usbInterface in configDescriptor.InterfaceList) { foreach (MonoUsbAltInterfaceDescriptor usbAltInterface in usbInterface.AltInterfaceList) { logger.info(" [Interface] bInterfaceNumber: {0}, bAlternateSetting: {1}", usbAltInterface.bInterfaceNumber, usbAltInterface.bAlternateSetting); foreach (MonoUsbEndpointDescriptor endpoint in usbAltInterface.EndpointList) { // Write the bEndpointAddress, EndpointType, and wMaxPacketSize to console output. logger.info(" [Endpoint] bEndpointAddress: 0x{0:X2}, EndpointType: {1}, wMaxPacketSize: {2}", endpoint.bEndpointAddress, (EndpointType)(endpoint.bmAttributes & 0x3), endpoint.wMaxPacketSize); } } } configHandle.Close(); } // profile.Close(); } }