private void findfirmware(Firmware.software fwtoupload)
        {
            var dr = CustomMessageBox.Show(Strings.AreYouSureYouWantToUpload + fwtoupload.name + Strings.QuestionMark,
                                           Strings.Continue, MessageBoxButtons.YesNo);

            if (dr == (int)DialogResult.Yes)
            {
                try
                {
                    MainV2.comPort.BaseStream.Close();
                }
                catch
                {
                }
                fw.Progress -= fw_ProgressPDR;
                fw.Progress += fw_Progress1;

                var history = (CMB_history.SelectedValue == null) ? "" : CMB_history.SelectedValue.ToString();

                if (history != "")
                {
                    foreach (var propertyInfo in fwtoupload.GetType().GetFields())
                    {
                        try
                        {
                            if (propertyInfo.Name.Contains("url"))
                            {
                                var oldurl = propertyInfo.GetValue(fwtoupload).ToString();
                                if (oldurl == "")
                                {
                                    continue;
                                }
                                var newurl = Firmware.getUrl(history, oldurl);
                                propertyInfo.SetValue(fwtoupload, newurl);
                            }
                        }
                        catch { }
                    }

                    //history = "";
                }

                var ports = Win32DeviceMgmt.GetAllCOMPorts();
                ports.AddRange(Linux.GetAllCOMPorts());

                if (ExtraDeviceInfo != null)
                {
                    try
                    {
                        ports.AddRange(ExtraDeviceInfo.Invoke());
                    }
                    catch
                    {
                    }
                }

                var updated = fw.update(MainV2.comPortName, fwtoupload, history, ports);

                if (updated)
                {
                    if (fwtoupload.url2560_2 != null && fwtoupload.url2560_2.ToLower().Contains("copter") &&
                        fwtoupload.name.ToLower().Contains("3.1"))
                    {
                        CustomMessageBox.Show(Strings.WarningAC31, Strings.Warning);
                    }

                    if (fwtoupload.url2560_2 != null && fwtoupload.url2560_2.ToLower().Contains("copter") &&
                        fwtoupload.name.ToLower().Contains("3.2"))
                    {
                        CustomMessageBox.Show(Strings.WarningAC32, Strings.Warning);
                    }
                }
                else
                {
                    CustomMessageBox.Show(Strings.ErrorUploadingFirmware, Strings.ERROR);
                }
            }
        }
        //Load custom firmware (old CTRL+C shortcut)
        private void Custom_firmware_label_Click(object sender, EventArgs e)
        {
            using (var fd = new OpenFileDialog
            {
                Filter = "Firmware (*.hex;*.px4;*.vrx;*.apj)|*.hex;*.px4;*.vrx;*.apj|All files (*.*)|*.*"
            })
            {
                if (Directory.Exists(custom_fw_dir))
                {
                    fd.InitialDirectory = custom_fw_dir;
                }
                fd.ShowDialog();
                if (File.Exists(fd.FileName))
                {
                    custom_fw_dir = Path.GetDirectoryName(fd.FileName);
                    Settings.Instance["FirmwareFileDirectory"] = custom_fw_dir;

                    fw.Progress -= fw_ProgressPDR;
                    fw.Progress += fw_Progress1;

                    var boardtype = BoardDetect.boards.none;
                    try
                    {
                        if (fd.FileName.ToLower().EndsWith(".px4") || fd.FileName.ToLower().EndsWith(".apj"))
                        {
                            if (solo.Solo.is_solo_alive &&
                                CustomMessageBox.Show("Solo", "Is this a Solo?",
                                                      CustomMessageBox.MessageBoxButtons.YesNo) == CustomMessageBox.DialogResult.Yes)
                            {
                                boardtype = BoardDetect.boards.solo;
                            }
                            else
                            {
                                boardtype = BoardDetect.boards.px4v2;
                            }
                        }
                        else
                        {
                            var ports = Win32DeviceMgmt.GetAllCOMPorts();
                            ports.AddRange(Linux.GetAllCOMPorts());

                            if (ExtraDeviceInfo != null)
                            {
                                try
                                {
                                    ports.AddRange(ExtraDeviceInfo.Invoke());
                                }
                                catch
                                {
                                }
                            }

                            boardtype = BoardDetect.DetectBoard(MainV2.comPortName, ports);
                        }

                        if (boardtype == BoardDetect.boards.none)
                        {
                            CustomMessageBox.Show(Strings.CantDetectBoardVersion);
                            return;
                        }
                    }
                    catch
                    {
                        CustomMessageBox.Show(Strings.CanNotConnectToComPortAnd, Strings.ERROR);
                        return;
                    }

                    try
                    {
                        fw.UploadFlash(MainV2.comPortName, fd.FileName, boardtype);
                    }
                    catch (Exception ex)
                    {
                        CustomMessageBox.Show(ex.ToString(), Strings.ERROR);
                    }
                }
            }
        }
