示例#1
0
        static async Task TestFileManagerCommands()
        {
            var meadow = await MeadowDeviceManager.GetMeadowForSerialPort("COM3");

            // request rejected; unknown command
            //Console.WriteLine("CreateFileSystem");
            //await MeadowFileManager.CreateFileSystem(meadow);

            Console.WriteLine("WriteFileToFlash");
            File.WriteAllText(".\\hello_meadow.txt", "test");
            await MeadowFileManager.WriteFileToFlash(meadow, @".\\hello_meadow.txt");

            // MonoUpdateRt, PartitionFileSystem, MountFileSystem, InitializeFileSystem, CreateFileSystem, FormatFileSystem - do we still need these?

            Console.WriteLine("ListFiles");
            await MeadowFileManager.ListFiles(meadow);

            Console.WriteLine("ListFilesAndCrcs");
            await MeadowFileManager.ListFilesAndCrcs(meadow);

            Console.WriteLine("DeleteFile");
            await MeadowFileManager.DeleteFile(meadow, @"hello_meadow.txt");

            Console.WriteLine("ListFiles");
            await MeadowFileManager.ListFiles(meadow);

            //await MeadowFileManager.EraseFlash(meadow);
            //await MeadowFileManager.VerifyErasedFlash(meadow);
        }
示例#2
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);
            }
        }
示例#3
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);
            }
        }
示例#4
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);
            }
        }
示例#5
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);
            }
        }
示例#6
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);
            }
        }
示例#7
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?
                }
            }
        }