8000 working on hardening by ctacke · Pull Request #371 · WildernessLabs/Meadow.CLI · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

working on hardening #371

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

Merged
merged 2 commits into from
Oct 5, 2023
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
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