Skip to content

Commit

Permalink
Working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellerbach committed Mar 11, 2024
1 parent 6763ab2 commit fc273ee
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 97 deletions.
1 change: 1 addition & 0 deletions USB Test App WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Button Content="Deploy File" HorizontalAlignment="Left" Margin="162,275,0,0" VerticalAlignment="Top" Width="75" Click="DeployFileTestButton_Click" />
<Button Content="Reboot CLR" HorizontalAlignment="Left" Margin="364,77,0,0" VerticalAlignment="Top" Width="121" Click="RebootDeviceButton_Click"/>
<Button Content="Upload File I:\" HorizontalAlignment="Left" Margin="39,306,0,0" VerticalAlignment="Top" Width="110" Click="UploadFileInternalStorage_Click"/>
<Button Content="Rm file I:\" HorizontalAlignment="Left" Margin="162,306,0,0" VerticalAlignment="Top" Width="75" Click="RemoveFileInternalStorage_Click"/>


</Grid>
Expand Down
24 changes: 21 additions & 3 deletions USB Test App WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,15 +1133,20 @@ private void DeployFileTestButton_Click(object sender, RoutedEventArgs e)

private void UploadFileInternalStorage_Click(object sender, RoutedEventArgs e)
{
string fileContent = "This is a test file to upload in internal storage. A long message to test more than just a line.\r\n" +
string fileContent = "1. This is a test file to upload in internal storage. A long message to test more than just a line.\r\n" +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " +
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\r\n" +
"2. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\r\n" +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " +
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
"3. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\r\n" +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " +
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
"4. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\r\n";
//string fileContent = "simple test file";
string fileName = "I:\\upload.txt";

// disable button
Expand All @@ -1153,5 +1158,18 @@ private void UploadFileInternalStorage_Click(object sender, RoutedEventArgs e)
// enable button
(sender as Button).IsEnabled = true;
}