Exemple #3
0
        private void LookForPort(APFirmware.MAV_TYPE mavtype, bool alloptions = false)
        {
            var ports = Win32DeviceMgmt.GetAllCOMPorts();

            ports.AddRange(Linux.GetAllCOMPorts());

            if (ExtraDeviceInfo != null)
            {
                try
                {
                    ports.AddRange(ExtraDeviceInfo.Invoke());
                }
                catch
                {
                }
            }

            if (alloptions)
            {
                ports.Add(default(ArduPilot.DeviceInfo));
            }

            foreach (var deviceInfo in ports)
            {
                long?devid = detectedboardid;

                // make best guess at board_id based on usb info
                if (!devid.HasValue)
                {
                    devid = APFirmware.GetBoardID(deviceInfo);
                }

                if (devid.HasValue && devid.Value != 0 || alloptions == true)
                {
                    log.InfoFormat("{0}: {1} - {2}", deviceInfo.name, deviceInfo.description, deviceInfo.board);

                    var baseurl = "";

                    // get the options for this device
                    var fwitems = APFirmware.Manifest.Firmware.Where(a =>
                                                                     a.BoardId == devid && a.MavType == mavtype.ToString() &&
                                                                     a.MavFirmwareVersionType == REL_Type.ToString()).ToList();

                    if (alloptions)
                    {
                        fwitems = APFirmware.Manifest.Firmware.ToList();
                    }

                    if (fwitems?.Count == 1)
                    {
                        baseurl = fwitems[0].Url.ToString();
                    }
                    else if (fwitems?.Count > 0)
                    {
                        FirmwareSelection fws = new FirmwareSelection(fwitems, deviceInfo);
                        fws.ShowXamarinControl(550, 400);
                        baseurl = fws.FinalResult;
                        if (fws.FinalResult == null)
                        {
                            // user canceled
                            return;
                        }
                    }
                    else
                    {
                        CustomMessageBox.Show(Strings.No_firmware_available_for_this_board, Strings.ERROR);
                    }

                    var tempfile = Path.GetTempFileName();
                    try
                    {
                        // update to use mirror url
                        log.Info("Using " + baseurl);

                        var starttime = DateTime.Now;

                        // Create a request using a URL that can receive a post.
                        WebRequest request = WebRequest.Create(baseurl);
                        if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
                        {
                            ((HttpWebRequest)request).UserAgent = Settings.Instance.UserAgent;
                        }
                        request.Timeout = 10000;
                        // Set the Method property of the request to POST.
                        request.Method = "GET";
                        // Get the request stream.
                        Stream dataStream; //= request.GetRequestStream();
                                           // Get the response (using statement is exception safe)
                        using (WebResponse response = request.GetResponse())
                        {
                            // Display the status.
                            log.Info(((HttpWebResponse)response).StatusDescription);
                            // Get the stream containing content returned by the server.
                            using (dataStream = response.GetResponseStream())
                            {
                                long bytes   = response.ContentLength;
                                long contlen = bytes;

                                byte[] buf1 = new byte[1024];

                                using (FileStream fs = new FileStream(tempfile, FileMode.Create))
                                {
                                    fw_Progress1(0, Strings.DownloadingFromInternet);

                                    long length   = response.ContentLength;
                                    long progress = 0;
                                    dataStream.ReadTimeout = 30000;

                                    while (dataStream.CanRead)
                                    {
                                        try
                                        {
                                            fw_Progress1(length == 0 ? 50 : (int)((progress * 100) / length), Strings.DownloadingFromInternet);
                                        }
                                        catch
                                        {
                                        }
                                        int len = dataStream.Read(buf1, 0, 1024);
                                        if (len == 0)
                                        {
                                            break;
                                        }
                                        progress += len;
                                        bytes    -= len;
                                        fs.Write(buf1, 0, len);
                                    }

                                    fs.Close();
                                }
                                dataStream.Close();
                            }
                            response.Close();
                        }

                        var timetook = (DateTime.Now - starttime).TotalMilliseconds;

                        Tracking.AddTiming("Firmware Download", deviceInfo.board, timetook, deviceInfo.description);

                        fw_Progress1(100, Strings.DownloadedFromInternet);
                        log.Info("Downloaded");
                    }
                    catch
                    {
                        CustomMessageBox.Show(Strings.FailedDownload, Strings.ERROR);
                        return;
                    }

                    MissionPlanner.Utilities.Tracking.AddFW(mavtype.ToString(), deviceInfo.board);

                    var fw = new Firmware();
                    fw.Progress += fw_Progress1;

                    var uploadstarttime = DateTime.Now;

                    flashdone = true;

                    fw.UploadFlash(deviceInfo.name, tempfile, BoardDetect.boards.pass);

                    var uploadtime = (DateTime.Now - uploadstarttime).TotalMilliseconds;

                    Tracking.AddTiming("Firmware Upload", deviceInfo.board, uploadtime, deviceInfo.description);

                    return;
                }

                /*
                 * // allow scanning multiple ports
                 * else
                 * {
                 *  CustomMessageBox.Show("Failed to discover board id. Please reconnect via usb and try again.", Strings.ERROR);
                 *  return;
                 * }
                 */
            }

            CustomMessageBox.Show("Failed to detect port to upload to", Strings.ERROR);
            return;
        }