Esempio n. 1
0
        private void OnMenuItem_Click(System.Object sender, System.EventArgs e)
        {
            ToolStripMenuItem mi = sender as ToolStripMenuItem;
            Form dlg             = null;

            if (mi == null)
            {
                return;
            }

            MFDevice       device    = null;
            MFConfigHelper cfgHelper = null;

            Cursor old = Cursor.Current;

            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (mi != signDeploymentFileToolStripMenuItem1)
                {
                    device = ConnectToSelectedDevice();

                    if (device == null || !device.IsConnected)
                    {
                        MessageBox.Show(this, string.Format(Properties.Resources.ErrorDeviceNotResponding, comboBoxDevice.Text), Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    cfgHelper = new MFConfigHelper(device);

                    if (!cfgHelper.IsValidConfig)
                    {
                        MessageBox.Show(this, Properties.Resources.ErrorUnsupportedConfiguration, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }

                if (mi == signDeploymentFileToolStripMenuItem1)
                {
                    dlg = new MFAppDeployConfigDialog(MFAppDeployConfigDialog.ConfigDialogCommand.SignDeployment);
                }
                else if (mi == updateDeviceKeysToolStripMenuItem)
                {
                    dlg = new MFAppDeployConfigDialog(device, MFAppDeployConfigDialog.ConfigDialogCommand.Configure);
                }
                else if (mi == createApplicationDeploymentToolStripMenuItem1)
                {
                    dlg = new MFAppDeployConfigDialog(device, MFAppDeployConfigDialog.ConfigDialogCommand.CreateDeployment);
                }
                else if (mi == uSBToolStripMenuItem)
                {
                    dlg = new MFUsbConfigDialog(device);
                }
                else if (mi == networkToolStripMenuItem)
                {
                    dlg = new MFNetworkConfigDialog(device);
                }

                if (dlg != null)
                {
                    dlg.StartPosition = FormStartPosition.CenterParent;
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        if (dlg is MFUsbConfigDialog)
                        {
                            cfgHelper.Dispose();
                            cfgHelper = null;
                            while (!DisconnectFromSelectedDevice())
                            {
                                ;
                            }
                            Thread.Sleep(500); // wait for device to reset
                            OnDeviceListUpdate(null, null);
                        }
                    }
                }

                else if (mi == uSBConfigurationToolStripMenuItem)
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Title           = "Load USB Config File";
                    ofd.DefaultExt      = "*.xml";
                    ofd.Filter          = "USB Config File (*.xml)|*.xml";
                    ofd.CheckFileExists = true;
                    ofd.Multiselect     = false;

                    cfgHelper.MaintainConnection = true;

                    if (DialogResult.OK == ofd.ShowDialog())
                    {
                        UsbXMLConfigSet xmlConfig    = new UsbXMLConfigSet(ofd.FileName);
                        byte[]          nativeConfig = xmlConfig.Read();
                        if (nativeConfig != null)
                        {
                            ofd.DefaultExt      = "*.txt";
                            ofd.CheckFileExists = false;
                            ofd.CheckPathExists = true;
                            ofd.FileName        = "USB Hex dump.txt";
                            ofd.Title           = "Dump file for native config data";
                            if (DialogResult.OK == ofd.ShowDialog())
                            {
                                FileStream dumpFile;
                                try
                                {
                                    dumpFile = File.Create(ofd.FileName);
                                }
                                catch (Exception e2)
                                {
                                    MessageBox.Show("Text dump file could not be opened due to exception: " + e2.Message);
                                    return;
                                }

                                // Dump byte array to text file for review
                                byte[] buffer    = new byte[16 * 3 + 2];
                                int    blockSize = 0;
                                int    bufIndex  = 0;
                                for (int index = 0; index < nativeConfig.Length; index += blockSize)
                                {
                                    blockSize = nativeConfig.Length - index;
                                    if (blockSize > 16)
                                    {
                                        blockSize = 16;
                                    }
                                    bufIndex = 0;
                                    for (int i = 0; i < blockSize; i++)
                                    {
                                        byte c;

                                        c  = (byte)((nativeConfig[index + i] >> 4) & 0x0F);
                                        c += (byte)'0';       // Convert to ASCII
                                        if (c > '9')
                                        {
                                            c += (byte)('A' - '9' - 1);
                                        }
                                        buffer[bufIndex++] = c;
                                        c  = (byte)(nativeConfig[index + i] & 0x0F);
                                        c += (byte)'0';       // Convert to ASCII
                                        if (c > '9')
                                        {
                                            c += (byte)('A' - '9' - 1);
                                        }
                                        buffer[bufIndex++] = c;
                                        buffer[bufIndex++] = (byte)' ';
                                    }
                                    buffer[bufIndex++] = (byte)'\r';
                                    buffer[bufIndex++] = (byte)'\n';
                                    dumpFile.Write(buffer, 0, bufIndex);
                                }
                                dumpFile.Close();
                                MessageBox.Show("Written " + nativeConfig.Length.ToString() + " configuration bytes to '" + ofd.FileName + "'.");
                            }

                            try
                            {
                                if (!cfgHelper.IsValidConfig)
                                {
                                    MessageBox.Show(null, Properties.Resources.ErrorUnsupportedConfiguration, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return;
                                }
                                // NOTE:
                                // This assumes there is only one USB controller:  controller 0.  In the future, if
                                // support is needed for multiple USB controllers, the user must be allowed to select
                                // which controller the XML configuration is for.  Then, the correct configuration
                                // name needs to be used for the selected controller.  "USB1" is used for controller 0,
                                // "USB2" is used for controller 1, etc.
                                cfgHelper.WriteConfig("USB1", nativeConfig);
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(null, string.Format(Properties.Resources.ErrorX, ex2.Message), Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, string.Format(Properties.Resources.ErrorX, ex.Message), Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                Cursor.Current = old;

                if (cfgHelper != null)
                {
                    cfgHelper.Dispose();
                }

                DisconnectFromSelectedDevice();

                if (dlg != null)
                {
                    dlg.Dispose();
                }
            }
        }
Esempio n. 2
0
        private void OnMenuItem_Click(System.Object sender, System.EventArgs e)
        {
            ToolStripMenuItem mi = sender as ToolStripMenuItem;
            Form dlg = null;

            if( mi == null ) return;

            MFDevice device = null;
            MFConfigHelper cfgHelper = null;

            Cursor old = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (mi != signDeploymentFileToolStripMenuItem1)
                {
                    device = ConnectToSelectedDevice();

                    if (device == null)
                    {
                        MessageBox.Show(this, string.Format(Properties.Resources.ErrorDeviceNotResponding, comboBoxDevice.Text), Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    cfgHelper = new MFConfigHelper(device);

                    if (!cfgHelper.IsValidConfig)
                    {
                        MessageBox.Show(this, Properties.Resources.ErrorUnsupportedConfiguration, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }

                if (mi == signDeploymentFileToolStripMenuItem1)
                {
                    dlg = new MFAppDeployConfigDialog(MFAppDeployConfigDialog.ConfigDialogCommand.SignDeployment);
                }
                else if (mi == updateDeviceKeysToolStripMenuItem)
                {
                    dlg = new MFAppDeployConfigDialog(device, MFAppDeployConfigDialog.ConfigDialogCommand.Configure);
                }
                else if (mi == createApplicationDeploymentToolStripMenuItem1)
                {
                    dlg = new MFAppDeployConfigDialog(device, MFAppDeployConfigDialog.ConfigDialogCommand.CreateDeployment);
                }
                else if (mi == uSBToolStripMenuItem)
                {
                    dlg = new MFUsbConfigDialog(device);
                }
                else if (mi == networkToolStripMenuItem)
                {
                    dlg = new MFNetworkConfigDialog(device);
                }

                if (dlg != null)
                {
                    dlg.StartPosition = FormStartPosition.CenterParent;
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        if (dlg is MFUsbConfigDialog)
                        {
                            cfgHelper.Dispose();
                            cfgHelper = null;
                            while (!DisconnectFromSelectedDevice()) ;
                            Thread.Sleep(500); // wait for device to reset
                            OnDeviceListUpdate(null, null);
                        }
                    }
                }

                else if (mi == uSBConfigurationToolStripMenuItem)
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Title = "Load USB Config File";
                    ofd.DefaultExt = "*.xml";
                    ofd.Filter = "USB Config File (*.xml)|*.xml";
                    ofd.CheckFileExists = true;
                    ofd.Multiselect = false;

                    cfgHelper.MaintainConnection = true;

                    if (DialogResult.OK == ofd.ShowDialog())
                    {
                        UsbXMLConfigSet xmlConfig = new UsbXMLConfigSet(ofd.FileName);
                        byte[] nativeConfig = xmlConfig.Read();
                        if (nativeConfig != null)
                        {
                            ofd.DefaultExt = "*.txt";
                            ofd.CheckFileExists = false;
                            ofd.CheckPathExists = true;
                            ofd.FileName = "USB Hex dump.txt";
                            ofd.Title = "Dump file for native config data";
                            if (DialogResult.OK == ofd.ShowDialog())
                            {
                                FileStream dumpFile;
                                try
                                {
                                    dumpFile = File.Create(ofd.FileName);
                                }
                                catch (Exception e2)
                                {
                                    MessageBox.Show("Text dump file could not be opened due to exception: " + e2.Message);
                                    return;
                                }

                                // Dump byte array to text file for review
                                byte[] buffer = new byte[16 * 3 + 2];
                                int blockSize = 0;
                                int bufIndex = 0;
                                for (int index = 0; index < nativeConfig.Length; index += blockSize)
                                {
                                    blockSize = nativeConfig.Length - index;
                                    if (blockSize > 16)
                                    {
                                        blockSize = 16;
                                    }
                                    bufIndex = 0;
                                    for (int i = 0; i < blockSize; i++)
                                    {
                                        byte c;

                                        c = (byte)((nativeConfig[index + i] >> 4) & 0x0F);
                                        c += (byte)'0';       // Convert to ASCII
                                        if (c > '9')
                                        {
                                            c += (byte)('A' - '9' - 1);
                                        }
                                        buffer[bufIndex++] = c;
                                        c = (byte)(nativeConfig[index + i] & 0x0F);
                                        c += (byte)'0';       // Convert to ASCII
                                        if (c > '9')
                                        {
                                            c += (byte)('A' - '9' - 1);
                                        }
                                        buffer[bufIndex++] = c;
                                        buffer[bufIndex++] = (byte)' ';
                                    }
                                    buffer[bufIndex++] = (byte)'\r';
                                    buffer[bufIndex++] = (byte)'\n';
                                    dumpFile.Write(buffer, 0, bufIndex);
                                }
                                dumpFile.Close();
                                MessageBox.Show("Written " + nativeConfig.Length.ToString() + " configuration bytes to '" + ofd.FileName + "'.");
                            }

                            try
                            {
                                if (!cfgHelper.IsValidConfig)
                                {
                                    MessageBox.Show(null, Properties.Resources.ErrorUnsupportedConfiguration, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return;
                                }
                                // NOTE:
                                // This assumes there is only one USB controller:  controller 0.  In the future, if
                                // support is needed for multiple USB controllers, the user must be allowed to select
                                // which controller the XML configuration is for.  Then, the correct configuration
                                // name needs to be used for the selected controller.  "USB1" is used for controller 0,
                                // "USB2" is used for controller 1, etc.
                                cfgHelper.WriteConfig("USB1", nativeConfig);
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(null, string.Format(Properties.Resources.ErrorX, ex2.Message), Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, string.Format(Properties.Resources.ErrorX, ex.Message), Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                Cursor.Current = old;

                if (cfgHelper != null)
                {
                    cfgHelper.Dispose();
                }

                DisconnectFromSelectedDevice();

                if (dlg != null)
                {
                    dlg.Dispose();
                }
            }
        }