Skip to content

Commit

Permalink
Merge pull request #367 from WildernessLabs/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
ctacke authored Oct 4, 2023
2 parents e96ff20 + e0fc344 commit 9087d5b
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 89 deletions.
22 changes: 15 additions & 7 deletions Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,32 @@ public BaseCommand(ILoggerFactory? loggerFactory)
}

protected abstract ValueTask ExecuteCommand();
protected virtual Task BeforeExecute() { return Task.CompletedTask; }

public virtual async ValueTask ExecuteAsync(IConsole console)
public async ValueTask ExecuteAsync(IConsole console)
{
try
{
SetConsole(console);
Console = console;
CancellationToken = Console.RegisterCancellationHandler();

await BeforeExecute();
await ExecuteCommand();
}
catch (Exception ex)
{
Logger?.LogError(ex.Message);
return;
}
}

protected void SetConsole(IConsole console)
{
Console = console;
CancellationToken = Console.RegisterCancellationHandler();
if (CancellationToken.IsCancellationRequested)
{
Logger?.LogInformation($"Cancelled.");
}
else
{
Logger?.LogInformation($"Done.");
}
}

}
8 changes: 2 additions & 6 deletions Source/v2/Meadow.Cli/Commands/Current/BaseDeviceCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Infrastructure;
using Meadow.Hcom;
using Meadow.Hcom;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down Expand Up @@ -41,7 +40,6 @@ protected async Task RefreshConnection()
}
else
{
await ExecuteCommand();
Logger?.LogInformation($"Done.");
}
}
Expand All @@ -60,10 +58,8 @@ protected async Task RefreshConnection()
}
}

public override async ValueTask ExecuteAsync(IConsole console)
protected override async Task BeforeExecute()
{
SetConsole(console);

await RefreshConnection();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.CLI.Core.Internals.Dfu;
using Meadow.LibUsb;
Expand Down Expand Up @@ -39,7 +38,7 @@ public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileMa
Settings = settingsManager;
}

public override async ValueTask ExecuteAsync(IConsole console)
protected override async ValueTask ExecuteCommand()
{
var package = await GetSelectedPackage();

Expand Down Expand Up @@ -77,17 +76,18 @@ public override async ValueTask ExecuteAsync(IConsole console)
var initialPorts = await MeadowConnectionManager.GetSerialPorts();

// get the device's serial number via DFU - we'll need it to find the device after it resets
ILibUsbDevice libUsbDevice;
try
{
_libUsbDevice = GetLibUsbDeviceForCurrentEnvironment();
libUsbDevice = GetLibUsbDeviceForCurrentEnvironment();
}
catch (Exception ex)
{
Logger?.LogError(ex.Message);
return;
}

var serial = _libUsbDevice.GetDeviceSerialNumber();
var serial = libUsbDevice.GetDeviceSerialNumber();

// no connection is required here - in fact one won't exist
// unless maybe we add a "DFUConnection"?
Expand Down Expand Up @@ -137,16 +137,19 @@ public override async ValueTask ExecuteAsync(IConsole console)
return;
}

var cancellationToken = console.RegisterCancellationHandler();

if (Files.Any(f => f != FirmwareType.OS))
{
await CurrentConnection.WaitForMeadowAttach();

await ExecuteCommand();
if (CancellationToken.IsCancellationRequested)
{
return;
}

await WriteFiles();
}

var deviceInfo = await CurrentConnection.Device.GetDeviceInfo(cancellationToken);
var deviceInfo = await CurrentConnection.Device.GetDeviceInfo(CancellationToken);

if (deviceInfo != null)
{
Expand All @@ -156,36 +159,42 @@ public override async ValueTask ExecuteAsync(IConsole console)
}
else
{
await base.ExecuteAsync(console);
await WriteFiles();
}
}

private ILibUsbDevice GetLibUsbDeviceForCurrentEnvironment()
{
ILibUsbProvider provider;

// TODO: read the settings manager to decide which provider to use (default to non-classic)
var setting = Settings.GetAppSetting(SettingsManager.PublicSettings.LibUsb);
if (setting == "classic")
if (_libUsbDevice == null)
{
provider = new ClassicLibUsbProvider();
}
else
{
provider = new LibUsbProvider();
}
ILibUsbProvider provider;

// TODO: read the settings manager to decide which provider to use (default to non-classic)
var setting = Settings.GetAppSetting(SettingsManager.PublicSettings.LibUsb);
if (setting == "classic")
{
provider = new ClassicLibUsbProvider();
}
else
{
provider = new LibUsbProvider();
}

var devices = provider.GetDevicesInBootloaderMode();
var devices = provider.GetDevicesInBootloaderMode();

switch (devices.Count)
{
case 0:
throw new Exception("No device found in bootloader mode");
case 1:
return devices[0];
default:
throw new Exception("Multiple devices found in bootloader mode. Disconnect all but one");
switch (devices.Count)
{
case 0:
throw new Exception("No device found in bootloader mode");
case 1:
_libUsbDevice = devices[0];
break;
default:
throw new Exception("Multiple devices found in bootloader mode. Disconnect all but one");
}
}

return _libUsbDevice;
}

