From 6a2812d6b207a2c8db7d212086a731442261a793 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Tue, 6 Feb 2024 13:22:06 -0600 Subject: [PATCH 1/5] Trying to get DFU->HCOM transition to not die --- .../Commands/Current/Firmware/FirmwareWriteCommand.cs | 2 ++ Source/v2/Meadow.UsbLib.Core/ILibUsbDevice.cs | 2 +- Source/v2/Meadow.UsbLib/LibUsbDevice.cs | 5 +++++ Source/v2/Meadow.UsbLibClassic/ClassicLibUsbDevice.cs | 5 +++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs index ee182f2d..37af3175 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs @@ -174,6 +174,8 @@ protected override async ValueTask ExecuteCommand() return; } + dfuDevice?.Dispose(); + connection = await GetCurrentConnection(); await connection!.WaitForMeadowAttach(); diff --git a/Source/v2/Meadow.UsbLib.Core/ILibUsbDevice.cs b/Source/v2/Meadow.UsbLib.Core/ILibUsbDevice.cs index 0acb87ff..12f60856 100644 --- a/Source/v2/Meadow.UsbLib.Core/ILibUsbDevice.cs +++ b/Source/v2/Meadow.UsbLib.Core/ILibUsbDevice.cs @@ -5,7 +5,7 @@ public interface ILibUsbProvider List GetDevicesInBootloaderMode(); } -public interface ILibUsbDevice +public interface ILibUsbDevice : IDisposable { string GetDeviceSerialNumber(); } diff --git a/Source/v2/Meadow.UsbLib/LibUsbDevice.cs b/Source/v2/Meadow.UsbLib/LibUsbDevice.cs index d0a78185..82a967be 100644 --- a/Source/v2/Meadow.UsbLib/LibUsbDevice.cs +++ b/Source/v2/Meadow.UsbLib/LibUsbDevice.cs @@ -36,6 +36,11 @@ public LibUsbDevice(IUsbDevice usbDevice) _device = usbDevice; } + public void Dispose() + { + _device?.Dispose(); + } + public string GetDeviceSerialNumber() { var serialNumber = string.Empty; diff --git a/Source/v2/Meadow.UsbLibClassic/ClassicLibUsbDevice.cs b/Source/v2/Meadow.UsbLibClassic/ClassicLibUsbDevice.cs index 5b5a033f..9596261d 100644 --- a/Source/v2/Meadow.UsbLibClassic/ClassicLibUsbDevice.cs +++ b/Source/v2/Meadow.UsbLibClassic/ClassicLibUsbDevice.cs @@ -32,6 +32,11 @@ public ClassicLibUsbDevice(UsbRegistry usbDevice) _device = usbDevice; } + public void Dispose() + { + _device.Device.Close(); + } + public string GetDeviceSerialNumber() { if (_device != null && _device.DeviceProperties != null) From f59717c68fc704e1276817c8da17ade9eab48c48 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Tue, 6 Feb 2024 13:30:13 -0600 Subject: [PATCH 2/5] updated route error message --- Source/v2/Meadow.Cli/MeadowConnectionManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/v2/Meadow.Cli/MeadowConnectionManager.cs b/Source/v2/Meadow.Cli/MeadowConnectionManager.cs index 0e392ddd..f071c6af 100644 --- a/Source/v2/Meadow.Cli/MeadowConnectionManager.cs +++ b/Source/v2/Meadow.Cli/MeadowConnectionManager.cs @@ -26,7 +26,7 @@ public MeadowConnectionManager(ISettingsManager settingsManager) if (route == null) { - throw new Exception("No 'route' configuration set"); + throw new Exception($"No 'route' configuration set.{Environment.NewLine}Use the `meadow config route` command. For example:{Environment.NewLine} > meadow config route COM5"); } // TODO: support connection changing (CLI does this rarely as it creates a new connection with each command) From a5f64ed1e956a75a4e945c3f24938dd6934a7592 Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Tue, 6 Feb 2024 12:56:46 -0800 Subject: [PATCH 3/5] Minor cleanup --- .../Current/Firmware/FirmwareWriteCommand.cs | 54 +++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs index 37af3175..8987ddcf 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs @@ -65,13 +65,12 @@ private bool RequiresDfuForRuntimeUpdates(DeviceInfo info) { if (System.Version.TryParse(info.OsVersion, out var version)) { - switch (version.Major) + return version.Major switch { - case 0: return true; - case 1: - return version.Minor < 8; - default: return false; - } + 0 => true, + 1 => version.Minor < 8, + _ => false, + }; } return true; @@ -81,13 +80,12 @@ private bool RequiresDfuForEspUpdates(DeviceInfo info) { if (System.Version.TryParse(info.OsVersion, out var version)) { - switch (version.Major) + return version.Major switch { - case 0: return true; - case 1: - return version.Minor < 9; - default: return false; - } + 0 => true, + 1 => version.Minor < 9, + _ => false, + }; } return true; @@ -177,7 +175,14 @@ protected override async ValueTask ExecuteCommand() dfuDevice?.Dispose(); connection = await GetCurrentConnection(); - await connection!.WaitForMeadowAttach(); + + if (connection == null) + { + Logger?.LogError("No device found"); + return; + } + + await connection.WaitForMeadowAttach(); } else @@ -189,7 +194,7 @@ protected override async ValueTask ExecuteCommand() if (FirmwareFileTypes.Contains(FirmwareType.Runtime) || Path.GetFileName(IndividualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.RuntimeFile) { connection = await WriteFirmware(connection, deviceInfo, package); - if (connection == null) return; + if (connection == null) { return; } } if (FirmwareFileTypes.Contains(FirmwareType.ESP) @@ -206,11 +211,9 @@ protected override async ValueTask ExecuteCommand() private async Task WriteFirmware(IMeadowConnection? connection, DeviceInfo? deviceInfo, FirmwarePackage package) { - if (connection == null) - { - connection = await GetConnectionAndDisableRuntime(); - if (connection == null) return null; // couldn't find a connected device - } + connection ??= await GetConnectionAndDisableRuntime(); + + if (connection == null) { return null; } // couldn't find a connected device Logger?.LogInformation($"{Environment.NewLine}Writing Runtime {package.Version}..."); @@ -242,11 +245,9 @@ protected override async ValueTask ExecuteCommand() private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? deviceInfo, FirmwarePackage package) { - if (connection == null) - { - connection = await GetConnectionAndDisableRuntime(); - if (connection == null) return; // couldn't find a connected device - } + connection ??= await GetConnectionAndDisableRuntime(); + + if (connection == null) { return; }// couldn't find a connected device connection.DeviceMessageReceived += (s, e) => { @@ -281,10 +282,7 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi }; } - if (deviceInfo == null) - { - deviceInfo = await connection.GetDeviceInfo(CancellationToken); - } + deviceInfo ??= await connection.GetDeviceInfo(CancellationToken); if (UseDfu || RequiresDfuForEspUpdates(deviceInfo)) { From 568d4d3d53651e02ee14e164cf35a7388bedee8e Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Tue, 6 Feb 2024 18:37:33 -0600 Subject: [PATCH 4/5] more firmware stuff --- .../Current/Firmware/FirmwareWriteCommand.cs | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs index 8987ddcf..04d96265 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs @@ -52,7 +52,21 @@ public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileMa connection.FileWriteProgress += (s, e) => { var p = (e.completed / (double)e.total) * 100d; - Console?.Output.Write($"Writing {e.fileName}: {p:0}% \r"); + Console?.Output.Write($"Writing {e.fileName}: {p:0}% {(p < 100 ? "\n" : "\r\n")}"); + }; + connection.DeviceMessageReceived += (s, e) => + { + if (e.message.Contains("% downloaded")) + { // don't echo this, as we're already reporting % written + } + else + { + Logger?.LogInformation(e.message); + } + }; + connection.ConnectionMessage += (s, message) => + { + Logger?.LogInformation(message); }; Logger?.LogInformation("Disabling device runtime..."); @@ -156,6 +170,7 @@ protected override async ValueTask ExecuteCommand() if (dfuDevice != null) { Logger?.LogInformation($"DFU device detected. Using DFU to write OS"); + UseDfu = true; } else { @@ -174,15 +189,8 @@ protected override async ValueTask ExecuteCommand() dfuDevice?.Dispose(); - connection = await GetCurrentConnection(); - - if (connection == null) - { - Logger?.LogError("No device found"); - return; - } - - await connection.WaitForMeadowAttach(); + connection = await GetConnectionAndDisableRuntime(); + await connection!.WaitForMeadowAttach(); } else @@ -194,7 +202,7 @@ protected override async ValueTask ExecuteCommand() if (FirmwareFileTypes.Contains(FirmwareType.Runtime) || Path.GetFileName(IndividualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.RuntimeFile) { connection = await WriteFirmware(connection, deviceInfo, package); - if (connection == null) { return; } + if (connection == null) return; } if (FirmwareFileTypes.Contains(FirmwareType.ESP) @@ -211,9 +219,11 @@ protected override async ValueTask ExecuteCommand() private async Task WriteFirmware(IMeadowConnection? connection, DeviceInfo? deviceInfo, FirmwarePackage package) { - connection ??= await GetConnectionAndDisableRuntime(); - - if (connection == null) { return null; } // couldn't find a connected device + if (connection == null) + { + connection = await GetConnectionAndDisableRuntime(); + if (connection == null) return null; // couldn't find a connected device + } Logger?.LogInformation($"{Environment.NewLine}Writing Runtime {package.Version}..."); @@ -227,6 +237,8 @@ protected override async ValueTask ExecuteCommand() if (UseDfu || RequiresDfuForRuntimeUpdates(deviceInfo)) { + await connection!.Device!.RuntimeDisable(CancellationToken); + write_runtime: if (!await connection!.Device!.WriteRuntime(rtpath, CancellationToken)) { @@ -245,24 +257,11 @@ protected override async ValueTask ExecuteCommand() private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? deviceInfo, FirmwarePackage package) { - connection ??= await GetConnectionAndDisableRuntime(); - - if (connection == null) { return; }// couldn't find a connected device - - connection.DeviceMessageReceived += (s, e) => - { - if (e.message.Contains("% downloaded")) - { // don't echo this, as we're already reporting % written - } - else - { - Logger?.LogInformation(e.message); - } - }; - connection.ConnectionMessage += (s, message) => + if (connection == null) { - Logger?.LogInformation(message); - }; + connection = await GetConnectionAndDisableRuntime(); + if (connection == null) return; // couldn't find a connected device + } Logger?.LogInformation($"{Environment.NewLine}Writing Coprocessor files..."); @@ -282,7 +281,10 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi }; } - deviceInfo ??= await connection.GetDeviceInfo(CancellationToken); + if (deviceInfo == null) + { + deviceInfo = await connection.GetDeviceInfo(CancellationToken); + } if (UseDfu || RequiresDfuForEspUpdates(deviceInfo)) { From fc67ee4162a558d2766115160283c74948db3390 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Tue, 6 Feb 2024 18:43:49 -0600 Subject: [PATCH 5/5] output cleanup --- .../Commands/Current/Firmware/FirmwareWriteCommand.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs index 04d96265..e5c1e589 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs @@ -39,6 +39,8 @@ public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileMa Settings = settingsManager; } + private double _lastWriteProgress = -1; + private async Task GetConnectionAndDisableRuntime() { var connection = await GetCurrentConnection(); @@ -52,7 +54,11 @@ public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileMa connection.FileWriteProgress += (s, e) => { var p = (e.completed / (double)e.total) * 100d; - Console?.Output.Write($"Writing {e.fileName}: {p:0}% {(p < 100 ? "\n" : "\r\n")}"); + if (p == _lastWriteProgress) { return; } + + _lastWriteProgress = p; + + Logger?.LogInformation($"Writing {e.fileName}: {p:0}% {(p < 100 ? "\n" : "\r\n")}"); }; connection.DeviceMessageReceived += (s, e) => {