private void RemoveFileInternalStorage_Click(object sender, RoutedEventArgs e)
{
string fileName = "I:\\upload.txt";
// disable button
(sender as Button).IsEnabled = false;

var reply1 = (DataContext as MainViewModel).AvailableDevices[DeviceGrid.SelectedIndex].DebugEngine.RemoveFile(fileName);
Debug.WriteLine($"File upload internal success: {reply1}");

// enable button
(sender as Button).IsEnabled = true;
}
}
}
109 changes: 22 additions & 87 deletions nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,10 @@ public override bool PrepareForSend(byte[] data, int length, int offset = 0)
/// </summary>
public class Monitor_StorageOperation : OverheadBase
{
public StorageOperation Operation = StorageOperation.None;
public uint Length = 0;
public string StorageName = null;
public byte[] Data = null;
public uint Operation = (byte)StorageOperation.None;
public uint NameLength = 0;
public uint DataLength = 0;
public byte[] Data = new byte[0];

public class Reply
{
Expand All @@ -571,8 +571,9 @@ public void PrepareForSend(
StorageOperation operation,
string name)
{
Operation = operation;
StorageName = name;
Operation = (byte)operation;
Data = Encoding.UTF8.GetBytes(name);
NameLength = (uint)Data.Length;
}

public void PrepareForSend(
Expand All @@ -582,8 +583,9 @@ public void PrepareForSend(
int offset,
int length)
{
Operation = operation;
StorageName = name;
Operation = (byte)operation;
Data = Encoding.UTF8.GetBytes(name);
NameLength = (uint)(Data.Length + data.Length);

PrepareForSend(data, offset, length);
}
Expand All @@ -593,10 +595,16 @@ public override bool PrepareForSend(
int offset,
int length)
{
Length = (uint)length;
Data = new byte[length];
DataLength = (uint)length;
if(Data.Length < DataLength + NameLength)
{
byte[] bytes = new byte[Data.Length];
Array.Copy(Data, 0, bytes, 0, Data.Length);
Data = new byte[DataLength + NameLength];
Array.Copy(bytes, 0, Data, 0, bytes.Length);
}

Array.Copy(data, offset, Data, 0, length);
Array.Copy(data, offset, Data, NameLength, length);

return true;
}
Expand Down Expand Up @@ -629,88 +637,15 @@ public enum StorageOperation : byte
/// <remarks>
/// If the file doesn't exist, no action is taken.
/// </remarks>
Delete = 2
}
}

/// <summary>
/// Perform storage operation on the target device.
/// </summary>
public class Monitor_StorageOperation : OverheadBase
{
public StorageOperation Operation = StorageOperation.None;
public string StorageName = string.Empty;
public uint Length = 0;
public byte[] Data = null;

public class Reply
{
public uint ErrorCode;
};
Delete = 2,

public void PrepareForSend(
StorageOperation operation,
string name)
{
Operation = operation;
StorageName = name;
}

public void PrepareForSend(
StorageOperation operation,
string name,
byte[] data,
int offset,
int length)
{
Operation = operation;
StorageName = name;

PrepareForSend(data, offset, length);
}

public override bool PrepareForSend(
byte[] data,
int offset,
int length)
{
Length = (uint)length;
Data = new byte[length];

Array.Copy(data, offset, Data, 0, length);

return true;
}

//////////////////////////////////////////////////////////////////////////////////////
// !!! KEEP IN SYNC WITH typedef enum Monitor_StorageOperation (in native code) !!! //
//////////////////////////////////////////////////////////////////////////////////////

/// <summary>
/// Storage operation to be performed.
/// </summary>
public enum StorageOperation : byte
{
/// <summary>
/// Not specified.
/// </summary>
None = 0,

/// <summary>
/// Write to storage.
/// </summary>
/// <remarks>
/// If the file already exists, it will be overwritten.
/// </remarks>
Write = 1,

/// <summary>
/// Delete from storage.
/// Append to a file.
/// </summary>
/// <remarks>
/// If the file doesn't exist, no action is taken.
/// </remarks>
Delete = 2
Append = 3
}
}

Expand Down
37 changes: 30 additions & 7 deletions nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4426,15 +4426,38 @@ public int GetPacketMaxLength(Commands.OverheadBase cmd)
public StorageOperationErrorCodes AddFile(string fileName, byte[] fileContent)
{
var storop = new Commands.Monitor_StorageOperation();
storop.PrepareForSend(Commands.Monitor_StorageOperation.StorageOperation.Write, fileName, fileContent, 0, fileContent.Length);
IncomingMessage reply = PerformSyncRequest(Commands.c_Monitor_StorageOperation, 0, storop);
if (reply != null)
// Send by chunk, the first chunk is a write, the others will be appened
storop.PrepareForSend(Commands.Monitor_StorageOperation.StorageOperation.Write, fileName);

// We do have 3 * 4 = 12 overhead as well plus byte array type
var datasize = GetPacketMaxLength(storop) - 13;
int idx = 0;
int length;

IncomingMessage reply;

do
{
Commands.Monitor_StorageOperation.Reply cmdReply = reply.Payload as Commands.Monitor_StorageOperation.Reply;
return (StorageOperationErrorCodes)cmdReply.ErrorCode;
}
length = (idx + datasize < fileContent.Length) ? datasize : fileContent.Length - idx;
storop.PrepareForSend(fileContent, idx, length);
idx += length;
reply = PerformSyncRequest(Commands.c_Monitor_StorageOperation, 0, storop);
storop.Operation = (uint)Commands.Monitor_StorageOperation.StorageOperation.Append;
if (reply != null)
{
Commands.Monitor_StorageOperation.Reply cmdReply = reply.Payload as Commands.Monitor_StorageOperation.Reply;
if ((StorageOperationErrorCodes)cmdReply.ErrorCode != StorageOperationErrorCodes.NoError)
{
return StorageOperationErrorCodes.WriteError;
}
}
else
{
return StorageOperationErrorCodes.WriteError;
}
} while (idx < fileContent.Length);

return StorageOperationErrorCodes.WriteError;
return StorageOperationErrorCodes.NoError;
}

public StorageOperationErrorCodes RemoveFile(string fileName)
Expand Down

0 comments on commit fc273ee

Please sign in to comment.