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

bug fix for reading large files. bug fix for no local file name #545

Merged
merged 3 commits into from
Mar 27, 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
21 changes: 21 additions & 0 deletions Source/v2/Meadow.Cli/Commands/Current/File/FileReadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,29 @@ public FileReadCommand(MeadowConnectionManager connectionManager, ILoggerFactory
protected override async ValueTask ExecuteCommand()
{
var device = await GetCurrentDevice();
var connection = await GetCurrentConnection();

var received = 0;

connection.FileBytesReceived += (s, count) =>
{
received += count;
Logger?.LogInformation($"Received {received} bytes");
};

connection.FileReadCompleted += (s, f) =>
{
Logger?.LogInformation($"File written to '{f}'");
};

Logger?.LogInformation($"Getting file '{MeadowFile}' from device...");
var runtimeIsEnabled = await device.IsRuntimeEnabled();

if (runtimeIsEnabled)
{
Logger?.LogInformation($"Disabling runtime...");
await device.RuntimeDisable();
}

var success = await device.ReadFile(MeadowFile, LocalFile, CancellationToken);

Expand Down
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.31.0</PackageVersion>
<PackageVersion>2.0.32.0</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
Expand Down
4 changes: 2 additions & 2 deletions Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Meadow.CLI;

public static class Constants
{
public const string CLI_VERSION = "2.0.31.0";
}
public const string CLI_VERSION = "2.0.32.0";
}
6 changes: 5 additions & 1 deletion Source/v2/Meadow.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"Config: Set Route Serial": {
"commandName": "Project",
"commandLineArgs": "config route COM4"
"commandLineArgs": "config route COM5"
},
"Config: Set Route TCP": {
"commandName": "Project",
Expand Down Expand Up @@ -95,6 +95,10 @@
"commandName": "Project",
"commandLineArgs": "file delete all"
},
"File Read Large": {
"commandName": "Project",
"commandLineArgs": "file read Meadow.F7.dll"
},
"File Read": {
"commandName": "Project",
"commandLineArgs": "file read test.txt \"f:\\temp\\test2.txt\""
Expand Down
12 changes: 12 additions & 0 deletions Source/v2/Meadow.Hcom/Connections/ConnectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public abstract class ConnectionBase : IMeadowConnection, IDisposable
public event EventHandler<string> ConnectionMessage = default!;
public event EventHandler FileWriteFailed = default!;
public event EventHandler<byte[]> DebuggerMessageReceived;
public event EventHandler<string>? FileReadCompleted = default!;
public event EventHandler<int>? FileBytesReceived;

public abstract string Name { get; }

Expand Down Expand Up @@ -62,6 +64,11 @@ protected void RaiseDebuggerMessage(byte[] data)
DebuggerMessageReceived?.Invoke(this, data);
}

protected void RaiseFileReadCompleted(string fileName)
{
FileReadCompleted?.Invoke(this, fileName);
}

protected void RaiseFileWriteProgress(string fileName, long progress, long total)
{
FileWriteProgress?.Invoke(this, (fileName, progress, total));
Expand All @@ -82,6 +89,11 @@ protected void RaiseConnectionError(Exception error)
ConnectionError?.Invoke(this, error);
}

protected void RaiseFileBytesReceived(int length)
{
FileBytesReceived?.Invoke(this, length);
}

protected virtual void Dispose(bool disposing)
{
if (!_isDisposed)
Expand Down
17 changes: 14 additions & 3 deletions Source/v2/Meadow.Hcom/Connections/SerialConnection.ListenerProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ private async Task ListenerProc()

if (index < 0)
{
Debug.WriteLine($"No delimiter");
if (messageBytes.Count > 0)
{
Debug.WriteLine($"We have {messageBytes.Count} bytes with no end delimiter");
}
break;
}
var packetBytes = messageBytes.Remove(index + 1);
Expand All @@ -132,6 +135,12 @@ private async Task ListenerProc()
// now parse this per the HCOM protocol definition
var response = SerialResponse.Parse(decodedBuffer, decodedSize);

if (response == null)
{
Debug.WriteLine($"Response parsing yielded null");
continue;
}

Debug.WriteLine($"{response.RequestType}");
_state = ConnectionState.MeadowAttached;

Expand Down Expand Up @@ -248,6 +257,8 @@ private async Task ListenerProc()
}

_readFileInfo.FileStream.Write(udp.FileData, 0, udp.FileData.Length);

RaiseFileBytesReceived(udp.FileData.Length);
}
else if (response is UploadCompletedResponse ucr)
{
Expand All @@ -262,7 +273,7 @@ private async Task ListenerProc()
_readFileInfo.FileStream.Dispose();
_readFileInfo = null;

FileReadCompleted?.Invoke(this, fn);
RaiseFileReadCompleted(fn ?? string.Empty);
}
else if (response is FileReadInitFailedResponse frf)
{
Expand All @@ -286,7 +297,7 @@ private async Task ListenerProc()
}
else if (response is TextPayloadSerialResponse fib)
{
FileDataReceived?.Invoke(this, fib.Text);
FileTextReceived?.Invoke(this, fib.Text);
}
else if (response is FileDownloadFailedResponse fdf)
{
Expand Down
58 changes: 47 additions & 11 deletions Source/v2/Meadow.Hcom/Connections/SerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public partial class SerialConnection : ConnectionBase, IDisposable
public const int ReadBufferSizeBytes = 0x2000;
private const int DefaultTimeout = 5000;

private event EventHandler<string> FileReadCompleted = default!;
private event EventHandler FileWriteAccepted = default!;
private event EventHandler<string> FileDataReceived = default!;
private event EventHandler<string> FileTextReceived = default!;
public event ConnectionStateChangedHandler ConnectionStateChanged = default!;

private readonly SerialPort _port;
Expand Down Expand Up @@ -279,7 +278,7 @@ public string? LocalFileName
{
if (_localFileName != null) return _localFileName;

return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(MeadowFileName));
return Path.Combine(Environment.CurrentDirectory, Path.GetFileName(MeadowFileName));
}
set => _localFileName = value;
}
Expand Down Expand Up @@ -504,6 +503,48 @@ protected override void Dispose(bool disposing)

