Example #1
0
        private void FwUpgradeTimer_Tick_1(object sender, EventArgs e)
        {
            try
            {
                int    nProgress;
                int    result = 0;
                IntPtr ch_cpXml;
                byte[] _bTemp = new byte[1024];
                Array.Clear(_bTemp, 0, 1024);
                ch_cpXml = Marshal.AllocHGlobal(1024);
                Marshal.Copy(_bTemp, 0, ch_cpXml, 1024);
                string str_cpMethod     = "GET";
                string str_cpRequestCGI = "/FW_Upgrade.cgi?cmd=fw_upgrade_status";

                Console.WriteLine("Address of ch_cpXml is " + ch_cpXml);
                result = SdkShellGetHttpRequestXML(ptr, str_cpMethod, str_cpRequestCGI, ch_cpXml);
                if (result < 0)
                {
                    MessageBox.Show(Enum.Parse(typeof(ErrorMessage), result.ToString()).ToString());
                    Marshal.FreeHGlobal(ch_cpXml);
                    _bTemp = null;
                    this.Close();
                    return;
                }
                Marshal.Copy(ch_cpXml, _bTemp, 0, 1024);
                string str_getXml = System.Text.Encoding.Default.GetString(_bTemp);
                XmlDoc.LoadXml(str_getXml);
                string strValue = XmlDoc.GetElementsByTagName("result").Item(0).InnerXml;
                nProgress = Convert.ToInt32(strValue);
                Marshal.FreeHGlobal(ch_cpXml);
                _bTemp = null;

                if (nProgress >= 100) // upgrade finish
                {
                    pgsBarFw.Value   = 100;
                    txtFirmware.Text = "Upgrade 100%";

                    FwUpgradeTimer.Stop();

                    MessageBox.Show("Upgrade succeeded!\r\nThe DVR will reboot. Please reconnect it latter.");
                    panelPgsFw.Visible = false;
                }
                else if (nProgress < 0)
                {
                    FwUpgradeTimer.Stop();

                    MessageBox.Show("Upgrade failed!");
                    panelPgsFw.Visible = false;
                }
                else
                {
                    pgsBarFw.Value   = nProgress;
                    txtFirmware.Text = "Upgrade " + nProgress + "%";
                }
            }
            catch (Exception ex)
            {
                FwUpgradeTimer.Stop();

                MessageBox.Show(ex.Message, "Upgrade firmware error(timer).");
            }
        }
Example #2
0
        private void btnFwUpgrade_Click(object sender, EventArgs e)
        {
            if (edFwFilePath.Text != "")
            {
                try
                {
                    int    result = 0;
                    IntPtr ch_cpXml;
                    byte[] _bTemp = new byte[1024];
                    Array.Clear(_bTemp, 0, 1024);
                    ch_cpXml = Marshal.AllocHGlobal(1024);
                    Marshal.Copy(_bTemp, 0, ch_cpXml, 1024);

                    string str_cpMethod     = "POST";
                    string str_cpRequestCGI = "/FW_Upgrade.cgi";
                    string str_FileName     = edFwFilePath.Text;
                    string str_getXml;

                    //added by Edward in 20120827 for binary file exam
                    result = SdkShellFWExam(ptr, str_FileName);
                    if (result != 0)
                    {
                        MessageBox.Show(Enum.Parse(typeof(ErrorMessage), result.ToString()).ToString());
                        Marshal.FreeHGlobal(ch_cpXml);
                        _bTemp = null;
                        this.Close();
                        return;
                    }
                    result = SdkShellPostFile(ptr, str_cpMethod, str_cpRequestCGI, ch_cpXml, str_FileName);
                    if (result < 0)
                    {
                        MessageBox.Show(Enum.Parse(typeof(ErrorMessage), result.ToString()).ToString());
                        Marshal.FreeHGlobal(ch_cpXml);
                        _bTemp = null;
                        this.Close();
                        return;
                    }
                    Marshal.Copy(ch_cpXml, _bTemp, 0, 1024);
                    str_getXml         = System.Text.Encoding.Default.GetString(_bTemp);
                    pgsBarFw.Value     = 0;
                    txtFirmware.Text   = "Upgrade 0%";
                    panelPgsFw.Visible = true;

                    //Updata error message
                    for (int r = -1; r >= -12; r--)
                    {
                        if (Regex.IsMatch(str_getXml, "<result>" + r + "</result>") == true)
                        {
                            MessageBox.Show(Enum.Parse(typeof(UpdateErrorMessage), r.ToString()).ToString());
                            return;
                        }
                    }

                    // If the upload file is valid, DVR will response "<script>parent.startFwUpload();</script>\n",
                    // and you can start to ask the upload/upgrade status.
                    // Else, response "<script>parent.fwUploadFail();</script>\n".

                    if (Regex.IsMatch(str_getXml, @"parent.startFwUpload") == true)
                    {
                        FwUpgradeTimer.Start();
                    }
                    else
                    {
                        panelPgsFw.Visible = false;
                        MessageBox.Show("File submit failed.");
                    }

                    Marshal.FreeHGlobal(ch_cpXml);
                    _bTemp = null;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Upgrade firmware error.");
                    FwUpgradeTimer.Stop();
                    return;
                }
            }
            else
            {
                MessageBox.Show("Please select a file.");
            }
        }