private void LookForPort(APFirmware.MAV_TYPE mavtype, bool alloptions = false)
        {
            var ports = Win32DeviceMgmt.GetAllCOMPorts();

            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;
                }
                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;
        }
Exemple #2
0
        private async Task LookForPort(APFirmware.MAV_TYPE mavtype)
        {
            var ports = await Test.UsbDevices.GetDeviceInfoList().ConfigureAwait(false);

            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)
                {
                    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 (fwitems?.Count == 1)
                    {
                        baseurl = fwitems[0].Url.ToString();

                        await DownloadAndUpload(baseurl).ConfigureAwait(false);
                    }
                    else if (fwitems?.Count > 0)
                    {
                        FirmwareSelection fws = new FirmwareSelection(fwitems, deviceInfo);
                        fws.CloseAction += async() =>
                        {
                            Navigation.PopModalAsync();
                            baseurl = fws.FinalResult;

                            await DownloadAndUpload(baseurl).ConfigureAwait(false);

                            return;
                        };
                        await this.Navigation.PushModalAsync(fws).ConfigureAwait(false);

                        Debug.WriteLine(fws.FinalResult);
                        baseurl = fws.FinalResult;
                        if (fws.FinalResult == null)
                        {
                            // user canceled
                            return;
                        }
                    }
                    else
                    {
                        CustomMessageBox.Show(Strings.No_firmware_available_for_this_board, Strings.ERROR);
                        return;
                    }

                    return;
                }
                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;
        }