Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better dfu flashing behavior #528

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected override ValueTask ExecuteCommand()
{
switch (Settings?.Length)
{
case null:
case 0:
// not valid
throw new CommandException($"No setting provided");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Meadow.LibUsb;
using Meadow.Software;
using Microsoft.Extensions.Logging;
using System.Runtime.InteropServices;

namespace Meadow.CLI.Commands.DeviceManagement;

Expand Down Expand Up @@ -169,7 +170,9 @@ protected override async ValueTask ExecuteCommand()
}

// do we have a dfu device attached, or is DFU specified?
var dfuDevice = GetLibUsbDeviceForCurrentEnvironment();
var provider = new LibUsbProvider();
var dfuDevice = GetLibUsbDeviceForCurrentEnvironment(provider);
bool ignoreSerial = IgnoreSerialNumberForDfu(provider);

if (dfuDevice != null)
{
Expand All @@ -195,7 +198,7 @@ protected override async ValueTask ExecuteCommand()

try
{
await WriteOsWithDfu(dfuDevice, osFileWithBootloader!);
await WriteOsWithDfu(dfuDevice!, osFileWithBootloader!, ignoreSerial);
}
finally
{
Expand Down Expand Up @@ -337,13 +340,7 @@ private async Task<IMeadowConnection> FindMeadowConnection(IList<string> portsTo

if (connection == null)
{
var newPort = await WaitForNewSerialPort(initialPorts);

if (newPort == null)
{
throw CommandException.MeadowDeviceNotFound;
}

var newPort = await WaitForNewSerialPort(initialPorts) ?? throw CommandException.MeadowDeviceNotFound;
connection = await GetCurrentConnection(true);

Logger?.LogInformation($"{Strings.MeadowFoundAt} {newPort}");
Expand Down Expand Up @@ -401,9 +398,9 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi
}
}

private ILibUsbDevice? GetLibUsbDeviceForCurrentEnvironment()
private ILibUsbDevice? GetLibUsbDeviceForCurrentEnvironment(LibUsbProvider? provider)
{
var provider = new LibUsbProvider();
provider ??= new LibUsbProvider();

var devices = provider.GetDevicesInBootloaderMode();

Expand All @@ -414,8 +411,8 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi
return null;
}

if (meadowsInDFU.Count == 1)
{
if (meadowsInDFU.Count == 1 || IgnoreSerialNumberForDfu(provider))
{ //IgnoreSerialNumberForDfu is a macOS-specific hack for Mark's machine
return meadowsInDFU.FirstOrDefault();
}

Expand Down Expand Up @@ -451,22 +448,13 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi
return package;
}

private async Task WriteOsWithDfu(ILibUsbDevice? libUsbDevice, string osFile)
private async Task WriteOsWithDfu(ILibUsbDevice libUsbDevice, string osFile, bool ignoreSerialNumber = false)
{
// get the device's serial number via DFU - we'll need it to find the device after it resets
if (libUsbDevice == null)
{
libUsbDevice = GetLibUsbDeviceForCurrentEnvironment();

if (libUsbDevice == null)
{
throw new CommandException(Strings.NoDfuDeviceDetected);
}
}
string serialNumber;

try
{ //validate device
var serialNumber = libUsbDevice.GetDeviceSerialNumber();
serialNumber = libUsbDevice.GetDeviceSerialNumber();
}
catch
{
Expand All @@ -475,9 +463,14 @@ private async Task WriteOsWithDfu(ILibUsbDevice? libUsbDevice, string osFile)

try
{
if (ignoreSerialNumber)
{
serialNumber = string.Empty;
}

await DfuUtils.FlashFile(
osFile,
string.Empty, //serial number isn't needed to flash the OS and may cause issues on MacOS
serialNumber,
logger: Logger,
format: DfuUtils.DfuFlashFormat.ConsoleOut);
}
Expand Down Expand Up @@ -526,4 +519,22 @@ private async Task<IList<string>> WaitForNewSerialPorts(IList<string>? ignorePor

return ports.FirstOrDefault();
}

private bool IgnoreSerialNumberForDfu(LibUsbProvider provider)
{ //hack check for Mark's Mac
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
var devices = provider.GetDevicesInBootloaderMode();

if (devices.Count == 2)
{
if (devices[0].GetDeviceSerialNumber().Length > 12 || devices[1].GetDeviceSerialNumber().Length > 12)
{
return true;
}
}
}

return false;
}
}
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Meadow.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Authors>Wilderness Labs, Inc</Authors>
<Company>Wilderness Labs, Inc</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.0.25.0</PackageVersion>
<PackageVersion>2.0.27.0</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
Expand Down
9 changes: 4 additions & 5 deletions Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
[assembly: System.Reflection.AssemblyVersion(Meadow.CLI.Constants.CLI_VERSION)]
[assembly: System.Reflection.AssemblyInformationalVersion(Meadow.CLI.Constants.CLI_VERSION)]

namespace Meadow.CLI
namespace Meadow.CLI;

public static class Constants
{
public static class Constants
{
public const string CLI_VERSION = "2.0.25.0";
}
public const string CLI_VERSION = "2.0.27.0";
}
2 changes: 0 additions & 2 deletions Source/v2/Meadow.UsbLib/LibUsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public List<ILibUsbDevice> GetDevicesInBootloaderMode()
.Select(d => new LibUsbDevice(d))
.ToList<ILibUsbDevice>();

UsbDevice device;

return _devices;
}

Expand Down
Loading