Skip to content

Commit

Permalink
Merge pull request #371 from WildernessLabs/v2
Browse files Browse the repository at this point in the history
working on hardening
  • Loading branch information
jorgedevs authored Oct 5, 2023
2 parents e501bb4 + 44c5641 commit de1784e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
8 changes: 7 additions & 1 deletion Source/v2/Meadow.Cli/AppManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ public static async Task DeployApplication(
}
}

await connection?.WriteFile(localFile.Key, null, cancellationToken);
send_file:

if (!await connection?.WriteFile(localFile.Key, null, cancellationToken))
{
logger.LogWarning($"Error sending'{Path.GetFileName(localFile.Key)}'. Retrying.");
goto send_file;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ protected override async ValueTask ExecuteCommand()

if (connection == null)
{
Logger?.LogError($"No connection path is defined");
return;
}

if (connection != null)
{
string path = Path == null
? AppDomain.CurrentDomain.BaseDirectory
? Environment.CurrentDirectory
: Path;

// is the path a file?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,8 @@ private async ValueTask WriteFiles(IMeadowConnection connection)
var rtpath = package.GetFullyQualifiedPath(package.Runtime);

write_runtime:
await connection.Device.WriteRuntime(rtpath, CancellationToken);
if (_fileWriteError)
if (!await connection.Device.WriteRuntime(rtpath, CancellationToken))
{
_fileWriteError = false;
Logger?.LogInformation($"Error writing runtime. Retrying.");
goto write_runtime;
}
Expand Down
28 changes: 15 additions & 13 deletions Source/v2/Meadow.Cli/Commands/Current/ListenCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using Meadow.Hcom;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand All @@ -13,17 +12,10 @@ public class ListenCommand : BaseDeviceCommand<ListenCommand>
public ListenCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
{
var connection = connectionManager.GetCurrentConnection();

if (connection == null)
{
Logger?.LogError($"No device connection configured.");
return;
}

Logger?.LogInformation($"Listening for Meadow Console output on '{connection.Name}'. Press Ctrl+C to exit...");
}

connection.DeviceMessageReceived += OnDeviceMessageReceived;
private void Connection_ConnectionMessage(object? sender, string e)
{
}

private void OnDeviceMessageReceived(object? sender, (string message, string? source) e)
Expand All @@ -40,11 +32,21 @@ private void OnDeviceMessageReceived(object? sender, (string message, string? so

protected override async ValueTask ExecuteCommand()
{
var connection = await GetCurrentConnection();

if (connection == null)
{
return;
}

connection.DeviceMessageReceived += OnDeviceMessageReceived;
connection.ConnectionMessage += Connection_ConnectionMessage;

Logger?.LogInformation($"Listening for Meadow Console output on '{connection.Name}'. Press Ctrl+C to exit...");

while (!CancellationToken.IsCancellationRequested)
{
await Task.Delay(1000);
}

Logger?.LogInformation($"Cancelled.");
}
}
47 changes: 34 additions & 13 deletions Source/v2/Meadow.Hcom/Connections/SerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,10 @@ public override async Task<bool> WriteRuntime(
cancellationToken);
*/

await WaitForConcluded(null, cancellationToken);
if (status)
{
await WaitForConcluded(null, cancellationToken);
}

return status;
}
Expand Down Expand Up @@ -899,11 +902,14 @@ public override async Task<bool> WriteCoprocessorFile(
RaiseConnectionMessage($"Transferring {Path.GetFileName(localFileName)} to coprocessor...");

// push the file to the device
await WriteFile(localFileName, null,
if (!await WriteFile(localFileName, null,
RequestType.HCOM_MDOW_REQUEST_START_ESP_FILE_TRANSFER,
RequestType.HCOM_MDOW_REQUEST_END_ESP_FILE_TRANSFER,
destinationAddress,
cancellationToken);
cancellationToken))
{
return false;
}


_lastRequestConcluded = null;
Expand Down Expand Up @@ -954,6 +960,7 @@ private async Task<bool> WriteFile(

var accepted = false;
Exception? ex = null;
var needsRetry = false;

void OnFileWriteAccepted(object? sender, EventArgs a)
{
Expand All @@ -963,9 +970,16 @@ void OnFileError(object? sender, Exception exception)
{
ex = exception;
}
void OnFileRetry(object? sender, EventArgs e)
{
needsRetry = true;
}

FileWriteAccepted += OnFileWriteAccepted;
FileException += OnFileError;
FileWriteFailed += OnFileRetry;

Debug.WriteLine($"Sending '{localFileName}'");

EnqueueRequest(command);

Expand All @@ -984,7 +998,6 @@ void OnFileError(object? sender, Exception exception)
// now send the file data
// The maximum data bytes is max packet size - 2 bytes for the sequence number
byte[] packet = new byte[Protocol.HCOM_PROTOCOL_PACKET_MAX_SIZE - 2];
int bytesRead;
ushort sequenceNumber = 0;

var progress = 0;
Expand All @@ -997,7 +1010,7 @@ void OnFileError(object? sender, Exception exception)
var oldTimeout = _port.ReadTimeout;
_port.ReadTimeout = 60000;

while (true)
while (true && !needsRetry)
{
if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested)
{
Expand All @@ -1014,23 +1027,31 @@ void OnFileError(object? sender, Exception exception)
toRead = packet.Length - 2;
}
Array.Copy(fileBytes, progress, packet, 2, toRead);
_port.WriteTimeout = 1000;
await EncodeAndSendPacket(packet, toRead + 2, cancellationToken);
progress += toRead;
base.RaiseFileWriteProgress(fileName, progress, expected);
if (progress >= fileBytes.Length) break;
}
_port.ReadTimeout = oldTimeout;

base.RaiseFileWriteProgress(fileName, expected, expected);
if (!needsRetry)
{
_port.ReadTimeout = oldTimeout;

// finish with an "end" message - not enqued because this is all a serial operation
var request = RequestBuilder.Build<EndFileWriteRequest>();
request.SetRequestType(endRequestType);
var p = request.Serialize();
await EncodeAndSendPacket(p, cancellationToken);
base.RaiseFileWriteProgress(fileName, expected, expected);

return true;
// finish with an "end" message - not enqued because this is all a serial operation
var request = RequestBuilder.Build<EndFileWriteRequest>();
request.SetRequestType(endRequestType);
var p = request.Serialize();
await EncodeAndSendPacket(p, cancellationToken);
}

FileWriteAccepted += OnFileWriteAccepted;
FileException += OnFileError;
FileWriteFailed += OnFileRetry;

return !needsRetry;
}

public override async Task<bool> ReadFile(string meadowFileName, string? localFileName = null, CancellationToken? cancellationToken = null)
Expand Down

0 comments on commit de1784e

Please sign in to comment.