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

Serial wackamole #500

Merged
merged 9 commits into from
Feb 26, 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
4 changes: 2 additions & 2 deletions Source/v2/Meadow.Cli/Commands/Current/BaseDeviceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ protected async Task<IMeadowDevice> GetCurrentDevice()
return (await GetCurrentConnection()).Device ?? throw CommandException.MeadowDeviceNotFound;
}

protected async Task<IMeadowConnection> GetCurrentConnection()
protected async Task<IMeadowConnection> GetCurrentConnection(bool forceReconnect = false)
{
var connection = ConnectionManager.GetCurrentConnection();
var connection = ConnectionManager.GetCurrentConnection(forceReconnect);

if (connection != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileMa

private async Task<IMeadowConnection?> GetConnectionAndDisableRuntime()
{
var connection = await GetCurrentConnection();
var connection = await GetCurrentConnection(true);

if (connection == null || connection.Device == null)
{
// can't find a device
return null;
}
_lastWriteProgress = 0;

connection.FileWriteProgress += (s, e) =>
{
Expand Down Expand Up @@ -87,31 +83,16 @@ public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileMa

private bool RequiresDfuForRuntimeUpdates(DeviceInfo info)
{
return true;

if (System.Version.TryParse(info.OsVersion, out var version))
{
return version.Major switch
{
0 => true,
2 => version.Minor < 0,
_ => false,
};
return version.Major >= 2;
}

return true;
}

private bool RequiresDfuForEspUpdates(DeviceInfo info)
{
if (System.Version.TryParse(info.OsVersion, out var version))
{
return version.Major switch
{
0 => true,
2 => version.Minor < 0,
_ => false,
};
}

return true;
}

Expand Down Expand Up @@ -251,14 +232,7 @@ protected override async ValueTask ExecuteCommand()
|| Path.GetFileName(IndividualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.CoprocApplicationFile
|| Path.GetFileName(IndividualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.CoprocBootloaderFile)
{
if (connection == null)
{
connection = await GetConnectionAndDisableRuntime();
}
else
{
await connection.RuntimeDisable();
}
connection = await GetConnectionAndDisableRuntime();

await WriteEspFiles(connection, deviceInfo, package);
}
Expand All @@ -268,6 +242,8 @@ protected override async ValueTask ExecuteCommand()
{
await connection.Device.Reset();
}

Logger?.LogInformation("Firmware updated successfully");
}

private async Task<IMeadowConnection?> WriteRuntime(IMeadowConnection? connection, DeviceInfo? deviceInfo, FirmwarePackage package)
Expand Down Expand Up @@ -295,6 +271,7 @@ protected override async ValueTask ExecuteCommand()

if (UseDfu || RequiresDfuForRuntimeUpdates(deviceInfo))
{
var initialPorts = await MeadowConnectionManager.GetSerialPorts();

write_runtime:
if (!await connection.Device!.WriteRuntime(runtimePath, CancellationToken))
Expand All @@ -303,6 +280,23 @@ protected override async ValueTask ExecuteCommand()
Logger?.LogInformation($"Error writing runtime - retrying");
goto write_runtime;
}

connection = await GetCurrentConnection(true);

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

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

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

// configure the route to that port for the user
Settings.SaveSetting(SettingsManager.PublicSettings.Route, newPort);
}
}
else
{
Expand Down Expand Up @@ -463,11 +457,22 @@ await DfuUtils.FlashFile(
return false;
}

// now wait for a new serial port to appear
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;
}

async Task<string> WaitForNewSerialPort(IList<string>? ignorePorts)
{
var ports = await MeadowConnectionManager.GetSerialPorts();
var retryCount = 0;

var newPort = ports.Except(initialPorts).FirstOrDefault();
var newPort = ports.Except(ignorePorts).FirstOrDefault();

while (newPort == null)
{
Expand All @@ -477,14 +482,9 @@ await DfuUtils.FlashFile(
}
await Task.Delay(500);
ports = await MeadowConnectionManager.GetSerialPorts();
newPort = ports.Except(initialPorts).FirstOrDefault();
newPort = ports.Except(ignorePorts).FirstOrDefault();
}

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

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

return true;
return newPort;
}
}
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.10.0</PackageVersion>
<PackageVersion>2.0.11.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
13 changes: 11 additions & 2 deletions Source/v2/Meadow.Cli/MeadowConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public MeadowConnectionManager(ISettingsManager settingsManager)
_settingsManager = settingsManager;
}

public IMeadowConnection? GetCurrentConnection()
public IMeadowConnection? GetCurrentConnection(bool forceReconnect = false)
{
var route = _settingsManager.GetSetting(SettingsManager.PublicSettings.Route);

Expand All @@ -30,7 +30,12 @@ public MeadowConnectionManager(ISettingsManager settingsManager)
}

// TODO: support connection changing (CLI does this rarely as it creates a new connection with each command)
if (_currentConnection != null) return _currentConnection;
if (_currentConnection != null && forceReconnect == false)
{
return _currentConnection;
}
_currentConnection?.Detach();
_currentConnection?.Dispose();

// try to determine what the route is
string? uri = null;
Expand Down Expand Up @@ -109,7 +114,9 @@ public static async Task<IList<string>> GetSerialPorts()
public static async Task<IList<string>> GetMeadowSerialPortsForOsx()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) == false)
{
throw new PlatformNotSupportedException("This method is only supported on macOS");
}

return await Task.Run(() =>
{
Expand Down Expand Up @@ -203,7 +210,9 @@ public static async Task<IList<string>> GetMeadowSerialPortsForLinux()
public static IList<string> GetMeadowSerialPortsForWindows()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == false)
{
throw new PlatformNotSupportedException("This method is only supported on Windows");
}

try
{
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.10.0";
public const string CLI_VERSION = "2.0.11.0";
}
}
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Hcom/IMeadowConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Meadow.Hcom
{
public interface IMeadowConnection
public interface IMeadowConnection : IDisposable
{
event EventHandler<(string message, string? source)> DeviceMessageReceived;
event EventHandler<Exception> ConnectionError;
Expand Down
Loading