Exemple #1
0
        async Task DeployAppAsync(string target, string folder, IOutputPaneWriter outputPaneWriter, CancellationToken cts)
        {
            Stopwatch sw = Stopwatch.StartNew();
            await outputPaneWriter.WriteAsync($"Deploying to Meadow on {target}...");

            try
            {
                var meadow = _currentDevice = await MeadowDeviceManager.GetMeadowForSerialPort(target);

                EventHandler <MeadowMessageEventArgs> handler = (s, e) =>
                {
                    if (!string.IsNullOrEmpty(e.Message))
                    {
                        outputPaneWriter.WriteAsync(e.Message).Wait();
                    }
                };

                await MeadowDeviceManager.MonoDisable(meadow).ConfigureAwait(false);

                meadow.OnMeadowMessage += handler;
                await MeadowDeviceManager.DeployApp(meadow, Path.Combine(folder, "App.exe"));

                meadow.OnMeadowMessage -= handler;
                await MeadowDeviceManager.MonoEnable(meadow).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            sw.Stop();
            await outputPaneWriter.WriteAsync($"Deployment Duration: {sw.Elapsed}");
        }
Exemple #2
0
        async Task <bool> InitializeMeadowDevice(MeadowDeviceExecutionTarget target, CancellationTokenSource cts)
        {
            if (cts.IsCancellationRequested)
            {
                return(true);
            }

            target.Write($"Initializing Meadow {target.meadowSerialDevice.connection.ToString()}\n");

            if (target.meadowSerialDevice == null)
            {
                target.WriteError("Can't read Meadow device\n");
                return(false);
            }

            Console.WriteLine("MonoDisable");
            MeadowDeviceManager.MonoDisable(target.meadowSerialDevice);

            if (!(await target.meadowSerialDevice.AwaitStatus(5000, MeadowCLI.DeviceManagement.MeadowSerialDevice.DeviceStatus.PortOpen)).HasValue)
            {
                target.Write("The Meadow has failed to restart.\n");
                return(false);
            }
            return(true);
        }
 protected void OnRadioMonoDisabledPressed(object sender, EventArgs e)
 {
     if (!((Gtk.RadioButton)sender).Active)
     {
         MeadowDeviceManager.MonoDisable(meadowSerialDevice);
     }
 }
Exemple #4
0
        static async Task TestFlashCommands()
        {
            Stopwatch sw = new Stopwatch();

            string networkBootloaderFilename     = "bootloader.bin";
            string networkMeadowCommsFilename    = "MeadowComms.bin";
            string networkPartitionTableFilename = "partition-table.bin";

            var meadow = await MeadowDeviceManager.GetMeadowForSerialPort("COM3");

            //MeadowSerialDevice meadow = null;

            for (int i = 0; i < 20; i++)
            {
                //meadow = await MeadowDeviceManager.GetMeadowForSerialPort("COM3");

                Console.WriteLine("disable");
                await MeadowDeviceManager.MonoDisable(meadow);

                Thread.Sleep(11000);
                //meadow = await MeadowDeviceManager.GetMeadowForSerialPort("COM3");

                Console.WriteLine("enable");
                await MeadowDeviceManager.MonoEnable(meadow);

                Thread.Sleep(11000);
            }


            ////await MeadowFileManager.MonoUpdateRt(meadow, @"C:\Users\brikim\AppData\Local\WildernessLabs\Firmware\Meadow.OS.Runtime.bin");
            //Console.WriteLine("write runtime");
            //await MeadowFileManager.WriteFileToFlash(meadow, @"C:\Users\brikim\AppData\Local\WildernessLabs\Firmware\Meadow.OS.Runtime.bin");
            //Console.WriteLine("mono flash");

            //sw.Start();
            //await MeadowDeviceManager.MonoFlash(meadow); // does not emit `Concluded`
            //Console.WriteLine($"elapsed: {sw.Elapsed.TotalSeconds}");

            //Console.WriteLine($"flash esp: {networkBootloaderFilename}");
            //await MeadowFileManager.WriteFileToEspFlash(MeadowDeviceManager.CurrentDevice, Path.Combine(@"C:\Users\brikim\AppData\Local\WildernessLabs\Firmware", networkBootloaderFilename), mcuDestAddr: "0x1000");
            //Console.WriteLine($"flash esp: {networkPartitionTableFilename}");
            //await MeadowFileManager.WriteFileToEspFlash(MeadowDeviceManager.CurrentDevice, Path.Combine(@"C:\Users\brikim\AppData\Local\WildernessLabs\Firmware", networkPartitionTableFilename), mcuDestAddr: "0x8000");
            //Console.WriteLine($"flash esp: {networkMeadowCommsFilename}");
            //await MeadowFileManager.WriteFileToEspFlash(MeadowDeviceManager.CurrentDevice, Path.Combine(@"C:\Users\brikim\AppData\Local\WildernessLabs\Firmware", networkMeadowCommsFilename), mcuDestAddr: "0x10000");
        }
        async Task DeployAppAsync(string target, string folder, IOutputPaneWriter outputPaneWriter, CancellationToken cts)
        {
            Stopwatch sw = Stopwatch.StartNew();
            await outputPaneWriter.WriteAsync($"Deploying to Meadow on {target}...");

            try
            {
                var meadow = await MeadowDeviceManager.GetMeadowForSerialPort(target).ConfigureAwait(false);

                if (meadow == null)
                {
                    await outputPaneWriter.WriteAsync($"Device connection error, please disconnect device and try again.");

                    return;
                }

                await MeadowDeviceManager.ResetMeadow(meadow).ConfigureAwait(false);

                await Task.Delay(1000);

                meadow = await MeadowDeviceManager.GetMeadowForSerialPort(target).ConfigureAwait(false);

                await Task.Delay(1000);

                await MeadowDeviceManager.MonoDisable(meadow).ConfigureAwait(false);

                var meadowFiles = await GetFilesOnDevice(meadow, outputPaneWriter, cts).ConfigureAwait(false);

                var localFiles = await GetLocalFiles(outputPaneWriter, cts, folder).ConfigureAwait(false);
                await DeleteUnusedFiles(meadow, outputPaneWriter, cts, meadowFiles, localFiles).ConfigureAwait(false);
                await DeployApp(meadow, outputPaneWriter, cts, folder, meadowFiles, localFiles).ConfigureAwait(false);

                await MeadowDeviceManager.MonoEnable(meadow).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            sw.Stop();
            await outputPaneWriter.WriteAsync($"Deployment Duration: {sw.Elapsed}");
        }
Exemple #6
0
        public async Task FlashOSTest()
        {
            //DfuUpload.FlashOS(Path.Combine(fixturesPath.FullName, osFilename));

            using (var meadow = await MeadowDeviceManager.GetMeadowForSerialPort(port))
            {
                Assert.IsNotNull(meadow, "Initial connection");
                await MeadowDeviceManager.MonoDisable(meadow);

                var isEnabled = await MeadowDeviceManager.MonoRunState(meadow);

                // try to disable one more time
                if (isEnabled)
                {
                    await MeadowDeviceManager.MonoDisable(meadow);

                    isEnabled = await MeadowDeviceManager.MonoRunState(meadow);
                }
                Assert.IsFalse(isEnabled, "Disable mono");
            }

            using (var meadow = await MeadowDeviceManager.GetMeadowForSerialPort(port))
            {
                await MeadowFileManager.MonoUpdateRt(meadow, Path.Combine(fixturesPath.FullName, runtimeFilename));
            }

            using (var meadow = await MeadowDeviceManager.GetMeadowForSerialPort(port))
            {
                await MeadowFileManager.FlashEsp(meadow, fixturesPath.FullName);
            }

            using (var meadow = await MeadowDeviceManager.GetMeadowForSerialPort(port))
            {
                Assert.IsNotNull(meadow);
                await MeadowDeviceManager.MonoEnable(meadow);

                var isEnabled = await MeadowDeviceManager.MonoRunState(meadow);

                Assert.IsTrue(isEnabled);
            }
        }
Exemple #7
0
        async Task <bool> InitializeMeadowDeviceAsync(MeadowSerialDevice meadow, TextWriter outputPaneWriter, CancellationToken cts)
        {
            if (cts.IsCancellationRequested)
            {
                return(true);
            }

            await outputPaneWriter.WriteAsync("Initializing Meadow");

            if (meadow == null)
            {
                await outputPaneWriter.WriteAsync("Can't read Meadow device");

                return(false);
            }

            meadow.Initialize(false);
            MeadowDeviceManager.MonoDisable(meadow);
            await Task.Delay(5000); //hack for testing

            if (meadow.Initialize() == false)
            {
                await outputPaneWriter.WriteAsync("Couldn't initialize serial port");

                return(false);
            }
            else
            {
                MeadowDeviceManager.GetDeviceInfo(meadow);
                await Task.Delay(1000); // wait for device info to populate

                await outputPaneWriter.WriteAsync($"Device {meadow.DeviceInfo.MeadowOSVersion}");
            }

            return(true);
        }
Exemple #8
0
        private async void Flash_ESP_Click(object sender, RoutedEventArgs e)
        {
            _verbose = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);

            MeadowSerialDevice meadow = null;

            try
            {
                MeadowSettings settings = new MeadowSettings(Globals.SettingsFilePath);

                await OutputMessageAsync($"Begin '{Flash_Coprocessor_Text}'", true);

                EnableControls(false);

                await OutputMessageAsync($"Initialize device (~30s)");

                if (string.IsNullOrEmpty(settings.DeviceTarget))
                {
                    await OutputMessageAsync($"Select Target Device Port and click '{Flash_Device_Text}' to resume.");

                    _skipFlashToSelectDevice = true;
                    EnableControls(true);
                    return;
                }

                // initialize connection. need to jump through hoops after exiting dfu mode =(
                meadow = await MeadowDeviceManager.GetMeadowForSerialPort(settings.DeviceTarget).ConfigureAwait(false);

                await MeadowDeviceManager.ResetMeadow(meadow).ConfigureAwait(false);

                await Task.Delay(1000);

                meadow = await MeadowDeviceManager.GetMeadowForSerialPort(settings.DeviceTarget).ConfigureAwait(false);

                if (_verbose)
                {
                    await MeadowDeviceManager.TraceEnable(meadow).ConfigureAwait(false);
                }
                else
                {
                    await MeadowDeviceManager.TraceDisable(meadow).ConfigureAwait(false);
                }
                meadow.OnMeadowMessage += MeadowMesageHandler;
                await Task.Delay(1000);

                await MeadowDeviceManager.MonoDisable(meadow).ConfigureAwait(false);

                await Task.Delay(10000); // wait for reconnect, need an event for this

                await MeadowFileManager.WriteFileToEspFlash(meadow, Path.Combine(Globals.FirmwareDownloadsFilePath, networkMeadowCommsFilename), mcuDestAddr : "0x10000").ConfigureAwait(false);

                await Task.Delay(1000);

                await MeadowFileManager.WriteFileToEspFlash(meadow, Path.Combine(Globals.FirmwareDownloadsFilePath, networkBootloaderFilename), mcuDestAddr : "0x1000").ConfigureAwait(false);

                await Task.Delay(1000);

                await MeadowFileManager.WriteFileToEspFlash(meadow, Path.Combine(Globals.FirmwareDownloadsFilePath, networkPartitionTableFilename), mcuDestAddr : "0x8000").ConfigureAwait(false);

                await Task.Delay(1000);

                await OutputMessageAsync($"{Flash_Coprocessor_Text} completed.");

                await MeadowDeviceManager.MonoEnable(meadow).ConfigureAwait(false);

                await Task.Delay(10000); // wait for reconnect, need an event for this
            }
            catch (Exception ex)
            {
                await OutputMessageAsync($"An unexpected error occurred. Please try again.");
            }
            finally
            {
                if (meadow != null)
                {
                    meadow.OnMeadowMessage -= MeadowMesageHandler;
                }
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                EnableControls(true);
            }
        }
Exemple #9
0
        private async void Flash_Device(object sender, RoutedEventArgs e)
        {
            _verbose = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);

            MeadowSerialDevice meadow = null;

            try
            {
                MeadowSettings settings = new MeadowSettings(Globals.SettingsFilePath);

                var(osFilePath, runtimeFilePath) = await GetWorkingFiles();

                if (string.IsNullOrEmpty(osFilePath) && string.IsNullOrEmpty(runtimeFilePath))
                {
                    await OutputMessageAsync($"Meadow OS files not found. 'Download Meadow OS' first.");

                    return;
                }

                EnableControls(false);

                await OutputMessageAsync($"Begin '{Flash_Device_Text}'", true);

                if (_verbose)
                {
                    await OutputMessageAsync($"Trace output enabled.");
                }

                if (!string.IsNullOrEmpty(osFilePath))
                {
                    if (!await DfuFlash(osFilePath, osAddress).ConfigureAwait(false))
                    {
                        EnableControls(true);
                        return;
                    }
                }
                else
                {
                    await OutputMessageAsync($"{osFilename} not selected. Skipping OS flash.");
                }

                //reset skip flash flag
                _skipFlashToSelectDevice = false;

                if (!string.IsNullOrEmpty(runtimeFilePath))
                {
                    await OutputMessageAsync($"Initialize device (~30s)");

                    if (string.IsNullOrEmpty(settings.DeviceTarget))
                    {
                        await OutputMessageAsync($"Select Target Device Port and click '{Flash_Device_Text}' to resume.");

                        _skipFlashToSelectDevice = true;
                        EnableControls(true);
                        return;
                    }

                    // initialize connection. need to jump through hoops after exiting dfu mode =(
                    meadow = await MeadowDeviceManager.GetMeadowForSerialPort(settings.DeviceTarget).ConfigureAwait(false);

                    await MeadowDeviceManager.ResetMeadow(meadow).ConfigureAwait(false);

                    await Task.Delay(1000);

                    meadow = await MeadowDeviceManager.GetMeadowForSerialPort(settings.DeviceTarget).ConfigureAwait(false);

                    if (_verbose)
                    {
                        await MeadowDeviceManager.TraceEnable(meadow).ConfigureAwait(false);
                    }
                    else
                    {
                        await MeadowDeviceManager.TraceDisable(meadow).ConfigureAwait(false);
                    }
                    meadow.OnMeadowMessage += MeadowMesageHandler;
                    await Task.Delay(1000);

                    await MeadowDeviceManager.MonoDisable(meadow).ConfigureAwait(false);

                    await Task.Delay(10000); // wait for reconnect, need an event for this

                    await MeadowFileManager.WriteFileToFlash(meadow, runtimeFilePath).ConfigureAwait(false);

                    await MeadowDeviceManager.MonoFlash(meadow).ConfigureAwait(false);

                    await meadow.DeleteFile(runtimeFilename).ConfigureAwait(false);

                    await MeadowDeviceManager.MonoEnable(meadow).ConfigureAwait(false);

                    await Task.Delay(10000); // wait for reconnect, need an event for this
                }
                else
                {
                    await OutputMessageAsync($"{runtimeFilename} not selected. Skipping Runtime flash.");
                }

                await OutputMessageAsync($"'{Flash_Device_Text}' completed");
            }
            catch (Exception ex)
            {
                await OutputMessageAsync($"An unexpected error occurred. Please try again.");
            }
            finally
            {
                if (meadow != null)
                {
                    meadow.OnMeadowMessage -= MeadowMesageHandler;
                }
                _verbose = false;
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                EnableControls(true);
            }
        }
