Skip to content

Commit

Permalink
Minor refactor so we can confirm a connection before updating the route
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed Mar 6, 2024
1 parent ea406a8 commit 0ba6749
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileMa

connection.FileWriteProgress += (s, e) =>
{
var p = (int)((e.completed / (double)e.total) * 100d);
var p = (int)(e.completed / (double)e.total * 100d);
// don't report < 10% increments (decrease spew on large files)
if (p - _lastWriteProgress < 10) { return; }
Expand Down Expand Up @@ -179,6 +179,9 @@ protected override async ValueTask ExecuteCommand()

if (UseDfu || dfuDevice != null || osFileWithoutBootloader == null || RequiresDfuForRuntimeUpdates(deviceInfo!))
{
// get a list of ports - it will not have our meadow in it (since it should be in DFU mode)
var initialPorts = await MeadowConnectionManager.GetSerialPorts();

if (await WriteOsWithDfu(dfuDevice, osFileWithBootloader) == false)
{
throw new CommandException(Strings.DfuWriteFailed);
Expand All @@ -188,9 +191,16 @@ protected override async ValueTask ExecuteCommand()

await Task.Delay(1500);

var newPort = await WaitForNewSerialPort(initialPorts);

Logger?.LogInformation($"Meadow found at {newPort}");

connection = await GetConnectionAndDisableRuntime();

await connection.WaitForMeadowAttach();

// configure the route to that port for the user
Settings.SaveSetting(SettingsManager.PublicSettings.Route, newPort);
}
else
{
Expand Down Expand Up @@ -401,9 +411,6 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi

private async Task<bool> WriteOsWithDfu(ILibUsbDevice? libUsbDevice, string osFile)
{
// get a list of ports - it will not have our meadow in it (since it should be in DFU mode)
var initialPorts = await MeadowConnectionManager.GetSerialPorts();

// get the device's serial number via DFU - we'll need it to find the device after it resets
if (libUsbDevice == null)
{
Expand Down Expand Up @@ -438,7 +445,7 @@ await DfuUtils.FlashFile(
}
catch (ArgumentException)
{
Logger?.LogWarning("Unable to write firmware with Dfu - is Dfu-util installed? Run `meadow dfu install` to install");
Logger?.LogWarning("Unable to write firmware - is Dfu-util installed? Run `meadow dfu install` to install");
return false;
}
catch (Exception ex)
Expand All @@ -452,14 +459,6 @@ await DfuUtils.FlashFile(
Logger?.LogWarning("This machine requires an older version of LibUsb. The CLI settings have been updated, re-run the 'firmware write' command to update your device.");
return false;
}

var newPort = await WaitForNewSerialPort(initialPorts);

Logger?.LogInformation($"Meadow found at {newPort}");

// configure the route to that port for the user
Settings.SaveSetting(SettingsManager.PublicSettings.Route, newPort);

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
},
"Firmware Write version": {
"commandName": "Project",
"commandLineArgs": "firmware write -v 1.8.1.16"
"commandLineArgs": "firmware write -v 1.9.1.7"
},
"Firmware Write runtime": {
"commandName": "Project",
Expand Down

0 comments on commit 0ba6749

Please sign in to comment.