private async Task<FirmwarePackage?> GetSelectedPackage()
Expand Down Expand Up @@ -218,7 +227,7 @@ private ILibUsbDevice GetLibUsbDeviceForCurrentEnvironment()
return package;
}

protected override async ValueTask ExecuteCommand()
private async ValueTask WriteFiles()
{
// the connection passes messages back to us (info about actions happening on-device
CurrentConnection.DeviceMessageReceived += (s, e) =>
Expand Down Expand Up @@ -277,6 +286,12 @@ protected override async ValueTask ExecuteCommand()

await CurrentConnection.Device.WriteRuntime(rtpath, CancellationToken);
}

if (CancellationToken.IsCancellationRequested)
{
return;
}

if (Files.Contains(FirmwareType.ESP))
{
Logger?.LogInformation($"{Environment.NewLine}Writing Coprocessor files...");
Expand All @@ -289,13 +304,18 @@ protected override async ValueTask ExecuteCommand()
};

await CurrentConnection.Device.WriteCoprocessorFiles(fileList, CancellationToken);

if (CancellationToken.IsCancellationRequested)
{
return;
}
}

Logger?.LogInformation($"{Environment.NewLine}");

if (wasRuntimeEnabled)
{
await CurrentConnection.Device.RuntimeEnable();
await CurrentConnection.Device.RuntimeEnable(CancellationToken);
}

// TODO: if we're an F7 device, we need to reset
Expand Down
11 changes: 5 additions & 6 deletions Source/v2/Meadow.Cli/Commands/Legacy/FlashOsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.CLI.Core.Internals.Dfu;
using Meadow.LibUsb;
Expand Down Expand Up @@ -49,7 +48,7 @@ public FlashOsCommand(ISettingsManager settingsManager, FileManager fileManager,
Settings = settingsManager;
}

public override async ValueTask ExecuteAsync(IConsole console)
protected override async ValueTask ExecuteCommand()
{
var package = await GetSelectedPackage();

Expand Down Expand Up @@ -150,13 +149,13 @@ public override async ValueTask ExecuteAsync(IConsole console)
return;
}

var cancellationToken = console.RegisterCancellationHandler();
var cancellationToken = Console.RegisterCancellationHandler();

if (Files.Any(f => f != FirmwareType.OS))
{
await connection.WaitForMeadowAttach();

await ExecuteCommand();
await WriteFiles();
}

var deviceInfo = await connection.Device.GetDeviceInfo(cancellationToken);
Expand All @@ -169,7 +168,7 @@ public override async ValueTask ExecuteAsync(IConsole console)
}
else
{
await base.ExecuteAsync(console);
await WriteFiles();
}
}

Expand Down Expand Up @@ -231,7 +230,7 @@ private ILibUsbDevice GetLibUsbDeviceForCurrentEnvironment()
return package;
}

protected override async ValueTask ExecuteCommand()
private async ValueTask WriteFiles()
{
if (CurrentConnection == null)
{
Expand Down
5 changes: 2 additions & 3 deletions Source/v2/Meadow.Cli/Commands/Legacy/ListPortsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Microsoft.Extensions.Logging;

Expand All @@ -14,9 +13,9 @@ public ListPortsCommand(ISettingsManager settingsManager, ILoggerFactory loggerF
Logger?.LogWarning($"Deprecated command. Use `port list` instead");
}

public override ValueTask ExecuteAsync(IConsole console)
protected override ValueTask ExecuteCommand()
{
return base.ExecuteAsync(console);
return base.ExecuteCommand();
}
}

35 changes: 21 additions & 14 deletions Source/v2/Meadow.Hcom/DeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@ internal DeviceInfo(Dictionary<string, string> properties)
Properties = properties;
}

public string this[string propname] => Properties[propname];
public string Product => this["Product"];
public string Model => this["Model"];
public string ProcessorType => this["ProcessorType"];
public string CoprocessorType => this["CoprocessorType"];
public string OsVersion => this["OSVersion"];
public string CoprocessorOsVersion => this["CoprocessorVersion"];
public string ProcessorId => this["ProcessorId"];
public string HardwareVersion => this["Hardware"];
public string DeviceName => this["DeviceName"];
public string RuntimeVersion => this["MonoVersion"];
public string SerialNumber => this["SerialNo"];
public string MacAddress => this["WiFiMAC"];
public string SoftAPMacAddress => this["SoftAPMac"];
public string? this[string propname]
{
get
{
return Properties.Keys.Contains(propname) ? Properties[propname] : null;
}
}

public string? Product => this["Product"];
public string? Model => this["Model"];
public string? ProcessorType => this["ProcessorType"];
public string? CoprocessorType => this["CoprocessorType"];
public string? OsVersion => this["OSVersion"];
public string? CoprocessorOsVersion => this["CoprocessorVersion"];
public string? ProcessorId => this["ProcessorId"];
public string? HardwareVersion => this["Hardware"];
public string? DeviceName => this["DeviceName"];
public string? RuntimeVersion => this["MonoVersion"];
public string? SerialNumber => this["SerialNo"];
public string? MacAddress => this["WiFiMAC"];
public string? SoftAPMacAddress => this["SoftAPMac"];

/// <summary>
/// String representation of an unknown MAC address.
Expand Down
Loading

0 comments on commit 9087d5b

Please sign in to comment.