AVaRICEDebugPackage.UsbDriverHelper.TryCheckDeviceAndInstallDriversInteractively C# (CSharp) Method

TryCheckDeviceAndInstallDriversInteractively() public static method

public static TryCheckDeviceAndInstallDriversInteractively ( UsbIdentity usbIdentities, string universalDriverId ) : bool
usbIdentities UsbIdentity
universalDriverId string
return bool
        public static bool TryCheckDeviceAndInstallDriversInteractively(UsbIdentity[] usbIdentities, string universalDriverId)
        {
            DeviceRecord dev;

            for (;;)
            {
                dev = TryFindDeviceInstance(usbIdentities);
                if (dev != null)
                    break;

                string msg;
                if (usbIdentities.Length == 1)
                    msg = string.Format("Cannot find a USB device with the following VID/PID: {0}. Please check that your JTAG/SWD debugger is connected.", usbIdentities[0]);
                else
                {
                    StringBuilder msgBuilder = new StringBuilder("Cannot find a USB device with one of the following VID/PIDs:\r\n");
                    foreach (var id in usbIdentities)
                        msgBuilder.AppendLine("    " + id.ToString());

                    msgBuilder.Append("Please check that your JTAG/SWD debugger is connected.");
                    msg = msgBuilder.ToString();
                }

                switch (MessageBox.Show(msg, "VisualGDB", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning))
                {
                    case DialogResult.Retry:
                        continue;
                    case DialogResult.Ignore:
                        return true;
                    case DialogResult.Abort:
                    default:
                        return false;
                }
            }

            if (!string.IsNullOrEmpty(universalDriverId))
            {
                List<UniversalUsbDriver> universalDrivers = new List<UniversalUsbDriver>();
                try
                {
                    UniversalUsbDriver.LoadFromDriversSubdirectory(universalDrivers);
                }
                catch { }

                UniversalUsbDriver requiredDriver = null;

                foreach (var drv in universalDrivers)
                    if (drv.UniqueDriverId == universalDriverId)
                    {
                        requiredDriver = drv;
                        break;
                    }

                if (requiredDriver == null || string.IsNullOrEmpty(requiredDriver.UniversalDriverName))
                    return MessageBox.Show("Warning: cannot find a universal driver " + universalDriverId + ". Continue anyway?", "VisualGDB", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes;

                bool driverMissing;
                if (requiredDriver.FixedDriver != null && requiredDriver.FixedDriver.DeviceNameRegex != null)
                {
                    try
                    {
                        var rg = new Regex(requiredDriver.FixedDriver.DeviceNameRegex);
                        driverMissing = dev.Device.UserFriendlyName != null && !rg.IsMatch(dev.Device.UserFriendlyName);
                    }
                    catch
                    {
                        driverMissing = false;
                    }
                }
                else
                    driverMissing = dev.Device.UserFriendlyName != null && !dev.Device.UserFriendlyName.EndsWith("(" + requiredDriver.UniversalDriverName + ")");

                if (driverMissing)
                    if (MessageBox.Show(string.Format("\"{0}\" does not appear to have \"{1}\" driver installed. AVaRICE may have problems finding your device. Try installing it now?", dev.Device.UserFriendlyName, requiredDriver.UniversalDriverName), "VisualGDB", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        UsbDriverInstallProgressForm.DoInstallDriverWithProgress(dev, requiredDriver);

                return true;
            }

            return true;
        }

Usage Example

Example #1
0
        private void btnTest_Click(object sender, EventArgs e)
        {
            var adapterCommand = (cbAdapter.SelectedItem as PropertyEntry.Enumerated.Suggestion)?.InternalValue;

            UsbDriverHelper.UsbIdentity id = new UsbDriverHelper.UsbIdentity {
                VID = "03eb"
            };
            if (adapterCommand != null)
            {
                switch (adapterCommand)
                {
                case "-2":
                    id.PID = "2103";
                    break;

                case "-g":
                    id.PID = "2107";
                    break;
                }
            }

            if (id.PID != null)
            {
                if (!UsbDriverHelper.TryCheckDeviceAndInstallDriversInteractively(new UsbDriverHelper.UsbIdentity[] { id }, "com.sysprogs.libusb.mini"))
                {
                    return;
                }
            }

            Process process = new Process();

            process.StartInfo.FileName         = _ToolchainBin + @"\avarice.exe";
            process.StartInfo.WorkingDirectory = _ToolchainBin;
            var cfg = Configuration;

            cfg.Remove("com.sysprogs.avr.avarice.erase");
            cfg.Remove("com.sysprogs.avr.avarice.program");
            cfg.Remove("com.sysprogs.avr.avarice.verify");
            cfg.Remove("com.sysprogs.avr.avarice.port");

            process.StartInfo.Arguments = CommandLineTools.BuildCommandLine(_MyMethod.GDBServerArguments, new Dictionary <string, string>(), cfg).Replace(_MyMethod.GDBServerArguments.GNUArgumentPrefix, "") + " -l";

            using (var frm = new CommandTestForm(process))
            {
                frm.ShowDialog();
            }
        }