internal static unsafe UsbConfigInfo FromUsbConfigDescriptor(global::LibUsbDotNet.LibUsb.UsbDevice device, ConfigDescriptor descriptor) { Debug.Assert(descriptor.DescriptorType == (int)DescriptorType.Config, "A config descriptor was expected"); UsbConfigInfo value = new UsbConfigInfo(); value.Attributes = descriptor.Attributes; value.Configuration = device.GetStringDescriptor(descriptor.Configuration, failSilently: true); value.ConfigurationValue = descriptor.ConfigurationValue; value.RawDescriptors = new byte[descriptor.ExtraLength]; if (descriptor.ExtraLength > 0) { Span <byte> extra = new Span <byte>(descriptor.Extra, descriptor.ExtraLength); extra.CopyTo(value.RawDescriptors); } var interfaces = (Interface *)descriptor.Interface; for (int i = 0; i < descriptor.NumInterfaces; i++) { var values = UsbInterfaceInfo.FromUsbInterface(device, interfaces[i]); value.interfaces.AddRange(values); } value.MaxPower = descriptor.MaxPower; return(value); }
private static UsbInterfaceInfo usb_find_interface(UsbConfigInfo config_descriptor, int interface_number, out UsbInterfaceInfo first_interface) { first_interface = null; if (ReferenceEquals(config_descriptor, null)) return null; ReadOnlyCollection<UsbInterfaceInfo> interfaces = config_descriptor.InterfaceInfoList; for (int intfIndex = 0; intfIndex < interfaces.Count; intfIndex++) { if (ReferenceEquals(first_interface, null)) first_interface = interfaces[intfIndex]; if (interfaces[intfIndex].Descriptor.InterfaceID == interface_number) { return interfaces[intfIndex]; } } return null; }
private static ErrorCode GetConfigs(LibUsbDevice usbDevice, out List<UsbConfigInfo> configInfoListRtn) { configInfoListRtn = new List<UsbConfigInfo>(); UsbError usbError = null; List<LibUsbConfigDescriptor> configList = new List<LibUsbConfigDescriptor>(); int iConfigs = usbDevice.Info.Descriptor.ConfigurationCount; for (int iConfig = 0; iConfig < iConfigs; iConfig++) { LibUsbConfigHandle nextConfigHandle; int ret = LibUsbApi.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 { LibUsbConfigDescriptor nextConfig = new LibUsbConfigDescriptor(); 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; }
/// <summary> /// Looks up endpoint/interface information in a configuration. /// </summary> /// <param name="currentConfigInfo">The config to seach.</param> /// <param name="endpointAddress">The endpoint address to look for.</param> /// <param name="usbInterfaceInfo">On success, the <see cref="UsbInterfaceInfo"/> class for this endpoint.</param> /// <param name="usbEndpointInfo">On success, the <see cref="UsbEndpointInfo"/> class for this endpoint.</param> /// <returns>True of the endpoint was found, otherwise false.</returns> public static bool LookupEndpointInfo(UsbConfigInfo currentConfigInfo, byte endpointAddress, out UsbInterfaceInfo usbInterfaceInfo, out UsbEndpointInfo usbEndpointInfo) { bool found = false; usbInterfaceInfo = null; usbEndpointInfo = null; foreach (UsbInterfaceInfo interfaceInfo in currentConfigInfo.InterfaceInfoList) { foreach (UsbEndpointInfo endpointInfo in interfaceInfo.EndpointInfoList) { if ((endpointAddress & UsbConstants.ENDPOINT_NUMBER_MASK) == 0) { // find first read/write endpoint if ((endpointAddress & UsbConstants.ENDPOINT_DIR_MASK) == 0 && (endpointInfo.Descriptor.EndpointID & UsbConstants.ENDPOINT_DIR_MASK) == 0) { // first write endpoint found = true; } if ((endpointAddress & UsbConstants.ENDPOINT_DIR_MASK) != 0 && (endpointInfo.Descriptor.EndpointID & UsbConstants.ENDPOINT_DIR_MASK) != 0) { // first read endpoint found = true; } } else if (endpointInfo.Descriptor.EndpointID == endpointAddress) { found = true; } if (found) { usbInterfaceInfo = interfaceInfo; usbEndpointInfo = endpointInfo; return true; } } } return false; }
private bool DeviceInit(int vendorID, int productID, bool deviceReset = true) { try { _vendorID = vendorID; _productID = productID; Device = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(vendorID, productID)); if(Device == null) { Main.SendDebug(string.Format("No Device Found with VendorID: 0x{0:X04} and ProductID: 0x{1:X04}", vendorID, productID)); Release(); return false; } if(deviceReset) { Release(); Thread.Sleep(1000); return DeviceInit(vendorID, productID, false); } DeviceConfigInfo = Device.Configs[0]; var wholeUsbDevice = Device as IUsbDevice; if(!ReferenceEquals(wholeUsbDevice, null)) { wholeUsbDevice.SetConfiguration(DeviceConfigInfo.Descriptor.ConfigID); wholeUsbDevice.ClaimInterface(0); } _reader = Device.OpenEndpointReader((ReadEndpointID) 0x82); ClearReadBuffer(); _writer = Device.OpenEndpointWriter((WriteEndpointID) 0x05); UsbDevice.UsbErrorEvent += UsbDeviceOnUsbErrorEvent; return true; } catch(Exception ex) { Main.SendDebug(String.Format("Device Init exception occured: {0}", ex.Message)); throw; } }
/// <summary> /// Looks up endpoint/interface information in a configuration. /// </summary> /// <param name="currentConfigInfo">The config to seach.</param> /// <param name="endpointAddress">The endpoint address to look for.</param> /// <param name="usbInterfaceInfo">On success, the <see cref="UsbInterfaceInfo"/> class for this endpoint.</param> /// <param name="usbEndpointInfo">On success, the <see cref="UsbEndpointInfo"/> class for this endpoint.</param> /// <returns>True of the endpoint was found, otherwise false.</returns> public static bool LookupEndpointInfo(UsbConfigInfo currentConfigInfo, byte endpointAddress, out UsbInterfaceInfo usbInterfaceInfo, out UsbEndpointInfo usbEndpointInfo) { return LookupEndpointInfo(currentConfigInfo, -1, endpointAddress, out usbInterfaceInfo, out usbEndpointInfo); }