Exemple #10
0
        private async void Flash_Device(object sender, RoutedEventArgs e)
        {
            try
            {
                MeadowSettings settings = new MeadowSettings(Globals.SettingsFilePath);

                var(osFilePath, runtimeFilePath) = await GetWorkingFiles();

                if (string.IsNullOrEmpty(osFilePath) && string.IsNullOrEmpty(runtimeFilePath))
                {
                    await OutputMessageAsync($"Meadow OS files not found. 'Download Meadow OS' first.");

                    return;
                }

                EnableControls(false);

                await OutputMessageAsync($"Begin '{Flash_Device_Text}'", true);

                if (!string.IsNullOrEmpty(osFilePath))
                {
                    if (!await DfuFlash(osFilePath, osAddress))
                    {
                        EnableControls(true);
                        return;
                    }
                }
                else
                {
                    await OutputMessageAsync($"{osFilename} not selected. Skipping OS flash.");
                }

                //reset skip flash flag
                _skipFlashToSelectDevice = false;

                if (!string.IsNullOrEmpty(runtimeFilePath))
                {
                    await OutputMessageAsync($"Initialize device");

                    MeadowDeviceManager.CurrentDevice = null;

                    if (string.IsNullOrEmpty(settings.DeviceTarget))
                    {
                        await OutputMessageAsync($"Select Target Device Port and click '{Flash_Device_Text}' to resume.");

                        _skipFlashToSelectDevice = true;
                        EnableControls(true);
                        return;
                    }
                    else
                    {
                        await MeadowDeviceManager.GetMeadowForSerialPort(settings.DeviceTarget);
                    }

                    if (MeadowDeviceManager.CurrentDevice == null)
                    {
                        await OutputMessageAsync($"Initialization failed. Try again.");

                        return;
                    }

                    if (!await Process(() => MeadowDeviceManager.ResetMeadow(MeadowDeviceManager.CurrentDevice, 0)))
                    {
                        return;
                    }

                    if (!await Process(() => MeadowDeviceManager.MonoDisable(MeadowDeviceManager.CurrentDevice)))
                    {
                        return;
                    }

                    await OutputMessageAsync($"Erase flash (~3 mins)");

                    if (!await Process(() => MeadowFileManager.EraseFlash(MeadowDeviceManager.CurrentDevice)))
                    {
                        return;
                    }

                    await OutputMessageAsync($"Restart device");

                    if (!await Process(() => MeadowDeviceManager.ResetMeadow(MeadowDeviceManager.CurrentDevice, 0)))
                    {
                        return;
                    }

                    await OutputMessageAsync($"Upload {runtimeFilename} (~1 min)");

                    if (!await Process(() => MeadowFileManager.WriteFileToFlash(MeadowDeviceManager.CurrentDevice, runtimeFilePath)))
                    {
                        return;
                    }

                    await OutputMessageAsync($"Process {runtimeFilename} (~30 secs)");

                    if (!await Process(() => MeadowDeviceManager.MonoFlash(MeadowDeviceManager.CurrentDevice)))
                    {
                        return;
                    }

                    await MeadowDeviceManager.CurrentDevice.DeleteFile(runtimeFilename);

                    if (!await Process(() => MeadowDeviceManager.MonoEnable(MeadowDeviceManager.CurrentDevice)))
                    {
                        return;
                    }

                    await OutputMessageAsync($"Restart device");

                    if (!await Process(() => MeadowDeviceManager.ResetMeadow(MeadowDeviceManager.CurrentDevice, 0)))
                    {
                        return;
                    }
                }
                else
                {
                    await OutputMessageAsync($"{runtimeFilename} not selected. Skipping Runtime flash.");
                }

                EnableControls(true);

                await OutputMessageAsync($"'{Flash_Device_Text}' completed");
            }
            catch (Exception ex)
            {
                await OutputMessageAsync($"An unexpected error occurred. Please try again.");

                EnableControls(true);
            }
        }
