public MFTestResults MFPortDefinitionEnumerationTest()
        {
            MFDeploy dep = new MFDeploy();
            try
            {
                int count = dep.DeviceList.Count;
                Log.Comment("Found " + count + " PortDefinitions.");
                int i = 1;
                foreach (MFPortDefinition pd in dep.DeviceList)
                {
                    Log.Comment("====================================");
                    Log.Comment("Port Definition " + i++);
                    Log.Comment("====================================");
                    Log.Comment("Name = " + pd.Name);
                    Log.Comment("Port = " + pd.Port);
                    Log.Comment("Transport = " + pd.Transport);
                }
                Log.Comment("====================================");
                Log.Comment("Successfully iterated through all the enumerations");
                Log.Comment("====================================");
            }
            catch (Exception ex)
            {
                Log.Exception(ex.Message);
                return MFTestResults.Fail;
            }

            return MFTestResults.Pass;
        }
 public DeviceModel()
 {
     _deploy = new MFDeploy();
     _deploy.OnDeviceListUpdate += _deploy_OnDeviceListUpdate;
     // This is slow and needs to run in the background
     Task.Run(() => UpdateDeviceList());
 }
 public void Dispose()
 {
     foreach (var item in _devices)
     {
         item.Dispose();
     }
     _devices.Clear();
     _deploy.Dispose();
     _deploy = null;
 }
        static void Main(string[] args)
        {
            using (MFDeploy deploy = new MFDeploy())
            {
                // Obtain devices connected to USB port
                IList<MFPortDefinition> portDefs = deploy.EnumPorts(TransportType.USB);

                // List devices
                Debug.WriteLine("USB Devices:");
                foreach (MFPortDefinition portDef in portDefs)
                {
                    Debug.WriteLine(portDef.Name);
                }

                // Return if no device was found
                if (portDefs.Count == 0)
                {
                    Debug.WriteLine("No device.");
                    return;
                }

                // Connect to first device that was found
                using (MFDevice device = deploy.Connect(portDefs[0]))
                {
                    uint entryPoint = 0;
                    if (device.Deploy("c:\\myapp.hex", // deployment image
                                      "c:\\myapp.sig", // signature file (optional)
                                      ref entryPoint   // return apps entry point
                                      ))
                    {
                        Debug.WriteLine("Deploying succeded.");

                        if (entryPoint != 0) // check if image has an entry point
                        {
                            Debug.WriteLine("Executing application.");
                            device.Execute(entryPoint);
                        }
                    }
                    else
                        Debug.WriteLine("Deploying failed.");
                }
            }
        }
            private string PingDevice(HarnessExecutionResult prevResult)
            {
                MFDeploy mfDeploy = new MFDeploy();
                PingConnectionType pct = PingConnectionType.NoConnection;
                bool usbFound = false;
                int retries = 0;
                while (!usbFound && retries++ < 2)
                {
                    foreach (MFPortDefinition pd in mfDeploy.DeviceList)
                    {
                        if (string.Equals(pd.Transport.ToString(),
                            m_transport, StringComparison.InvariantCultureIgnoreCase))
                        {
                            usbFound = true;
                            Utils.WriteToEventLog("Previous result from harness: " + prevResult);
                            int attempts = 0;
                            while ((PingConnectionType.NoConnection == pct ||
                                PingConnectionType.TinyBooter == pct) &&
                                (attempts++ < 5))
                            {
                                try
                                {
                                    using (MFDevice dev = mfDeploy.Connect(pd))
                                    {
                                        pct = dev.Ping();
                                        Utils.WriteToEventLog("MFDevice.Ping() resulted in a " + pct.ToString());
                                        if (PingConnectionType.TinyBooter == pct)
                                        {
                                            // Clear the bootloader flag so that the device gets
                                            // back into tinyclr.
                                            dev.Execute(0);
                                        }
                                        dev.Dispose();
                                    }

                                    if (PingConnectionType.NoConnection == pct)
                                    {
                                        Utils.WriteToEventLog("Attempting to reset power since we have a " +
                                            "NoConnection status from device..");
                                        ResetDevicePower();
                                    }
                                }
                                catch
                                {
                                    Utils.WriteToEventLog("An exception was thrown when attempting a ping. " +
                                        "Reseting power contoller directly...");
                                    MFPowerController.Reset();
                                    System.Threading.Thread.Sleep(5000);
                                }

                                System.Threading.Thread.Sleep(1000);
                            }
                            break;
                        }                       
                    }
                    
                    if (!usbFound)
                    {
                        Utils.WriteToEventLog("USB not found after the attempt#: " + retries);
                        MFPowerController.Reset();
                        System.Threading.Thread.Sleep(5000);
                    }
                }
                
                return pct.ToString();
            }
        internal void Execute()
        {
            MFDeploy deploy = new MFDeploy();
            MFDevice port   = null;

            try
            {
                switch (m_cmd)
                {
                    case Commands.Help:
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.HelpBanner);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.HelpDescription);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.HelpUsage);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.HelpCommand);
                        foreach (CommandMap cm in m_commandMap)
                        {
                            Console.WriteLine("  " + cm.HelpString);
                        }
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.HelpInterface);
                        foreach (InterfaceMap im in m_interfaceMap)
                        {
                            Console.WriteLine("  " + im.InterfaceHelp);
                        }
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.HelpInterfaceSpecial);
                        Console.WriteLine();
                        break;
                    case Commands.List:
                        foreach (MFPortDefinition pd in deploy.DeviceList)
                        {
                            Console.WriteLine(pd.Name);
                        }
                        break;
                    case Commands.Ping:
                        Console.Write(Properties.Resources.StatusPinging);
                        try
                        {
                            port = deploy.Connect(m_transports[0]);
                            Console.WriteLine(port.Ping().ToString());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(Properties.Resources.ErrorPrefix + e.Message);
                        }
                        break;
                    case Commands.Erase:
                        Console.Write(Properties.Resources.StatusErasing);
                        try
                        {
                            port = deploy.Connect(m_transports[0]);
                            port.OnProgress += new MFDevice.OnProgressHandler(OnStatus);
                            Console.WriteLine((port.Erase() ? Properties.Resources.ResultSuccess : Properties.Resources.ResultFailure));
                            port.OnProgress -= new MFDevice.OnProgressHandler(OnStatus);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(Properties.Resources.ErrorPrefix + e.Message);
                        }
                        break;
                    case Commands.Deploy:
                        try
                        {
                            bool fOK = false;
                            uint entrypoint = 0;

                            port = deploy.Connect(m_transports[0]);

                            foreach (string file in m_flashFiles)
                            {
                                uint entry = 0;
                                FileInfo fi = new FileInfo(file);
                                string signature_file = file;

                                if (fi.Extension != null || fi.Extension.Length > 0)
                                {
                                    int index = file.LastIndexOf(fi.Extension);
                                    signature_file = file.Remove(index, fi.Extension.Length);
                                }

                                signature_file += ".sig";

                                if (!File.Exists(file))
                                {
                                    Console.WriteLine(string.Format(Properties.Resources.ErrorFileNotFound, file));
                                    break;
                                }

                                if (!File.Exists(signature_file))
                                {
                                    Console.WriteLine(string.Format(Properties.Resources.ErrorFileNotFound, signature_file));
                                    break;
                                }

                                Console.WriteLine(string.Format(Properties.Resources.StatusFlashing, file));
                                port.OnProgress += new MFDevice.OnProgressHandler(OnDeployStatus); 
                                fOK = port.Deploy(file, signature_file, ref entry);
                                port.OnProgress -= new MFDevice.OnProgressHandler(OnDeployStatus);
                                Console.WriteLine();
                                Console.WriteLine((fOK ? Properties.Resources.ResultSuccess : Properties.Resources.ResultFailure));
                                if (entry != 0 && entrypoint == 0 || file.ToLower().Contains("\\er_flash"))
                                {
                                    entrypoint = entry;
                                }
                            }
                            if (fOK)
                            {
                                Console.WriteLine(string.Format(Properties.Resources.StatusExecuting, entrypoint));
                                port.Execute(entrypoint);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(Properties.Resources.ErrorPrefix + e.Message);
                        }
                        break;
                    case Commands.Reboot:
                        try
                        {
                            port = deploy.Connect(m_transports[0]);
                            Console.WriteLine(Properties.Resources.StatusRebooting);
                            port.Reboot(!m_fWarmReboot);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(Properties.Resources.ErrorPrefix + e.Message);
                        }
                        break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format(Properties.Resources.ErrorFailure, e.Message));
            }
            finally
            {
                if (deploy != null)
                {
                    deploy.Dispose();
                    deploy = null;
                }
            }
        }
        private void Form1_FormClosing(System.Object sender, FormClosingEventArgs e)
        {
            m_fShuttingDown = true;

            if (m_pluginThread != null && m_pluginThread.IsAlive)
            {
                m_pluginThread.Abort();
            }

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

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