private void add_Click(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
            if (commandWindow is null)
            {
                commandWindow = new commandWindow();
            }
            else
            {
                commandWindow = new commandWindow(commandWindow);
            }
            commandWindow.ShowDialog();


            if (commandWindow.addCommand)
            {
                try
                {
                    utils.command c = createCommand();
                    script.commands.Add(c);
                    this.commands.Items.Add(c.name);
                }
                catch
                {
                    MessageBox.Show(utils.ErrorMessages.unexpected_error, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
                    add_Click(sender, e);
                }
            }
            this.IsEnabled = true;
        }
        private void edit_Click(object sender, RoutedEventArgs e)
        {
            int index = this.commands.SelectedIndex;

            if (index < 0)
            {
                return;
            }

            this.IsEnabled = false;

            commandWindow = new commandWindow(script.commands[index]);

            commandWindow.ShowDialog();


            if (commandWindow.addCommand)
            {
                try
                {
                    utils.command c = createCommand();
                    script.commands[index]     = c;
                    this.commands.Items[index] = c.name;
                }
                catch
                {
                    MessageBox.Show(utils.ErrorMessages.unexpected_error, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
                    edit_Click(sender, e);
                }
            }
            this.IsEnabled = true;
        }
Example #3
0
        public commandWindow(utils.command c)
        {
            InitializeComponent();
            addCommand = false;
            if (this.algorithm.Text == "")
            {
                this.algorithm.Items.Clear();
                this.algorithm.Items.Add("Nenhum");
                this.algorithm.Items.Add("CRC");
                this.algorithm.Items.Add("Checksum");
            }
            this.send.Text    = c.send;
            this.name.Text    = c.name;
            this.receive.Text = c.receive;

            this.algorithm.Text            = c.checksum.algorithm;
            this.from.Text                 = c.checksum.from.ToString();
            this.polynominal.Text          = c.checksum.polynominal.ToString("x");
            this.initialValue.Text         = c.checksum.initialValue.ToString("x");
            this.finalXorVal.Text          = c.checksum.finalXorVal.ToString("x");
            this.outputReflected.IsChecked = c.checksum.resultReflected;
            this.inputReflected.IsChecked  = c.checksum.inputReflected;
            this.lowBitFirst.IsChecked     = c.checksum.lowBitFirst;
            if (c.send_script != "")
            {
                this.send_external_script.Content = c.send_script;
            }
            else
            {
                this.send_external_script.Content = "Nenhum Arquivo Selecionado";
            }
            if (c.receive_script != "")
            {
                this.receive_external_script.Content = c.receive_script;
            }
            else
            {
                this.receive_external_script.Content = "Nenhum Arquivo Selecionado";
            }


            switch (c.checksum.width)
            {
            case 8:
                this.w8.IsChecked = true;
                break;

            case 16:
                this.w16.IsChecked = true;
                break;

            case 32:
                this.w32.IsChecked = true;
                break;

            case 64:
                this.w64.IsChecked = true;
                break;
            }
        }
        private utils.command createCommand()
        {
            utils.command c = new utils.command();
            c.name    = commandWindow.name.Text;
            c.send    = commandWindow.send.Text;
            c.receive = commandWindow.receive.Text;
            if (commandWindow.receive_external_script.Content.ToString() != "Nenhum Arquivo Selecionado")
            {
                c.receive_script = commandWindow.receive_external_script.Content.ToString();
            }
            else
            {
                c.receive_script = "";
            }
            if (commandWindow.send_external_script.Content.ToString() != "Nenhum Arquivo Selecionado")
            {
                c.send_script = commandWindow.send_external_script.Content.ToString();
            }
            else
            {
                c.send_script = "";
            }

            c.checksum           = new crc();
            c.checksum.algorithm = commandWindow.algorithm.SelectedItem.ToString();
            if (c.checksum.algorithm.Equals("CRC"))
            {
                c.checksum.initialValue    = UInt64.Parse(commandWindow.initialValue.Text, System.Globalization.NumberStyles.HexNumber);
                c.checksum.finalXorVal     = UInt64.Parse(commandWindow.finalXorVal.Text, System.Globalization.NumberStyles.HexNumber);
                c.checksum.polynominal     = UInt64.Parse(commandWindow.polynominal.Text, System.Globalization.NumberStyles.HexNumber);
                c.checksum.inputReflected  = commandWindow.inputReflected.IsChecked == true;
                c.checksum.resultReflected = commandWindow.outputReflected.IsChecked == true;
                c.checksum.from            = int.Parse(commandWindow.from.Text, System.Globalization.NumberStyles.HexNumber);
                c.checksum.lowBitFirst     = commandWindow.lowBitFirst.IsChecked == true;
                if (commandWindow.w8.IsChecked == true)
                {
                    c.checksum.width = 8;
                }
                else if (commandWindow.w16.IsChecked == true)
                {
                    c.checksum.width = 16;
                }
                else if (commandWindow.w32.IsChecked == true)
                {
                    c.checksum.width = 32;
                }
                else if (commandWindow.w64.IsChecked == true)
                {
                    c.checksum.width = 64;
                }
            }
            else if (c.checksum.algorithm.Equals("Checksum"))
            {
                c.checksum.initialValue    = 0;
                c.checksum.finalXorVal     = 0;
                c.checksum.polynominal     = 0;
                c.checksum.inputReflected  = false;
                c.checksum.resultReflected = false;
                c.checksum.from            = int.Parse(commandWindow.from.Text, System.Globalization.NumberStyles.HexNumber);
                c.checksum.lowBitFirst     = commandWindow.lowBitFirst.IsChecked == true;
                if (commandWindow.w8.IsChecked == true)
                {
                    c.checksum.width = 8;
                }
                else if (commandWindow.w16.IsChecked == true)
                {
                    c.checksum.width = 16;
                }
                else if (commandWindow.w32.IsChecked == true)
                {
                    c.checksum.width = 32;
                }
                else if (commandWindow.w64.IsChecked == true)
                {
                    c.checksum.width = 64;
                }
            }
            return(c);
        }
        private void RunScript(List <utils.command> command_list, int timeOut)
        {
            List <utils.test_results> _test = new List <utils.test_results>();

            utils.test_results commandOnTest = new utils.test_results();
            for (int i = 0; i < command_list.Count; i++)
            {
                if (!scriptRunning)
                {
                    return;
                }

                receive = "";

                utils.command c = command_list[i];
                htmlTextOutput.Add($"<p><b>[{i}] - {c.name}</b></p>");

                for (int j = 0; j < replacer.Count; j++)
                {
                    c.send    = c.send.ToUpper().Replace($"#{tags[j]}#", utils.getFrameFormat(replacer[j], false));
                    c.receive = c.receive.ToUpper().Replace($"#{tags[j]}#", utils.getFrameFormat(replacer[j], false));
                }
                commandOnTest.sent.raw       = c.send;
                commandOnTest.expectedAnswer = c.receive;
                if (c.send_script == null)
                {
                    c.send_script = "";
                }

                if (c.send_script.ToLower().IndexOf("js") < 0)
                {
                    if (commandOnTest.sent.raw.IndexOf("#CS#") > 0)
                    {
                        commandOnTest.sent.raw = commandOnTest.sent.raw
                                                 .Replace("#CS#",
                                                          c.checksum.calcCRC(commandOnTest.sent.raw.Substring
                                                                                 (c.checksum.from, commandOnTest.sent.raw.IndexOf("#CS#") - c.checksum.from)));
                    }
                    commandOnTest.sent.processed = commandOnTest.sent.raw;
                    byte[] b = utils.str2Hex(commandOnTest.sent.processed);
                    receive = "";
                    s0.Write(b, 0, b.Length);
                    this.screenText.Dispatcher.Invoke((Action)(() => write2Screen(commandOnTest.sent.processed)));
                    Thread.Sleep(timeOut);
                    commandOnTest.realAnswer.raw = receive;
                    if (c.receive_script == null)
                    {
                        c.receive_script = "";
                    }
                    if (Receive2Processed(c.receive_script, ref commandOnTest.realAnswer))
                    {
                        return;
                    }
                    if (!String.IsNullOrWhiteSpace(commandOnTest.realAnswer.processed))
                    {
                        this.Dispatcher.Invoke((Action)(() => read2Screen(commandOnTest.realAnswer.processed)));
                    }
                }
                else
                {
                    utils.ScriptAnswer sa = new utils.ScriptAnswer();
                    sa.newRound = true;
                    int round = 0;
                    commandOnTest.realAnswer.raw = "";
                    while (sa.newRound)
                    {
                        if (!scriptRunning)
                        {
                            return;
                        }

                        sa = utils.callScript(c.send_script, commandOnTest.sent.raw, commandOnTest.realAnswer.raw, round);

                        if (!String.IsNullOrWhiteSpace(sa.error))
                        {
                            MessageBox.Show($"{sa.error}", "Error ao executar o script", MessageBoxButton.OK, MessageBoxImage.Error);
                            scriptRunning = false;
                            this.screenText.Dispatcher.Invoke((Action)(() => updateScreenButtons(state.connected)));
                            return;
                        }
                        else
                        {
                            commandOnTest.sent.processed = sa.processed_frame;
                            if (!String.IsNullOrWhiteSpace(commandOnTest.sent.processed))
                            {
                                if (commandOnTest.sent.processed.IndexOf("#CS#") > 0)
                                {
                                    commandOnTest.sent.processed = commandOnTest.sent.processed
                                                                   .Replace("#CS#",
                                                                            c.checksum.calcCRC(commandOnTest.sent.processed.Substring
                                                                                                   (c.checksum.from, commandOnTest.sent.processed.IndexOf("#CS#") - c.checksum.from)));
                                }
                                byte[] b = utils.str2Hex(commandOnTest.sent.processed);
                                receive = "";
                                s0.Write(b, 0, b.Length);
                                this.screenText.Dispatcher.Invoke((Action)(() => write2Screen(commandOnTest.sent.processed)));
                                Thread.Sleep(timeOut);
                                commandOnTest.realAnswer.raw = receive;
                                if (c.receive_script == null)
                                {
                                    c.receive_script = "";
                                }
                                if (Receive2Processed(c.receive_script, ref commandOnTest.realAnswer))
                                {
                                    return;
                                }
                                if (!String.IsNullOrWhiteSpace(commandOnTest.realAnswer.processed))
                                {
                                    this.Dispatcher.Invoke((Action)(() => read2Screen(commandOnTest.realAnswer.processed)));
                                }
                                round++;
                            }
                        }
                    }
                }
                try
                {
                    commandOnTest.expectedAnswer = commandOnTest.expectedAnswer.Replace(" ", "");
                    string ans = "";
                    if (commandOnTest.realAnswer.processed != null)
                    {
                        ans = commandOnTest.realAnswer.processed.Replace(" ", "");
                    }
                    for (int ch = 0; ch < commandOnTest.expectedAnswer.Length; ch++)
                    {
                        if (commandOnTest.expectedAnswer[ch] == 'X')
                        {
                            char[] array = commandOnTest.expectedAnswer.ToCharArray();
                            array[ch] = ans[ch];
                            commandOnTest.expectedAnswer = new string(array);
                        }
                    }
                    if (commandOnTest.expectedAnswer.IndexOf("#CS#") > 0)
                    {
                        commandOnTest.expectedAnswer = commandOnTest.expectedAnswer
                                                       .Replace("#CS#",
                                                                c.checksum.calcCRC(
                                                                    commandOnTest.expectedAnswer.Substring(c.checksum.from,
                                                                                                           commandOnTest.expectedAnswer.IndexOf("#CS#") - c.checksum.from))).ToUpper();
                        Console.WriteLine(commandOnTest.expectedAnswer.Length);
                    }
                    commandOnTest.evaluation = commandOnTest.expectedAnswer.Replace(" ", "").Equals(ans);
                }
                catch
                {
                    commandOnTest.evaluation = false;
                }

                if (!String.IsNullOrWhiteSpace(commandOnTest.expectedAnswer))
                {
                    total_evaluation &= commandOnTest.evaluation;
                }

                _test.Add(commandOnTest);

                this.Dispatcher.Invoke((Action)(() =>
                {
                    if (total_evaluation)
                    {
                        this.partial_result.Background = Brushes.Green;
                    }
                    else
                    {
                        this.partial_result.Background = Brushes.Red;
                    }
                }));
            }

            scriptRunning = false;
            htmlTextOutput.Add("<br><br><h3>Análise automática dos resultados:</h3>");
            htmlTextOutput.Add("<table  class=\"table table-striped\">");
            htmlTextOutput.Add("<tr>");
            htmlTextOutput.Add($"<th>ID</th>");
            htmlTextOutput.Add($"<th>Resultado</th>");
            htmlTextOutput.Add("</tr>");
            int id = 0;

            foreach (utils.test_results r in _test)
            {
                string eval = "";
                if (r.expectedAnswer == "")
                {
                    eval = "Análise manual";
                }
                else
                {
                    eval = r.evaluation ? "Aprovado" : "Reprovado";
                }

                htmlTextOutput.Add("<tr>");
                htmlTextOutput.Add($"<td>{id}</td>");
                htmlTextOutput.Add($"<td>{eval}</td>");
                htmlTextOutput.Add("</tr>");
                id++;
            }
            htmlTextOutput.Add("</table>");
            this.screenText.Dispatcher.Invoke((Action)(() => updateScreenButtons(state.connected)));
        }
        private void send_Click(object sender, RoutedEventArgs e)
        {
            if (this.commands.SelectedItems.Count == 0)
            {
                return;
            }

            List <utils.command> c_list     = new List <utils.command>();
            List <string>        ActiveTags = new List <string>();

            for (int i = 0; i < this.commands.SelectedItems.Count; i++)
            {
                // Import command information
                utils.command c = script.commands[this.commands.Items.IndexOf(this.commands.SelectedItems[i])];

                for (int id = 0; id < c.send.Length; id++)
                {
                    if (c.send[id].Equals('#'))
                    {
                        string flag   = c.send.Substring(id + 1).ToUpper();
                        int    new_id = flag.IndexOf('#');
                        flag = flag.Substring(0, new_id);
                        id  += new_id + 1;

                        if (flag != "CS")
                        {
                            if (!tags.Contains(flag))
                            {
                                ActiveTags.Add(flag);
                                tags.Add(flag);
                            }
                            else
                            {
                                ActiveTags.Add(flag);
                            }
                        }
                    }
                    else if (c.send[id].Equals(' '))
                    {
                        c.send.Remove(id, 1);
                    }
                }
                for (int id = 0; id < c.receive.Length; id++)
                {
                    if (c.receive[id].Equals('#'))
                    {
                        string flag   = c.receive.Substring(id + 1).ToUpper();
                        int    new_id = flag.IndexOf('#');
                        flag = flag.Substring(0, new_id);
                        id  += new_id + 1;
                        if (flag != "CS")
                        {
                            if (!tags.Contains(flag))
                            {
                                ActiveTags.Add(flag);
                                tags.Add(flag);
                            }
                            else
                            {
                                ActiveTags.Add(flag);
                            }
                        }
                    }
                    else if (c.receive[id].Equals(' '))
                    {
                        c.receive.Remove(id, 1);
                    }
                }
                c_list.Add(c);
            }

            if (ActiveTags.Count > 0)
            {
                int i = 0;
                this.IsEnabled = false;
                ReplaceStrings replace = new ReplaceStrings();

                foreach (string str in tags)
                {
                    if (ActiveTags.Contains(str))
                    {
                        string TAG     = "";
                        int    maxSize = 256;
                        if (str.IndexOf(",") != -1)
                        {
                            int len = str.IndexOf(",");
                            Tag     = str.Substring(0, len).Replace("#", "");
                            maxSize = int.Parse(str.Substring(len + 1).Replace("#", "").Trim()) * 2;
                        }
                        else
                        {
                            Tag = str.Replace("#", "");
                        }
                        replace.stack.Children.Add(new Label {
                            Content = Tag, Margin = new Thickness(10, 0, 10, 0), FontWeight = FontWeights.Normal
                        });
                        if (i < replacer.Count)
                        {
                            replace.stack.Children.Add(new TextBox
                            {
                                Margin        = new Thickness(10, 0, 10, 10),
                                FontWeight    = FontWeights.Normal,
                                TextAlignment = TextAlignment.Left,
                                Text          = replacer[i],
                                MaxLength     = maxSize
                            });
                        }
                        else
                        {
                            replace.stack.Children.Add(new TextBox
                            {
                                Margin        = new Thickness(10, 0, 10, 10),
                                FontWeight    = FontWeights.Normal,
                                TextAlignment = TextAlignment.Left,
                                MaxLength     = maxSize
                            });
                        }

                        i++;
                    }
                }

                replace.ShowDialog();
                this.IsEnabled = true;
                if (replace.flag)
                {
                    replacer = replace.textValues;
                }
                else
                {
                    return;
                }

                replace.Close();
            }

            scriptRunning = true;
            Thread.Sleep(100);
            clean_Click(sender, e);
            int timeOut = int.Parse(this.timeout.Text);

            total_evaluation = true;
            Thread t = new Thread(new ThreadStart(() => this.RunScript(c_list, timeOut)));

            updateScreenButtons(state.runningScript);
            t.Start();
        }