Exemple #11
0
        //Probably rename

        static async Task <CompletionBehavior> ProcessHcom(Options options)
        {
            Console.Write($"Opening port '{options.SerialPort}'...");
            if (ConnectToMeadowDevice(options.SerialPort))
            {
                // verify that the port was actually connected
                if (MeadowDeviceManager.CurrentDevice.Socket == null &&
                    !IsSerialPortValid(MeadowDeviceManager.CurrentDevice.SerialPort))
                {
                    Console.WriteLine($"port not available");
                    return(CompletionBehavior.RequestFailed);
                }
            }
            else
            {
                Console.WriteLine($"failed to open port");
                return(CompletionBehavior.RequestFailed);
            }
            Console.WriteLine($"ok.");

            try
            {
                if (options.WriteFile)
                {
                    if (string.IsNullOrEmpty(options.FileName))
                    {
                        Console.WriteLine($"option --WriteFile also requires option --File (the local file you wish to write)");
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(options.TargetFileName))
                        {
#if USE_PARTITIONS
                            Console.WriteLine($"Writing {options.FileName} to partition {options.Partition}");
#else
                            Console.WriteLine($"Writing {options.FileName}");
#endif
                        }
                        else
                        {
#if USE_PARTITIONS
                            Console.WriteLine($"Writing {options.FileName} as {options.TargetFileName} to partition {options.Partition}");
#else
                            Console.WriteLine($"Writing {options.FileName} as {options.TargetFileName}");
#endif
                        }

                        await MeadowFileManager.WriteFileToFlash(MeadowDeviceManager.CurrentDevice, options.FileName, options.TargetFileName, options.Partition).ConfigureAwait(false);;
                    }
                }
                else if (options.DeleteFile)
                {
                    if (string.IsNullOrEmpty(options.TargetFileName))
                    {
                        Console.WriteLine($"option --DeleteFile also requires option --TargetFileName (the file you wish to delete)");
                    }
                    else
                    {
#if USE_PARTITIONS
                        Console.WriteLine($"Deleting {options.FileName} from partion {options.Partition}");
#else
                        Console.WriteLine($"Deleting {options.FileName}");
#endif
                        await MeadowFileManager.DeleteFile(MeadowDeviceManager.CurrentDevice, options.TargetFileName, options.Partition);
                    }
                }
                else if (options.EraseFlash)
                {
                    Console.WriteLine("Erasing flash");
                    await MeadowFileManager.EraseFlash(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.VerifyErasedFlash)
                {
                    Console.WriteLine("Verifying flash is erased");
                    await MeadowFileManager.VerifyErasedFlash(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.PartitionFileSystem)
                {
                    Console.WriteLine($"Partioning file system into {options.NumberOfPartitions} partition(s)");
                    await MeadowFileManager.PartitionFileSystem(MeadowDeviceManager.CurrentDevice, options.NumberOfPartitions);
                }
                else if (options.MountFileSystem)
                {
#if USE_PARTITIONS
                    Console.WriteLine($"Mounting partition {options.Partition}");
#else
                    Console.WriteLine("Mounting file system");
#endif
                    await MeadowFileManager.MountFileSystem(MeadowDeviceManager.CurrentDevice, options.Partition);
                }
                else if (options.InitFileSystem)
                {
#if USE_PARTITIONS
                    Console.WriteLine($"Intializing filesystem in partition {options.Partition}");
#else
                    Console.WriteLine("Intializing filesystem");
#endif
                    await MeadowFileManager.InitializeFileSystem(MeadowDeviceManager.CurrentDevice, options.Partition);
                }
                else if (options.CreateFileSystem) //should this have a partition???
                {
                    Console.WriteLine($"Creating file system");
                    await MeadowFileManager.CreateFileSystem(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.FormatFileSystem)
                {
#if USE_PARTITIONS
                    Console.WriteLine($"Format file system on partition {options.Partition}");
#else
                    Console.WriteLine("Format file system");
#endif
                    await MeadowFileManager.FormatFileSystem(MeadowDeviceManager.CurrentDevice, options.Partition);
                }
                else if (options.ListFiles)
                {
#if USE_PARTITIONS
                    Console.WriteLine($"Getting list of files on partition {options.Partition}");
#else
                    Console.WriteLine($"Getting list of files");
#endif
                    await MeadowFileManager.ListFiles(MeadowDeviceManager.CurrentDevice, options.Partition);
                }
                else if (options.ListFilesAndCrcs)
                {
#if USE_PARTITIONS
                    Console.WriteLine($"Getting list of files and CRCs on partition {options.Partition}");
#else
                    Console.WriteLine("Getting list of files and CRCs");
#endif
                    await MeadowFileManager.ListFilesAndCrcs(MeadowDeviceManager.CurrentDevice, options.Partition);
                }
                //Device manager
                else if (options.SetTraceLevel)
                {
                    Console.WriteLine($"Setting trace level to {options.TraceLevel}");
                    await MeadowDeviceManager.SetTraceLevel(MeadowDeviceManager.CurrentDevice, options.TraceLevel);
                }
                else if (options.SetDeveloper1)
                {
                    Console.WriteLine($"Setting developer level to {options.DeveloperValue}");
                    await MeadowDeviceManager.SetDeveloper1(MeadowDeviceManager.CurrentDevice, options.DeveloperValue);
                }
                else if (options.SetDeveloper2)
                {
                    Console.WriteLine($"Setting developer level to {options.DeveloperValue}");
                    await MeadowDeviceManager.SetDeveloper2(MeadowDeviceManager.CurrentDevice, options.DeveloperValue);
                }
                else if (options.SetDeveloper3)
                {
                    Console.WriteLine($"Setting developer level to {options.DeveloperValue}");
                    await MeadowDeviceManager.SetDeveloper3(MeadowDeviceManager.CurrentDevice, options.DeveloperValue);
                }
                else if (options.SetDeveloper4)
                {
                    Console.WriteLine($"Setting developer level to {options.DeveloperValue}");
                    await MeadowDeviceManager.SetDeveloper4(MeadowDeviceManager.CurrentDevice, options.DeveloperValue);
                }
                else if (options.NshEnable)
                {
                    Console.WriteLine($"Enable Nsh");
                    await MeadowDeviceManager.NshEnable(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.MonoDisable)
                {
                    await MeadowDeviceManager.MonoDisable(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.MonoEnable)
                {
                    await MeadowDeviceManager.MonoEnable(MeadowDeviceManager.CurrentDevice);

                    // the device is going to reset, so we need to wait for it to reconnect
                    Console.WriteLine($"Reconnecting...");
                    System.Threading.Thread.Sleep(5000);

                    // just enter port echo mode until the user cancels
                    MeadowDeviceManager.EnterEchoMode(MeadowDeviceManager.CurrentDevice);

                    return(CompletionBehavior.Success | CompletionBehavior.KeepConsoleOpen);
                }
                else if (options.MonoRunState)
                {
                    await MeadowDeviceManager.MonoRunState(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.MonoFlash)
                {
                    await MeadowDeviceManager.MonoFlash(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.MonoUpdateRt)
                {
                    await MeadowFileManager.MonoUpdateRt(MeadowDeviceManager.CurrentDevice,
                                                         options.FileName, options.TargetFileName, options.Partition);
                }
                else if (options.GetDeviceInfo)
                {
                    await MeadowDeviceManager.GetDeviceInfo(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.ResetMeadow)
                {
                    Console.WriteLine("Resetting Meadow");
                    await MeadowDeviceManager.ResetMeadow(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.EnterDfuMode)
                {
                    Console.WriteLine("Entering Dfu mode");
                    await MeadowDeviceManager.EnterDfuMode(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.TraceDisable)
                {
                    Console.WriteLine("Disabling Meadow trace messages");
                    await MeadowDeviceManager.TraceDisable(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.TraceEnable)
                {
                    Console.WriteLine("Enabling Meadow trace messages");
                    await MeadowDeviceManager.TraceEnable(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.Uart1Apps)
                {
                    Console.WriteLine("Use Uart1 for .NET Apps");
                    await MeadowDeviceManager.Uart1Apps(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.Uart1Trace)
                {
                    Console.WriteLine("Use Uart1 for outputting Meadow trace messages");
                    await MeadowDeviceManager.Uart1Trace(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.RenewFileSys)
                {
                    Console.WriteLine("Recreate a new file system on Meadow");
                    await MeadowDeviceManager.RenewFileSys(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.QspiWrite)
                {
                    Console.WriteLine($"Executing QSPI Flash Write using {options.DeveloperValue}");
                    await MeadowDeviceManager.QspiWrite(MeadowDeviceManager.CurrentDevice, options.DeveloperValue);
                }
                else if (options.QspiRead)
                {
                    Console.WriteLine($"Executing QSPI Flash Read using {options.DeveloperValue}");
                    await MeadowDeviceManager.QspiRead(MeadowDeviceManager.CurrentDevice, options.DeveloperValue);
                }
                else if (options.QspiInit)
                {
                    Console.WriteLine($"Executing QSPI Flash Initialization using {options.DeveloperValue}");
                    await MeadowDeviceManager.QspiInit(MeadowDeviceManager.CurrentDevice, options.DeveloperValue);
                }
                else if (options.VSDebug)
                {
                    MeadowDeviceManager.VSDebug(options.VSDebugPort);

                    Console.WriteLine($"Ready for Visual Studio debugging");
                    options.KeepAlive = true;
                }
                else if (options.Esp32WriteFile)
                {
                    if (string.IsNullOrEmpty(options.FileName))
                    {
                        Console.WriteLine($"option --Esp32WriteFile requires option --File (the local file you wish to write)");
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(options.TargetFileName))
                        {
                            Console.WriteLine($"Writing {options.FileName} to ESP32");
                        }
                        else
                        {
                            Console.WriteLine($"Writing {options.FileName} as {options.TargetFileName}");
                        }
                        await MeadowFileManager.WriteFileToEspFlash(MeadowDeviceManager.CurrentDevice,
                                                                    options.FileName, options.TargetFileName, options.Partition, options.McuDestAddr);
                    }
                }
                else if (options.Esp32ReadMac)
                {
                    await MeadowDeviceManager.Esp32ReadMac(MeadowDeviceManager.CurrentDevice);
                }
                else if (options.Esp32Restart)
                {
                    await MeadowDeviceManager.Esp32Restart(MeadowDeviceManager.CurrentDevice);
                }
            }
            catch (IOException ex)
            {
                if (ex.Message.Contains("semaphore"))
                {
                    Console.WriteLine("Timeout communicating with Meadow");
                }
                else
                {
                    Console.WriteLine($"Exception communicating with Meadow: {ex.Message}");
                }
                return(CompletionBehavior.RequestFailed | CompletionBehavior.KeepConsoleOpen);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception communicating with Meadow: {ex.Message}");
                return(CompletionBehavior.RequestFailed | CompletionBehavior.KeepConsoleOpen);
            }

            if (options.KeepAlive)
            {
                return(CompletionBehavior.Success | CompletionBehavior.KeepConsoleOpen);
            }
            else
            {
                return(CompletionBehavior.Success | CompletionBehavior.ExitConsole);
            }
        }
Exemple #12
0
        static async Task TestNonFlashDeviceManagerCommands()
        {
            try
            {
                var meadow = await MeadowDeviceManager.GetMeadowForSerialPort("COM3");

                Console.WriteLine("SetTraceLevel");
                await MeadowDeviceManager.SetTraceLevel(meadow, 1);

                // does not sent Concluded
                //Console.WriteLine("ResetMeadow");
                //await MeadowDeviceManager.ResetMeadow(meadow);

                // not implemented
                //Console.WriteLine("EnterDfuMode");
                //await MeadowDeviceManager.EnterDfuMode(meadow);

                // request rejected; unknown command
                //Console.WriteLine("NshEnable");
                //await MeadowDeviceManager.NshEnable(meadow);

                Console.WriteLine("MonoRunState");
                await MeadowDeviceManager.MonoRunState(meadow);

                Console.WriteLine("GetDeviceInfo");
                await MeadowDeviceManager.GetDeviceInfo(meadow);

                Console.WriteLine("SetDeveloper1");
                await MeadowDeviceManager.SetDeveloper1(meadow, 1);

                Console.WriteLine("SetDeveloper2");
                await MeadowDeviceManager.SetDeveloper2(meadow, 1);

                Console.WriteLine("SetDeveloper3");
                await MeadowDeviceManager.SetDeveloper3(meadow, 1);

                Console.WriteLine("SetDeveloper4");
                await MeadowDeviceManager.SetDeveloper4(meadow, 1);

                Console.WriteLine("TraceDisable");
                await MeadowDeviceManager.TraceDisable(meadow);

                Console.WriteLine("TraceEnable");
                await MeadowDeviceManager.TraceEnable(meadow);

                Console.WriteLine("Uart1Apps");
                await MeadowDeviceManager.Uart1Apps(meadow);

                Console.WriteLine("Uart1Trace");
                await MeadowDeviceManager.Uart1Trace(meadow);

                // restarts device. send reconnect?
                //Console.WriteLine("RenewFileSys");
                //await MeadowDeviceManager.RenewFileSys(meadow);

                // request rejected; unknown command
                //Console.WriteLine("QspiWrite");
                //await MeadowDeviceManager.QspiWrite(meadow, 1);

                // request rejected; unknown command
                //Console.WriteLine("QspiRead");
                //await MeadowDeviceManager.QspiRead(meadow, 1);

                //request rejected; unknown command
                //Console.WriteLine("QspiInit");
                //await MeadowDeviceManager.QspiInit(meadow, 1);

                // mono needs to be disabled for the ESP commands
                await MeadowDeviceManager.MonoDisable(meadow);

                Console.WriteLine("Esp32ReadMac");
                await MeadowDeviceManager.Esp32ReadMac(meadow);

                Console.WriteLine("Esp32Restart");
                await MeadowDeviceManager.Esp32Restart(meadow);
            }
            catch (MeadowDeviceManagerException ex)
            {
                Console.WriteLine(ex.HcomMeadowRequestType);
            }
        }
Exemple #13
0
        //Probably rename

        static async Task ProcessHcom(Options options)
        {
            if (string.IsNullOrEmpty(options.SerialPort))
            {
                Console.WriteLine("Please specify a --SerialPort");
                return;
            }

            MeadowSerialDevice device = null;

            try
            {
                Console.WriteLine($"Opening port '{options.SerialPort}'");
                device = await MeadowDeviceManager.GetMeadowForSerialPort(options.SerialPort);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error connecting to device: {ex.Message}");
                return;
            }

            using (device)
            {
                // verify that the port was actually connected
                if (device.Socket == null && device.SerialPort == null)
                {
                    Console.WriteLine($"Port is unavailable.");
                    return;
                }

                try
                {
                    if (options.WriteFile.Any())
                    {
                        string[] parameters = options.WriteFile.ToArray();

                        string[] files = parameters[0].Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                        string[] targetFileNames;

                        if (parameters.Length == 1)
                        {
                            targetFileNames = new string[files.Length];
                        }
                        else
                        {
                            targetFileNames = parameters[1].Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                        }

                        if (files.Length != targetFileNames.Length)
                        {
                            Console.WriteLine($"Number of files to write ({files.Length}) does not match the number of target file names ({targetFileNames.Length}).");
                        }
                        else
                        {
                            for (int i = 0; i < files.Length; i++)
                            {
                                string targetFileName = targetFileNames[i];

                                if (String.IsNullOrEmpty(targetFileName))
                                {
                                    targetFileName = null;
                                }

                                if (!File.Exists(files[i]))
                                {
                                    Console.WriteLine($"Cannot find {files[i]}");
                                }
                                else
                                {
                                    if (string.IsNullOrEmpty(targetFileName))
                                    {
#if USE_PARTITIONS
                                        Console.WriteLine($"Writing {files[i]} to partition {options.Partition}");
#else
                                        Console.WriteLine($"Writing {files[i]}");
#endif
                                    }
                                    else
                                    {
#if USE_PARTITIONS
                                        Console.WriteLine($"Writing {files[i]} as {targetFileName} to partition {options.Partition}");
#else
                                        Console.WriteLine($"Writing {files[i]} as {targetFileName}");
#endif
                                    }

                                    await MeadowFileManager.WriteFileToFlash(device, files[i], targetFileName, options.Partition).ConfigureAwait(false);
                                }
                            }
                        }
                    }
                    else if (options.DeleteFile.Any())
                    {
                        string[] parameters = options.DeleteFile.ToArray();
                        string[] files      = parameters[0].Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

                        foreach (string file in files)
                        {
                            if (!String.IsNullOrEmpty(file))
                            {
#if USE_PARTITIONS
                                Console.WriteLine($"Deleting {file} from partion {options.Partition}");
#else
                                Console.WriteLine($"Deleting {file}");
#endif
                                await MeadowFileManager.DeleteFile(device, file, options.Partition);
                            }
                        }
                    }
                    else if (options.EraseFlash)
                    {
                        Console.WriteLine("Erasing flash");
                        await MeadowFileManager.EraseFlash(device);
                    }
                    else if (options.VerifyErasedFlash)
                    {
                        Console.WriteLine("Verifying flash is erased");
                        await MeadowFileManager.VerifyErasedFlash(device);
                    }
                    else if (options.PartitionFileSystem)
                    {
                        Console.WriteLine($"Partioning file system into {options.NumberOfPartitions} partition(s)");
                        await MeadowFileManager.PartitionFileSystem(device, options.NumberOfPartitions);
                    }
                    else if (options.MountFileSystem)
                    {
#if USE_PARTITIONS
                        Console.WriteLine($"Mounting partition {options.Partition}");
#else
                        Console.WriteLine("Mounting file system");
#endif
                        await MeadowFileManager.MountFileSystem(device, options.Partition);
                    }
                    else if (options.InitFileSystem)
                    {
#if USE_PARTITIONS
                        Console.WriteLine($"Intializing filesystem in partition {options.Partition}");
#else
                        Console.WriteLine("Intializing filesystem");
#endif
                        await MeadowFileManager.InitializeFileSystem(device, options.Partition);
                    }
                    else if (options.CreateFileSystem) //should this have a partition???
                    {
                        Console.WriteLine($"Creating file system");
                        await MeadowFileManager.CreateFileSystem(device);
                    }
                    else if (options.FormatFileSystem)
                    {
#if USE_PARTITIONS
                        Console.WriteLine($"Format file system on partition {options.Partition}");
#else
                        Console.WriteLine("Format file system");
#endif
                        await MeadowFileManager.FormatFileSystem(device, options.Partition);
                    }
                    else if (options.ListFiles)
                    {
#if USE_PARTITIONS
                        Console.WriteLine($"Getting list of files on partition {options.Partition}");
#else
                        Console.WriteLine($"Getting list of files");
#endif
                        await MeadowFileManager.ListFiles(device, options.Partition);
                    }
                    else if (options.ListFilesAndCrcs)
                    {
#if USE_PARTITIONS
                        Console.WriteLine($"Getting list of files and CRCs on partition {options.Partition}");
#else
                        Console.WriteLine("Getting list of files and CRCs");
#endif
                        await MeadowFileManager.ListFilesAndCrcs(device, options.Partition);
                    }
                    else if (options.SetTraceLevel)
                    {
                        Console.WriteLine($"Setting trace level to {options.TraceLevel}");
                        await MeadowDeviceManager.SetTraceLevel(device, options.TraceLevel);
                    }
                    else if (options.SetDeveloper1)
                    {
                        Console.WriteLine($"Setting developer level to {options.DeveloperValue}");
                        await MeadowDeviceManager.SetDeveloper1(device, options.DeveloperValue);
                    }
                    else if (options.SetDeveloper2)
                    {
                        Console.WriteLine($"Setting developer level to {options.DeveloperValue}");
                        await MeadowDeviceManager.SetDeveloper2(device, options.DeveloperValue);
                    }
                    else if (options.SetDeveloper3)
                    {
                        Console.WriteLine($"Setting developer level to {options.DeveloperValue}");
                        await MeadowDeviceManager.SetDeveloper3(device, options.DeveloperValue);
                    }
                    else if (options.SetDeveloper4)
                    {
                        Console.WriteLine($"Setting developer level to {options.DeveloperValue}");
                        await MeadowDeviceManager.SetDeveloper4(device, options.DeveloperValue);
                    }
                    else if (options.NshEnable)
                    {
                        Console.WriteLine($"Enable Nsh");
                        await MeadowDeviceManager.NshEnable(device);
                    }
                    else if (options.MonoDisable)
                    {
                        await MeadowDeviceManager.MonoDisable(device);
                    }
                    else if (options.MonoEnable)
                    {
                        await MeadowDeviceManager.MonoEnable(device);
                    }
                    else if (options.MonoRunState)
                    {
                        await MeadowDeviceManager.MonoRunState(device);
                    }
                    else if (options.MonoFlash)
                    {
                        await MeadowDeviceManager.MonoFlash(device);
                    }
                    else if (options.MonoUpdateRt)
                    {
                        string sourcefilename = options.FileName;
                        if (string.IsNullOrWhiteSpace(sourcefilename))
                        {
                            // check local override
                            sourcefilename = Path.Combine(Directory.GetCurrentDirectory(), DownloadManager.RuntimeFilename);
                            if (File.Exists(sourcefilename))
                            {
                                Console.WriteLine($"Using current directory '{DownloadManager.RuntimeFilename}'");
                            }
                            else
                            {
                                sourcefilename = Path.Combine(DownloadManager.FirmwareDownloadsFilePath, DownloadManager.RuntimeFilename);
                                if (File.Exists(sourcefilename))
                                {
                                    Console.WriteLine("FileName not specified, using latest download.");
                                }
                                else
                                {
                                    Console.WriteLine("Unable to locate a runtime file. Either provide a path or download one.");
                                    return; // KeepConsoleOpen?
                                }
                            }
                        }

                        if (!File.Exists(sourcefilename))
                        {
                            Console.WriteLine($"File '{sourcefilename}' not found");
                            return; // KeepConsoleOpen?
                        }

                        await MeadowFileManager.MonoUpdateRt(device, sourcefilename, options.TargetFileName, options.Partition);
                    }
                    else if (options.GetDeviceInfo)
                    {
                        await MeadowDeviceManager.GetDeviceInfo(device);
                    }
                    else if (options.GetDeviceName)
                    {
                        await MeadowDeviceManager.GetDeviceName(device);
                    }
                    else if (options.ResetMeadow)
                    {
                        Console.WriteLine("Resetting Meadow");
                        await MeadowDeviceManager.ResetMeadow(device);
                    }
                    else if (options.EnterDfuMode)
                    {
                        Console.WriteLine("Entering Dfu mode");
                        await MeadowDeviceManager.EnterDfuMode(device);
                    }
                    else if (options.TraceDisable)
                    {
                        Console.WriteLine("Disabling Meadow trace messages");
                        await MeadowDeviceManager.TraceDisable(device);
                    }
                    else if (options.TraceEnable)
                    {
                        Console.WriteLine("Enabling Meadow trace messages");
                        await MeadowDeviceManager.TraceEnable(device);
                    }
                    else if (options.Uart1Apps)
                    {
                        Console.WriteLine("Use Uart1 for .NET Apps");
                        await MeadowDeviceManager.Uart1Apps(device);
                    }
                    else if (options.Uart1Trace)
                    {
                        Console.WriteLine("Use Uart1 for outputting Meadow trace messages");
                        await MeadowDeviceManager.Uart1Trace(device);
                    }
                    else if (options.RenewFileSys)
                    {
                        Console.WriteLine("Recreate a new file system on Meadow");
                        await MeadowDeviceManager.RenewFileSys(device);
                    }
                    else if (options.QspiWrite)
                    {
                        Console.WriteLine($"Executing QSPI Flash Write using {options.DeveloperValue}");
                        await MeadowDeviceManager.QspiWrite(device, options.DeveloperValue);
                    }
                    else if (options.QspiRead)
                    {
                        Console.WriteLine($"Executing QSPI Flash Read using {options.DeveloperValue}");
                        await MeadowDeviceManager.QspiRead(device, options.DeveloperValue);
                    }
                    else if (options.QspiInit)
                    {
                        Console.WriteLine($"Executing QSPI Flash Initialization using {options.DeveloperValue}");
                        await MeadowDeviceManager.QspiInit(device, options.DeveloperValue);
                    }
                    else if (options.StartDebugging)
                    {
                        MeadowDeviceManager.StartDebugging(device, options.VSDebugPort);
                        Console.WriteLine($"Ready for Visual Studio debugging");
                        options.KeepAlive = true;
                    }
                    else if (options.Esp32WriteFile)
                    {
                        if (string.IsNullOrEmpty(options.FileName))
                        {
                            Console.WriteLine($"option --Esp32WriteFile requires option --File (the local file you wish to write)");
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(options.TargetFileName))
                            {
                                Console.WriteLine($"Writing {options.FileName} to ESP32");
                            }
                            else
                            {
                                Console.WriteLine($"Writing {options.FileName} as {options.TargetFileName}");
                            }
                            await MeadowFileManager.WriteFileToEspFlash(device,
                                                                        options.FileName, options.TargetFileName, options.Partition, options.McuDestAddr);
                        }
                    }
                    else if (options.FlashEsp)
                    {
                        Console.WriteLine($"Transferring {DownloadManager.NetworkMeadowCommsFilename}");
                        await MeadowFileManager.WriteFileToEspFlash(device,
                                                                    Path.Combine(DownloadManager.FirmwareDownloadsFilePath, DownloadManager.NetworkMeadowCommsFilename), mcuDestAddr : "0x10000");

                        await Task.Delay(1000);

                        Console.WriteLine($"Transferring {DownloadManager.NetworkBootloaderFilename}");
                        await MeadowFileManager.WriteFileToEspFlash(device,
                                                                    Path.Combine(DownloadManager.FirmwareDownloadsFilePath, DownloadManager.NetworkBootloaderFilename), mcuDestAddr : "0x1000");

                        await Task.Delay(1000);

                        Console.WriteLine($"Transferring {DownloadManager.NetworkPartitionTableFilename}");
                        await MeadowFileManager.WriteFileToEspFlash(device,
                                                                    Path.Combine(DownloadManager.FirmwareDownloadsFilePath, DownloadManager.NetworkPartitionTableFilename), mcuDestAddr : "0x8000");

                        await Task.Delay(1000);
                    }
                    else if (options.Esp32ReadMac)
                    {
                        await MeadowDeviceManager.Esp32ReadMac(device);
                    }
                    else if (options.Esp32Restart)
                    {
                        await MeadowDeviceManager.Esp32Restart(device);
                    }
                    else if (options.DeployApp && !string.IsNullOrEmpty(options.FileName))
                    {
                        await MeadowDeviceManager.DeployApp(device, options.FileName);
                    }
                    else if (options.RegisterDevice)
                    {
                        var sn = await MeadowDeviceManager.GetDeviceSerialNumber(device);

                        if (string.IsNullOrEmpty(sn))
                        {
                            Console.WriteLine("Could not get device serial number. Reconnect device and try again.");
                            return;
                        }

                        Console.WriteLine($"Registering device {sn}");

                        DeviceRepository repository = new DeviceRepository();
                        var result = await repository.AddDevice(sn);

                        if (result.isSuccess)
                        {
                            Console.WriteLine("Device registration complete");
                        }
                        else
                        {
                            Console.WriteLine(result.message);
                        }
                    }

                    if (options.KeepAlive)
                    {
                        Console.Read();
                    }
                }
                catch (IOException ex)
                {
                    if (ex.Message.Contains("semaphore"))
                    {
                        if (ex.Message.Contains("semaphore"))
                        {
                            Console.WriteLine("Timeout communicating with Meadow");
                        }
                        else
                        {
                            Console.WriteLine($"Unexpected error occurred: {ex.Message}");
                        }
                        return; // KeepConsoleOpen?
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Unexpected error occurred: {ex.Message}");
                    return; // KeepConsoleOpen?
                }
            }
        }