Skip to content

Commit

Permalink
Merge pull request #369 from WildernessLabs/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
jorgedevs authored Oct 4, 2023
2 parents a803799 + 85d4e59 commit 65c611d
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ protected override async ValueTask ExecuteCommand()
Logger?.LogInformation($"Logging out of Meadow.Cloud...");

IdentityManager.Logout();

Logger?.LogInformation($"Done.");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.Hcom;
using Meadow.Software;
using Microsoft.Extensions.Logging;

Expand All @@ -15,8 +13,8 @@ public FirmwareDefaultCommand(FileManager fileManager, ISettingsManager settings
{
}

[CommandParameter(0, Name = "Version number to use as default", IsRequired = true)]
public string Version { get; set; } = default!;
[CommandParameter(0, Name = "Version number to use as default", IsRequired = false)]
public string? Version { get; set; } = null;

protected override async ValueTask ExecuteCommand()
{
Expand All @@ -26,12 +24,17 @@ protected override async ValueTask ExecuteCommand()
// TODO: add switch and support for other platforms
var collection = FileManager.Firmware["Meadow F7"];

var existing = collection.FirstOrDefault(p => p.Version == Version);
if (Version == null)
{
Logger?.LogInformation($"Default firmware is '{collection.DefaultPackage.Version}'.");
}
else
{
var existing = collection.FirstOrDefault(p => p.Version == Version);

Logger?.LogInformation($"Setting default firmware to '{Version}'...");
Logger?.LogInformation($"Setting default firmware to '{Version}'...");

await collection.SetDefaultPackage(Version);

Logger?.LogInformation($"Done.");
await collection.SetDefaultPackage(Version);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.Hcom;
using Meadow.Software;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -29,7 +27,5 @@ protected override async ValueTask ExecuteCommand()
Logger?.LogInformation($"Deleting firmware '{Version}'...");

await collection.DeletePackage(Version);

Logger?.LogInformation($"Done.");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.Hcom;
using Meadow.Software;
using Microsoft.Extensions.Logging;

Expand All @@ -18,7 +16,7 @@ public FirmwareDownloadCommand(FileManager fileManager, ISettingsManager setting
[CommandOption("force", 'f', IsRequired = false)]
public bool Force { get; set; }

[CommandParameter(0, Name = "Version number to download", IsRequired = false)]
[CommandOption("version", 'v', IsRequired = false)]
public string? Version { get; set; } = default!;

protected override async ValueTask ExecuteCommand()
Expand Down Expand Up @@ -79,8 +77,11 @@ protected override async ValueTask ExecuteCommand()
}
}

private long _lastProgress = 0;

private void OnDownloadProgress(object? sender, long e)
{
Logger?.LogInformation($"Retrieved {e} bytes... \r");
// use Console so we can Write instead of Logger which only supports WriteLine
Console?.Output.Write($"Retrieved {e} bytes... \r");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class FirmwareWriteCommand : BaseDeviceCommand<FirmwareWriteCommand>
private ISettingsManager Settings { get; }

private ILibUsbDevice? _libUsbDevice;
private bool _fileWriteError = false;

public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileManager, MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
Expand Down Expand Up @@ -70,6 +71,7 @@ protected override async ValueTask ExecuteCommand()
UseDfu = true;
}

IMeadowConnection connection;

if (UseDfu && Files.Contains(FirmwareType.OS))
{
Expand Down Expand Up @@ -130,7 +132,7 @@ protected override async ValueTask ExecuteCommand()
// configure the route to that port for the user
Settings.SaveSetting(SettingsManager.PublicSettings.Route, newPort);

var connection = await GetCurrentConnection();
connection = await GetCurrentConnection();

if (connection == null)
{
Expand All @@ -148,24 +150,26 @@ protected override async ValueTask ExecuteCommand()

await WriteFiles(connection);
}

var deviceInfo = await connection.Device.GetDeviceInfo(CancellationToken);

if (deviceInfo != null)
{
Logger?.LogInformation($"Done.");
Logger?.LogInformation(deviceInfo.ToString());
}
}
else
{
var connection = await GetCurrentConnection();
connection = await GetCurrentConnection();
if (connection == null)
{
return;
}
await WriteFiles(connection);
}

await connection.ResetDevice(CancellationToken);
await connection.WaitForMeadowAttach();

var deviceInfo = await connection.Device.GetDeviceInfo(CancellationToken);

if (deviceInfo != null)
{
Logger?.LogInformation(deviceInfo.ToString());
}
}

private ILibUsbDevice GetLibUsbDeviceForCurrentEnvironment()
Expand Down Expand Up @@ -250,6 +254,10 @@ private async ValueTask WriteFiles(IMeadowConnection connection)
{
Logger?.LogInformation(message);
};
connection.FileWriteFailed += (s, e) =>
{
_fileWriteError = true;
};

var package = await GetSelectedPackage();

Expand Down Expand Up @@ -287,9 +295,14 @@ private async ValueTask WriteFiles(IMeadowConnection connection)
// get the path to the runtime file
var rtpath = package.GetFullyQualifiedPath(package.Runtime);

// TODO: for serial, we must wait for the flash to complete

write_runtime:
await connection.Device.WriteRuntime(rtpath, CancellationToken);
if (_fileWriteError)
{
_fileWriteError = false;
Logger?.LogInformation($"Error writing runtime. Retrying.");
goto write_runtime;
}
}