public int CommandTimeoutSeconds { get; set; } = 30;

private async Task<bool> WaitForFileReadCompleted(CancellationToken? cancellationToken)
{
var timeout = CommandTimeoutSeconds * 2;

var completed = false;

void LocalFRCHandler(object s, string e)
{
completed = true;
}
void LocalFBRHandler(object s, int e)
{
timeout = CommandTimeoutSeconds * 2;
}

FileBytesReceived += LocalFBRHandler;
FileReadCompleted += LocalFRCHandler;

try
{
while (timeout-- > 0)
{
if (cancellationToken?.IsCancellationRequested ?? false) return false;
if (_lastException != null) return false;

if (timeout <= 0) throw new TimeoutException();

if (completed) return true;

await Task.Delay(500);
}
}
finally
{
// clean up local events
FileBytesReceived -= LocalFBRHandler;
FileReadCompleted -= LocalFRCHandler;
}

return true;
}

private async Task<bool> WaitForResult(Func<bool> checkAction, CancellationToken? cancellationToken)
{
var timeout = CommandTimeoutSeconds * 2;
Expand Down Expand Up @@ -1077,12 +1118,7 @@ void OnFileError(object? sender, Exception exception)

EnqueueRequest(command);

if (!await WaitForResult(
() =>
{
return completed | ex != null;
},
cancellationToken))
if (!await WaitForFileReadCompleted(cancellationToken))
{
return false;
}
Expand All @@ -1108,7 +1144,7 @@ void OnFileDataReceived(object? sender, string data)
contents = data;
}

FileDataReceived += OnFileDataReceived;
FileTextReceived += OnFileDataReceived;

_lastRequestConcluded = null;
EnqueueRequest(command);
Expand Down Expand Up @@ -1159,7 +1195,7 @@ void OnFileDataReceived(object? sender, string data)
contents = data;
}

FileDataReceived += OnFileDataReceived;
FileTextReceived += OnFileDataReceived;

var lastTimeout = CommandTimeoutSeconds;

Expand Down
2 changes: 2 additions & 0 deletions Source/v2/Meadow.Hcom/IMeadowConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface IMeadowConnection : IDisposable
event EventHandler<(string fileName, long completed, long total)> FileWriteProgress;
event EventHandler FileWriteFailed;
event EventHandler<byte[]> DebuggerMessageReceived;
event EventHandler<string>? FileReadCompleted;
event EventHandler<int>? FileBytesReceived;

string Name { get; }
IMeadowDevice? Device { get; }
Expand Down
7 changes: 6 additions & 1 deletion Source/v2/Meadow.Hcom/SerialResponses/SerialResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ internal class SerialResponse
public uint UserData => BitConverter.ToUInt32(_data, HCOM_PROTOCOL_REQUEST_HEADER_USER_DATA_OFFSET);
protected int PayloadLength => _data.Length - RESPONSE_PAYLOAD_OFFSET;

public static SerialResponse Parse(byte[] data, int length)
public static SerialResponse? Parse(byte[] data, int length)
{
if (length == 0)
{
return null;
adrianstevens marked this conversation as resolved.
Show resolved Hide resolved
}

var type = (ResponseType)BitConverter.ToUInt16(data, HCOM_PROTOCOL_REQUEST_HEADER_RQST_TYPE_OFFSET);

return type switch
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Tooling.Core/Meadow.Tooling.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>
Expand Down
Loading