public static void Main(string[] Args) { Initialize(); Warn.LLogL("Make sure to open Goldtree after having opened Goldleaf on your console. Looking for connection..."); try { var pat = new KLST_PATTERN_MATCH { DeviceID = @"USB\VID_057E&PID_3000" }; var lst = new LstK(0, ref pat); lst.MoveNext(out var dinfo); USB = new UsbK(dinfo); Log.LogL("Connection was established."); } catch { Error.LLogL("Unable to find USB communication. Are you sure Goldleaf is open and plugged in?"); } Console.WriteLine(); while (true) { USB.CommandHandleLoop(); } Error.LLogL("An error ocurred handling Goldleaf's latest command request."); }
private void connect(bool re) { try { var pat = new KLST_PATTERN_MATCH { DeviceID = @"USB\VID_057E&PID_3000" }; var lst = new LstK(0, ref pat); lst.MoveNext(out var dinfo); this.USB = new UsbK(dinfo); } catch (Exception) { this.USB = null; } if (this.USB == null) { MessageBox.Show("No Switch running uLaunch detected!"); disconnect(); } else { if (!re) { MessageBox.Show("Switch running uLaunch detected, continuing!"); } miConn.IsEnabled = false; miReConn.IsEnabled = true; miDisConn.IsEnabled = true; USBThread = new Thread(new ThreadStart(USBThreadMain)); USBThread.Start(); } }
public MainForm() { InitializeComponent(); ViewerButton.Enabled = Utils.IsWindows(); if (Utils.IsWindows()) { try { var pat = new KLST_PATTERN_MATCH { DeviceID = @"USB\VID_057E&PID_3000" }; var lst = new LstK(0, ref pat); lst.MoveNext(out var dinfo); USB = new UsbK(dinfo); } catch { } if (USB == null) { ViewerButton.Enabled = false; } } var drive = Utils.DetectSDCardDrive(); if (drive != null) { EntryManagerButton.Enabled = true; PluginButton.Enabled = true; FoundDriveText.Text = "Found SD card on drive \"" + drive.VolumeLabel + "\" (" + drive.Name + ")"; } }
private void createWithoutExplictDestroyToolStripMenuItem_Click(object sender, EventArgs e) { LstK lstTemp = new LstK(KLST_FLAG.NONE); int count = 0; lstTemp.Count(ref count); }
private void referenceToolStripMenuItem_Click(object sender, EventArgs e) { LstK lstTemp = new LstK(KLST_FLAG.NONE); LstKList.Add(lstTemp); LstKField = lstTemp; }
private static void Main() { int deviceIndex = 0; int deviceCount = 0; KLST_DEVINFO_HANDLE deviceInfo; LstK lst = new LstK(KLST_FLAG.NONE); lst.Count(ref deviceCount); while (lst.MoveNext(out deviceInfo)) { // Write some information about the device. string displayLine = String.Format("{0} of {1}: {2}", ++deviceIndex, deviceCount, deviceInfo.DeviceDesc); Console.WriteLine(displayLine); Console.WriteLine("- " + deviceInfo.DeviceID); Console.WriteLine(); } if (deviceCount == 0) { Console.WriteLine("No devices found!\n"); } lst.Free(); }
private static UsbK FindDevice() { var patternMatch = new KLST_PATTERN_MATCH { DeviceID = @"USB\VID_0955&PID_7321" }; var deviceList = new LstK(0, ref patternMatch); deviceList.MoveNext(out var deviceInfo); return(new UsbK(deviceInfo)); }
private void derefrenceToolStripMenuItem_Click(object sender, EventArgs e) { if (LstKList.Count > 0) { LstKList.Clear(); } else { LstKField = null; } }
public static string GetUsbDevices() { string report = "usb:\n"; KLST_DEVINFO_HANDLE deviceInfo; LstK lst = new LstK(KLST_FLAG.NONE); while (lst.MoveNext(out deviceInfo)) { report += deviceInfo.SerialNumber + ",Connected=" + deviceInfo.Connected + ",Service=" + deviceInfo.Service + ",Pid=" + deviceInfo.Common.Pid.ToString("X4") + ",Vid=" + deviceInfo.Common.Vid.ToString("X4") + ",Mfg=" + deviceInfo.Mfg + ",DeviceDesc=" + deviceInfo.DeviceDesc + "\n"; } lst.Free(); return(report); }
private void exhaustAllHandlesToolStripMenuItem_Click(object sender, EventArgs e) { LstKList.Clear(); LstKField = null; int count = 0; try { while (true) { new LstK(KLST_FLAG.NONE); count++; } } catch { } Debug.Print("All handles exhausted. Count:{0}", count); }
// Scan the current usb devices to check all connected. private void CheckConnections() { lock (UsbConnectionLock) { KLST_DEVINFO_HANDLE deviceInfo; LstK lst = new LstK(KLST_FLAG.NONE); int deviceCount = 0; lst.Count(ref deviceCount); while (lst.MoveNext(out deviceInfo)) { LinkDevice d = LinkManager.Manager.FindDevice(UsbLinkDevice.GetDevIdFromHandle(deviceInfo)); // Check if d is a suspended Acc with same SN but different V/P. if (d == null || ((d.State != ComponentState.Working) /*&& !UsbLinkDevice.CompareVidPid(d, deviceInfo)*/)) { if (UsbPermissionValidator.CheckAllowed(deviceInfo.Common.Vid, deviceInfo.Common.Pid)) { UsbLinkDevice.TryOpeningDevice(deviceInfo); } } } lst.Free(); } }
public static void Refresh() { WMIDeviceInfo = null; Device = null; DeviceWrapper = null; foreach (UsbDeviceInfo info in CreateUsbControllerDeviceInfos(GetUsbDevices())) { if (info.DeviceID.StartsWith($"USB\\VID_{VID}&PID_{PID}")) { WMIDeviceInfo = info; var patternMatch = new KLST_PATTERN_MATCH { ClassGUID = WMIDeviceInfo.ClassGuid }; var deviceList = new LstK(0, ref patternMatch); deviceList.MoveNext(out KLST_DEVINFO_HANDLE deviceInfo); Device = new UsbK(deviceInfo); Device.SetAltInterface(0, false, 0); DeviceWrapper = new UsbKWrapper(Device); break; } } }
public static void Scan() { WMIDeviceInfo = null; Device = null; DeviceWrapper = null; foreach (UsbDeviceInfo info in AllUsbDevices) { if (info.DeviceID.StartsWith($"USB\\VID_{VID}&PID_{PID}")) { WMIDeviceInfo = info; if (!LibusbKInstalled) { MessageBoxResult result = MessageBox.Show("You have plugged in your console, but it lacks the libusbK driver. Want to install it? (You cannot inject anything until this is done)", "", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { InstallDriver(); } WMIDeviceInfo = AllUsbDevices.First(x => x.DeviceID == WMIDeviceInfo.DeviceID); // we need to refresh the info } if (LibusbKInstalled) { var patternMatch = new KLST_PATTERN_MATCH { ClassGUID = WMIDeviceInfo.ClassGuid }; var deviceList = new LstK(0, ref patternMatch); deviceList.MoveNext(out KLST_DEVINFO_HANDLE deviceInfo); Device = new UsbK(deviceInfo); Device.SetAltInterface(0, false, 0); DeviceWrapper = new UsbKWrapper(Device); } break; } } }
public static void Main(string[] Args) { Initialize(); Warn.LLogL("Make sure Goldleaf is already opened and the console is connected to this system via USB. Looking for connection..."); try { var pat = new KLST_PATTERN_MATCH { DeviceID = @"USB\VID_057E&PID_3000" }; var lst = new LstK(0, ref pat); lst.MoveNext(out var dinfo); USB = new UsbK(dinfo); Log.LogL("Connection with Goldleaf was established."); } catch { Error.LLogL("Unable to find connection. Have you installed libusbK drivers? Are you sure Goldleaf is open and the USB icon is shown there?"); } Console.WriteLine(); while (true) { USB.CommandHandleLoop(); } }
public ViewerMainForm() { InitializeComponent(); InitializePictureBox(CaptureBox); try { var pat = new KLST_PATTERN_MATCH { DeviceID = @"USB\VID_057E&PID_3000" }; var lst = new LstK(0, ref pat); lst.MoveNext(out var dinfo); USB = new UsbK(dinfo); } catch { } Toolbox = new ToolboxForm(this); Toolbox.Show(); USBThread = new Thread(new ThreadStart(USBThreadMain)); USBThread.Start(); }
public static void Main(string[] Args) { Initialize(); var pat = new KLST_PATTERN_MATCH { DeviceID = @"USB\VID_057E&PID_3000" }; var lst = new LstK(0, ref pat); try { lst.MoveNext(out var dinfo); USB = new UsbK(dinfo); } catch { // System.Diagnostics.Process.Start(System.Windows.Forms.Application.ExecutablePath); return; } while (USB.HandleNextCommand() == CommandHandleResult.Success) { ; } /* * usbnsp: * // Console.Clear(); * Initialize(); * try * { * var pat = new KLST_PATTERN_MATCH { DeviceID = @"USB\VID_057E&PID_3000" }; * var lst = new LstK(0, ref pat); * lst.MoveNext(out var dinfo); * USB = new UsbK(dinfo); * } * catch * { * Error.Log("No USB connection was found. Make sure you have Goldleaf open before running Goldtree."); * } * try * { * Command c = new Command(CommandId.ConnectionRequest); * USB.Write(c); * Log.Log("Attempting to connect to Goldleaf via USB..."); * Command rc = USB.Read(); * if(rc.MagicOk()) * { * if(rc.IsCommandId(CommandId.ConnectionResponse)) * { * Log.Log("Connection was established with Goldleaf."); * Log.Log("Select the NSP to send to Goldleaf on the dialog."); * OpenFileDialog fd = new OpenFileDialog() * { * Title = "Select NSP to send to Goldleaf via USB", * Filter = "NSP / Nintendo Submission Package (*.nsp)|*.nsp", * Multiselect = false, * }; * if(fd.ShowDialog() == DialogResult.OK) * { * string nsp = fd.FileName; * string nspname = Path.GetFileName(nsp); * c = new Command(CommandId.NSPName); * USB.Write(c); * USB.Write((uint)nspname.Length); * USB.Write(nspname); * Log.Log("Selected NSP's name was sent to Goldleaf. Waiting for install approval from Goldleaf..."); * Command rc2 = USB.Read(); * if(rc2.MagicOk()) * { * if(rc2.IsCommandId(CommandId.Start)) * { * Log.Log("Goldleaf is ready for the installation. Preparing everything..."); * try * { * FileStream fs = new FileStream(nsp, FileMode.Open); * StreamStorage ist = new StreamStorage(fs, true); * Pfs pnsp = new Pfs(ist); * ist.Dispose(); * fs.Close(); * fs = new FileStream(nsp, FileMode.Open); * uint filecount = (uint)pnsp.Files.Length; * c = new Command(CommandId.NSPData); * USB.Write(c); * USB.Write(filecount); * int tikidx = -1; * int certidx = -1; * int tmpidx = 0; * foreach(var file in pnsp.Files) * { * ulong offset = (ulong)pnsp.HeaderSize + (ulong)file.Offset; * ulong size = (ulong)file.Size; * uint len = (uint)file.Name.Length; * USB.Write(len); * USB.Write(file.Name); * USB.Write(offset); * USB.Write(size); * if(Path.GetExtension(file.Name).Replace(".", "").ToLower() == "tik") tikidx = tmpidx; * else if(Path.GetExtension(file.Name).Replace(".", "").ToLower() == "cert") certidx = tmpidx; * tmpidx++; * } * BinaryReader br = new BinaryReader(fs); * while(true) * { * Command ccmd = USB.Read(); * if(ccmd.MagicOk()) * { * if(ccmd.IsCommandId(CommandId.NSPContent)) * { * USB.Read(out uint idx); * Log.Log("Sending content \'" + pnsp.Files[idx].Name + "\'... (" + (idx + 1) + " of " + pnsp.Files.Length + ")"); * PfsFileEntry ent = pnsp.Files[idx]; * long rsize = 0x100000; * long coffset = pnsp.HeaderSize + ent.Offset; * long toread = ent.Size; * long tmpread = 1; * byte[] bufb; * while((tmpread > 0) && (toread > 0) && (coffset < (coffset + ent.Size))) * { * if(rsize >= ent.Size) rsize = ent.Size; * int tor = (int)Math.Min(rsize, toread); * br.BaseStream.Position = coffset; * bufb = br.ReadBytes(tor); * tmpread = bufb.Length; * USB.Write(bufb); * coffset += tmpread; * toread -= tmpread; * } * Log.Log("Content was sent to Goldleaf."); * } * else if(ccmd.IsCommandId(CommandId.NSPTicket)) * { * Log.Log("Sending ticket file..."); * PfsFileEntry tik = pnsp.Files[tikidx]; * br.BaseStream.Seek(pnsp.HeaderSize + tik.Offset, SeekOrigin.Begin); * byte[] file = br.ReadBytes((int)tik.Size); * USB.Write(file); * Log.Log("Ticket was sent to Goldleaf."); * } * else if(ccmd.IsCommandId(CommandId.Finish)) break; * else * { * USB = null; * Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); * } * } * else * { * USB = null; * Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); * } * } * } * catch * { * Error.Log("An error ocurred opening the selected NSP. Are you sure it's a valid NSP?"); * } * } * else if(rc2.IsCommandId(CommandId.Finish)) * { * USB = null; * Error.Log("Goldleaf has canceled the installation."); * } * else * { * USB = null; * Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); * } * } * else * { * USB = null; * Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); * } * } * else Error.Log("The dialog was closed without selecting a NSP, or another error ocurred. Reopen Goldleaf and Goldtree and try again."); * } * else if(rc.IsCommandId(CommandId.Finish)) * { * USB = null; * Error.Log("Goldleaf has canceled the installation."); * } * else * { * USB = null; * Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); * } * } * else * { * USB = null; * Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); * } * } * catch * { * Error.Log("An error ocurred selecting the NSP to be sent."); * } * Log.Log("The installation has finished. Press ENTER to close Goldtree, or any other key to start another USB installation.", true); * ConsoleKeyInfo ki = Console.ReadKey(); * if(ki.Key == ConsoleKey.Enter) * { * if(USB != null) * { * Command cmd = new Command(CommandId.Finish); * USB.Write(cmd); * } * Environment.Exit(0); * } * else goto usbnsp; */ }
public static void Main(string[] Args) { usbnsp: Console.Clear(); Initialize(); try { var pat = new KLST_PATTERN_MATCH { DeviceID = @"USB\VID_057E&PID_3000" }; var lst = new LstK(0, ref pat); lst.MoveNext(out var dinfo); USB = new UsbK(dinfo); } catch { Error.Log("No USB connection was found. Make sure you have Goldleaf open before running Goldtree."); } try { Command c = new Command(CommandId.ConnectionRequest); USB.Write(c); Log.Log("Attempting to connect to Goldleaf via USB..."); Command rc = USB.Read(); if (rc.MagicOk()) { if (rc.IsCommandId(CommandId.ConnectionResponse)) { Log.Log("Connection was established with Goldleaf."); Log.Log("Select the NSP to send to Goldleaf on the dialog."); OpenFileDialog fd = new OpenFileDialog() { Title = "Select NSP to send to Goldleaf via USB", Filter = "NSP / Nintendo Submission Package (*.nsp)|*.nsp", Multiselect = false, }; if (fd.ShowDialog() == DialogResult.OK) { string nsp = fd.FileName; string nspname = Path.GetFileName(nsp); c = new Command(CommandId.NSPName); USB.Write(c); USB.Write((uint)nspname.Length); USB.Write(nspname); Log.Log("Selected NSP's name was sent to Goldleaf. Waiting for install approval from Goldleaf..."); Command rc2 = USB.Read(); if (rc2.MagicOk()) { if (rc2.IsCommandId(CommandId.Start)) { Log.Log("Goldleaf is ready for the installation. Preparing everything..."); try { FileStream fs = new FileStream(nsp, FileMode.Open); StreamStorage ist = new StreamStorage(fs, true); Pfs pnsp = new Pfs(ist); ist.Dispose(); fs.Close(); fs = new FileStream(nsp, FileMode.Open); uint filecount = (uint)pnsp.Files.Length; c = new Command(CommandId.NSPData); USB.Write(c); USB.Write(filecount); int tikidx = -1; int certidx = -1; int tmpidx = 0; foreach (var file in pnsp.Files) { ulong offset = (ulong)pnsp.HeaderSize + (ulong)file.Offset; ulong size = (ulong)file.Size; uint len = (uint)file.Name.Length; USB.Write(len); USB.Write(file.Name); USB.Write(offset); USB.Write(size); if (Path.GetExtension(file.Name).Replace(".", "").ToLower() == "tik") { tikidx = tmpidx; } else if (Path.GetExtension(file.Name).Replace(".", "").ToLower() == "cert") { certidx = tmpidx; } tmpidx++; } BinaryReader br = new BinaryReader(fs); while (true) { Command ccmd = USB.Read(); if (ccmd.MagicOk()) { if (ccmd.IsCommandId(CommandId.NSPContent)) { USB.Read(out uint idx); Log.Log("Sending content \'" + pnsp.Files[idx].Name + "\'... (" + (idx + 1) + " of " + pnsp.Files.Length + ")"); PfsFileEntry ent = pnsp.Files[idx]; long rsize = 0x800000; long coffset = pnsp.HeaderSize + ent.Offset; long toread = ent.Size; long tmpread = 1; byte[] bufb; while ((tmpread > 0) && (toread > 0) && (coffset < (coffset + ent.Size))) { if (rsize >= ent.Size) { rsize = ent.Size; } int tor = (int)Math.Min(rsize, toread); br.BaseStream.Position = coffset; bufb = br.ReadBytes(tor); tmpread = bufb.Length; USB.Write(bufb); coffset += tmpread; toread -= tmpread; } Log.Log("Content was sent to Goldleaf."); } else if (ccmd.IsCommandId(CommandId.NSPTicket)) { Log.Log("Sending ticket file..."); PfsFileEntry tik = pnsp.Files[tikidx]; br.BaseStream.Seek(pnsp.HeaderSize + tik.Offset, SeekOrigin.Begin); byte[] file = br.ReadBytes((int)tik.Size); USB.Write(file); Log.Log("Ticket was sent to Goldleaf."); } else if (ccmd.IsCommandId(CommandId.Finish)) { break; } else { USB = null; Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); } } else { USB = null; Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); } } } catch { Error.Log("An error ocurred opening the selected NSP. Are you sure it's a valid NSP?"); } } else if (rc2.IsCommandId(CommandId.Finish)) { USB = null; Error.Log("Goldleaf has canceled the installation."); } else { USB = null; Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); } } else { USB = null; Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); } } else { Error.Log("The dialog was closed without selecting a NSP, or another error ocurred. Reopen Goldleaf and Goldtree and try again."); } } else if (rc.IsCommandId(CommandId.Finish)) { USB = null; Error.Log("Goldleaf has canceled the installation."); } else { USB = null; Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); } } else { USB = null; Error.Log("An invalid command was received. Are you sure Goldleaf is active?"); } } catch { Error.Log("An error ocurred selecting the NSP to be sent."); } Log.Log("The installation has finished. Press ENTER to close Goldtree, or any other key to start another USB installation.", true); ConsoleKeyInfo ki = Console.ReadKey(); if (ki.Key == ConsoleKey.Enter) { if (USB != null) { Command cmd = new Command(CommandId.Finish); USB.Write(cmd); } Environment.Exit(0); } else { goto usbnsp; } }
public bool InitializeDevice() { if (deviceList != null) { deviceList.Free(); } deviceList = null; if (usb != null) { usb.Free(); } usb = null; feature_value_dict.Clear(); feature_value_lookup_dict.Clear(); foreach (Control ctrl in this.Controls) { if (ctrl is ComboBox) { ComboBox box = (ComboBox)ctrl; box.Items.Clear(); } } deviceList = new LstK(KLST_FLAG.NONE); KLST_DEVINFO_HANDLE deviceInfo; DeviceInfo.Text = ""; bool success = false; deviceList.MoveReset(); while (deviceList.MoveNext(out deviceInfo)) { if ( // BSB 20120928 revised test // (deviceInfo.Common.Vid == vendorid1 || deviceInfo.Common.Vid == vendorid2) && // (deviceInfo.Common.Pid == productid1 || deviceInfo.Common.Pid == productid2 || deviceInfo.Common.Pid == productid3 || deviceInfo.Common.Pid == productid4) && // deviceInfo.DeviceInterfaceGUID.ToUpper() == "{D49AB938-53BA-498A-A848-8E2780A4A75F}" (deviceInfo.DeviceInterfaceGUID.ToUpper() == "{D49AB938-53BA-498A-A848-8E2780A4A75F}") && ( ( (deviceInfo.Common.Vid == vendorid1 || deviceInfo.Common.Vid == vendorid2) && (deviceInfo.Common.Pid == productid1 || deviceInfo.Common.Pid == productid2 || deviceInfo.Common.Pid == productid3 || deviceInfo.Common.Pid == productid4) ) || ( (deviceInfo.Common.Vid == audio_vendor_id) && ( (deviceInfo.Common.Pid == audio_product_id_1) || (deviceInfo.Common.Pid == audio_product_id_2) || (deviceInfo.Common.Pid == audio_product_id_3) || (deviceInfo.Common.Pid == audio_product_id_4) || (deviceInfo.Common.Pid == audio_product_id_5) || (deviceInfo.Common.Pid == audio_product_id_6) || (deviceInfo.Common.Pid == audio_product_id_7) || (deviceInfo.Common.Pid == audio_product_id_8) || (deviceInfo.Common.Pid == audio_product_id_9) || (deviceInfo.Common.Pid == audio_product_id_10) || (deviceInfo.Common.Pid == audio_product_id_11) || (deviceInfo.Common.Pid == audio_product_id_12) ) ) ) ) { if (deviceInfo.Connected) { success = true; break; } } } if (!success) { DeviceInfo.Text += String.Format("Audio-Widget device not found!\r\n"); if (deviceList != null) { deviceList.Free(); } deviceList = null; return(false); } usb = new UsbK(deviceInfo); DeviceInfo.Text += String.Format("Opening usb device OK\r\n"); usb.GetDescriptor((byte)USB_DESCRIPTOR_TYPE.DEVICE, 0, 0, globalBuffer, globalBufferLength, out LengthTransferred); USB_DEVICE_DESCRIPTOR deviceDescriptor = ByteToDeviceDescriptor(globalBuffer); usb.GetDescriptor((byte)USB_DESCRIPTOR_TYPE.CONFIGURATION, 0, 0, globalBuffer, globalBufferLength, out LengthTransferred); USB_CONFIGURATION_DESCRIPTOR configurationDescriptor = ByteToConfigDescriptor(globalBuffer); string product = GetStringDescriptor(deviceDescriptor.iProduct); string manufacturer = GetStringDescriptor(deviceDescriptor.iManufacturer); string serial = GetStringDescriptor(deviceDescriptor.iSerialNumber); DeviceInfo.Text += String.Format("Device: VID=0x{0:X04}/PID=0x{1:X04}\r\n", deviceDescriptor.idVendor, deviceDescriptor.idProduct); DeviceInfo.Text += String.Format("Product: {0}\r\n", product); DeviceInfo.Text += String.Format("Manufacturer: {0}\r\n", manufacturer); DeviceInfo.Text += String.Format("Serial number: {0}\r\n", serial); success = SendUsbControl(interfaceNumber, (byte)BMREQUEST_DIR.DEVICE_TO_HOST, (byte)BMREQUEST_TYPE.VENDOR, (byte)BMREQUEST_RECIPIENT.DEVICE, 0x71, 4, 1, globalBuffer, globalBufferLength, out LengthTransferred); ushort max_feature_value_index = globalBuffer[0]; ushort feature_index = 0; ComboBox control = null; for (ushort i = 0; i < max_feature_value_index; i++) { success = SendUsbControl(interfaceNumber, (byte)BMREQUEST_DIR.DEVICE_TO_HOST, (byte)BMREQUEST_TYPE.VENDOR, (byte)BMREQUEST_RECIPIENT.DEVICE, 0x71, 8, i, globalBuffer, globalBufferLength, out LengthTransferred); if (globalBuffer[0] == 63) { break; } string output_str = ""; for (int s = (int)LengthTransferred - 1; s >= 0; s--) { output_str += (char)globalBuffer[s]; } if (output_str == "end") { feature_index++; control = null; } else { if (control == null) { control = FindFeatureControl(feature_index); } if (control != null) { control.Items.Add(output_str); feature_value_dict[(int)i] = output_str; feature_value_lookup_dict[feature_index.ToString() + output_str] = (int)i; } } if (i > 100) { break; } } foreach (Control ctrl in this.Controls) { if (ctrl is ComboBox) { int index = Convert.ToInt32(ctrl.Tag); ComboBox box = (ComboBox)ctrl; success = SendUsbControl(interfaceNumber, (byte)BMREQUEST_DIR.DEVICE_TO_HOST, (byte)BMREQUEST_TYPE.VENDOR, (byte)BMREQUEST_RECIPIENT.DEVICE, 0x71, 4, (byte)(2 + index), globalBuffer, globalBufferLength, out LengthTransferred); int feature_value_index = (int)globalBuffer[0]; if (feature_value_dict.ContainsKey(feature_value_index)) { string text = feature_value_dict[feature_value_index].ToString(); box.SelectedIndex = box.Items.IndexOf(text); } } } return(true); }
public bool ConfigureDevice(out WINUSB_PIPE_INFORMATION pipeInfo, out UsbK usb, out USB_INTERFACE_DESCRIPTOR interfaceDescriptor) { bool success; Console.WriteLine("Finding usb device by VID/PID.. ({0:X4}h / {1:X4}h)", Vid, Pid); // Use a patttern match to include only matching devices. // NOTE: You can use the '*' and '?' chars as wildcards for all chars or a single char (respectively). KLST_PATTERN_MATCH patternMatch = new KLST_PATTERN_MATCH(); if (MI != -1) { patternMatch.DeviceID = String.Format("USB\\VID_{0:X4}&PID_{1:X4}&MI_{2:X2}*", Vid, Pid, MI); } else { patternMatch.DeviceID = String.Format("USB\\VID_{0:X4}&PID_{1:X4}*", Vid, Pid); } LstK deviceList = new LstK(KLST_FLAG.NONE, ref patternMatch); KLST_DEVINFO_HANDLE deviceInfo; interfaceDescriptor = new USB_INTERFACE_DESCRIPTOR(); pipeInfo = new WINUSB_PIPE_INFORMATION(); usb = null; // Iterate the devices looking for a matching alt-interface and endpoint id. while (deviceList.MoveNext(out deviceInfo)) { // libusbK class contructors can throw exceptions; For instance, if the device is // using the WinUsb driver and already in-use by another application. Console.WriteLine("Opening usb device.."); usb = new UsbK(deviceInfo); Console.WriteLine("Finding interface and endpoint by PipeId.."); success = FindPipeAndInterface(usb, out interfaceDescriptor, out pipeInfo); if (success) { break; } Console.WriteLine("PipeId {0:X2}h not found on this device.", PipeId); usb.Free(); usb = null; } if (ReferenceEquals(usb, null)) { Console.WriteLine("Usb device not found: {0}", patternMatch.DeviceID); success = false; goto Done; } MI = interfaceDescriptor.bInterfaceNumber; AltInterfaceId = interfaceDescriptor.bAlternateSetting; PipeId = pipeInfo.PipeId; // Set interface alt setting. Console.WriteLine("Setting interface #{0} to bAlternateSetting #{1}..", interfaceDescriptor.bInterfaceNumber, interfaceDescriptor.bAlternateSetting); success = usb.SetAltInterface(interfaceDescriptor.bInterfaceNumber, false, interfaceDescriptor.bAlternateSetting); if (!success) { Console.WriteLine("SetAltInterface failed. ErrorCode={0:X8}h", Marshal.GetLastWin32Error()); Console.WriteLine(interfaceDescriptor.ToString()); } Done: deviceList.Free(); return(success); }
private void createAndExplictlyDestroyToolStripMenuItem_Click(object sender, EventArgs e) { LstK lstTemp = new LstK(KLST_FLAG.NONE); lstTemp.Dispose(); }
public bool ConfigureDevice(out WINUSB_PIPE_INFORMATION pipeInfo, out UsbK usb, out USB_INTERFACE_DESCRIPTOR interfaceDescriptor) { bool success; Console.WriteLine("Finding usb device by VID/PID.. ({0:X4}h / {1:X4}h)", Vid, Pid); // Use a patttern match to include only matching devices. // NOTE: You can use the '*' and '?' chars as wildcards for all chars or a single char (respectively). KLST_PATTERN_MATCH patternMatch = new KLST_PATTERN_MATCH(); if (MI != -1) patternMatch.DeviceID = String.Format("USB\\VID_{0:X4}&PID_{1:X4}&MI_{2:X2}*", Vid, Pid, MI); else patternMatch.DeviceID = String.Format("USB\\VID_{0:X4}&PID_{1:X4}*", Vid, Pid); LstK deviceList = new LstK(KLST_FLAG.NONE, ref patternMatch); KLST_DEVINFO_HANDLE deviceInfo; interfaceDescriptor = new USB_INTERFACE_DESCRIPTOR(); pipeInfo = new WINUSB_PIPE_INFORMATION(); usb = null; // Iterate the devices looking for a matching alt-interface and endpoint id. while (deviceList.MoveNext(out deviceInfo)) { // libusbK class contructors can throw exceptions; For instance, if the device is // using the WinUsb driver and already in-use by another application. Console.WriteLine("Opening usb device.."); usb = new UsbK(deviceInfo); Console.WriteLine("Finding interface and endpoint by PipeId.."); success = FindPipeAndInterface(usb, out interfaceDescriptor, out pipeInfo); if (success) break; Console.WriteLine("PipeId {0:X2}h not found on this device.", PipeId); usb.Free(); usb = null; } if (ReferenceEquals(usb, null)) { Console.WriteLine("Usb device not found: {0}", patternMatch.DeviceID); success = false; goto Done; } MI = interfaceDescriptor.bInterfaceNumber; AltInterfaceId = interfaceDescriptor.bAlternateSetting; PipeId = pipeInfo.PipeId; // Set interface alt setting. Console.WriteLine("Setting interface #{0} to bAlternateSetting #{1}..", interfaceDescriptor.bInterfaceNumber, interfaceDescriptor.bAlternateSetting); success = usb.SetAltInterface(interfaceDescriptor.bInterfaceNumber, false, interfaceDescriptor.bAlternateSetting); if (!success) { Console.WriteLine("SetAltInterface failed. ErrorCode={0:X8}h", Marshal.GetLastWin32Error()); Console.WriteLine(interfaceDescriptor.ToString()); } Done: deviceList.Free(); return success; }