if (CancellationToken.IsCancellationRequested)
Expand Down
10 changes: 7 additions & 3 deletions Source/v2/Meadow.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@
},
"Firmware Download version": {
"commandName": "Project",
"commandLineArgs": "firmware download 1.0.2.0 --force"
"commandLineArgs": "firmware download -v 1.0.2.0 --force"
},
"Firmware Default": {
"Firmware Default get": {
"commandName": "Project",
"commandLineArgs": "firmware default 1.2.0.1"
"commandLineArgs": "firmware default"
},
"Firmware Default set": {
"commandName": "Project",
"commandLineArgs": "firmware default 1.3.4.0"
},
"Firmware Delete": {
"commandName": "Project",
Expand Down
8 changes: 6 additions & 2 deletions Source/v2/Meadow.Hcom/Connections/ConnectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public abstract class ConnectionBase : IMeadowConnection, IDisposable
public event EventHandler<Exception> ConnectionError = default!;
public event EventHandler<(string fileName, long completed, long total)> FileWriteProgress = default!;
public event EventHandler<string> ConnectionMessage = default!;
public event EventHandler FileWriteFailed;

public abstract string Name { get; }

Expand Down Expand Up @@ -57,6 +58,11 @@ protected void RaiseFileWriteProgress(string fileName, long progress, long total
FileWriteProgress?.Invoke(this, (fileName, progress, total));
}

protected void RaiseFileWriteFailed()
{
FileWriteFailed?.Invoke(this, EventArgs.Empty);
}

protected void RaiseDeviceMessageReceived(string message, string? source)
{
DeviceMessageReceived?.Invoke(this, (message, source));
Expand All @@ -73,8 +79,6 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Close();
// _port.Dispose();
}

_isDisposed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ private async Task ListenerProc()
{
FileDataReceived?.Invoke(this, fib.Text);
}
else if (response is FileDownloadFailedResponse fdf)
{
RaiseFileWriteFailed();
}
else
{
Debug.WriteLine($"{response?.GetType().Name} for: {response?.RequestType}");
Expand Down
8 changes: 7 additions & 1 deletion Source/v2/Meadow.Hcom/Connections/SerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,17 @@ public override async Task<bool> WriteRuntime(
{
InfoMessages.Clear();

_lastRequestConcluded = null;

var status = await WriteFile(localFileName, "Meadow.OS.Runtime.bin",
RequestType.HCOM_MDOW_REQUEST_MONO_UPDATE_RUNTIME,
RequestType.HCOM_MDOW_REQUEST_MONO_UPDATE_FILE_END,
0,
cancellationToken);

RaiseConnectionMessage("\nErasing runtime flash blocks...");

/*
RaiseConnectionMessage("\nErasing runtime flash blocks...");
status = await WaitForResult(() =>
{
if (_lastRequestConcluded != null)
Expand Down Expand Up @@ -865,6 +868,9 @@ public override async Task<bool> WriteRuntime(
return false;
},
cancellationToken);
*/

await WaitForConcluded(null, cancellationToken);

return status;
}
Expand Down
1 change: 1 addition & 0 deletions Source/v2/Meadow.Hcom/IMeadowConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface IMeadowConnection
event EventHandler<Exception> ConnectionError;
event EventHandler<string> ConnectionMessage;
event EventHandler<(string fileName, long completed, long total)> FileWriteProgress;
event EventHandler FileWriteFailed;

string Name { get; }
IMeadowDevice? Device { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text;

namespace Meadow.Hcom;

internal class FileDownloadFailedResponse : SerialResponse
{
public string Text => Encoding.UTF8.GetString(_data, RESPONSE_PAYLOAD_OFFSET, PayloadLength);

internal FileDownloadFailedResponse(byte[] data, int length)
: base(data, length)
{
}
}

2 changes: 2 additions & 0 deletions Source/v2/Meadow.Hcom/Serial Responses/SerialResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public static SerialResponse Parse(byte[] data, int length)
return new TextPayloadSerialResponse(data, length);
case ResponseType.HCOM_MDOW_REQUEST_OTA_REGISTER_DEVICE:
return new TextPayloadSerialResponse(data, length);
case ResponseType.HCOM_HOST_REQUEST_DNLD_FAIL_RESEND:
return new FileDownloadFailedResponse(data, length);
default:
return new SerialResponse(data, length);
}
Expand Down

0 comments on commit 65c611d

Please sign in to comment.