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

Fix for bad serial number being returned by UsbLib on Mac + minor cleanup #505

Merged
merged 6 commits into from
Feb 27, 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 @@ -365,12 +365,32 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi

var devices = provider.GetDevicesInBootloaderMode();

return devices.Count switch
if (devices.Count == 0)
{
0 => null,
1 => devices[0],
_ => throw new CommandException("Multiple devices found in bootloader mode - only connect one device"),
};
return null;
}
else if (devices.Count == 1)
{
return devices[0];
}
else if (devices.Count == 2)
{ //this is a workaround for a specific case when a bad 2nd device is returned by the libusb provider on MacOS
//this fix is constrained to the known reproducible case
var serial2 = devices[1].GetDeviceSerialNumber();

if (serial2.Length > 12)
{
return devices[0];
}
else
{
throw new CommandException("Multiple devices found in bootloader mode - only connect one device");
}
}
else
{
throw new CommandException("Multiple devices found in bootloader mode - only connect one device");
}
}

private async Task<FirmwarePackage?> GetSelectedPackage()
Expand Down Expand Up @@ -421,10 +441,9 @@ private async Task<bool> WriteOsWithDfu(ILibUsbDevice? libUsbDevice, string osFi
}
}

string serialNumber;
try
{
serialNumber = libUsbDevice.GetDeviceSerialNumber();
{ //validate device
var serialNumber = libUsbDevice.GetDeviceSerialNumber();
}
catch
{
Expand All @@ -435,10 +454,10 @@ private async Task<bool> WriteOsWithDfu(ILibUsbDevice? libUsbDevice, string osFi
try
{
await DfuUtils.FlashFile(
osFile,
serialNumber,
logger: Logger,
format: DfuUtils.DfuFlashFormat.ConsoleOut);
osFile,
string.Empty, //serial number isn't needed to flash the OS and may cause issues on MacOS
logger: Logger,
format: DfuUtils.DfuFlashFormat.ConsoleOut);
}
catch (ArgumentException)
{
Expand Down
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.12.0-beta</PackageVersion>
<PackageVersion>2.0.16.0-beta</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Meadow.CLI
{
public static class Constants
{
public const string CLI_VERSION = "2.0.12.0";
public const string CLI_VERSION = "2.0.16.0";
}
}
17 changes: 13 additions & 4 deletions Source/v2/Meadow.Dfu/DfuUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static void FormatDfuOutput(string logLine, ILogger? logger, DfuFlashFor
}
}

public static async Task<bool> FlashFile(string fileName, string dfuSerialNumber, ILogger? logger = null, DfuFlashFormat format = DfuFlashFormat.Percent)
public static async Task<bool> FlashFile(string fileName, string? dfuSerialNumber, ILogger? logger = null, DfuFlashFormat format = DfuFlashFormat.Percent)
{
logger ??= NullLogger.Instance;

Expand All @@ -72,7 +72,7 @@ public static async Task<bool> FlashFile(string fileName, string dfuSerialNumber

logger.LogInformation($"Flashing OS with {fileName}");

var dfuUtilVersion = new System.Version(GetDfuUtilVersion());
var dfuUtilVersion = new Version(GetDfuUtilVersion());
logger.LogDebug("Detected OS: {os}", RuntimeInformation.OSDescription);

if (dfuUtilVersion == null)
Expand All @@ -91,7 +91,7 @@ public static async Task<bool> FlashFile(string fileName, string dfuSerialNumber
}
return false;
}
else if (dfuUtilVersion.CompareTo(new System.Version("0.11")) < 0)
else if (dfuUtilVersion.CompareTo(new Version("0.11")) < 0)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand All @@ -113,7 +113,16 @@ public static async Task<bool> FlashFile(string fileName, string dfuSerialNumber

try
{
var args = $"-a 0 -S {dfuSerialNumber} -D \"{fileName}\" -s {_osAddress}:leave";
string args;

if (string.IsNullOrWhiteSpace(dfuSerialNumber))
{
args = $"-a 0 -D \"{fileName}\" -s {_osAddress}:leave";
}
else
{
args = $"-a 0 -S {dfuSerialNumber} -D \"{fileName}\" -s {_osAddress}:leave";
}

await RunDfuUtil(args, logger, format);
}
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.UsbLib.Core/ILibUsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public interface ILibUsbProvider
public interface ILibUsbDevice : IDisposable
{
string GetDeviceSerialNumber();
}
}
3 changes: 1 addition & 2 deletions Source/v2/Meadow.UsbLib/LibUsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public List<ILibUsbDevice> GetDevicesInBootloaderMode()

public class LibUsbDevice : ILibUsbDevice
{
private IUsbDevice _device;
private readonly IUsbDevice _device;

public LibUsbDevice(IUsbDevice usbDevice)
{
Expand All @@ -52,7 +52,6 @@ public string GetDeviceSerialNumber()
_device.Close();
}


return serialNumber;
}
}
Expand Down
Loading