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

Show firmware folder when downloading + code cleanup #517

Merged
merged 4 commits into from
Mar 7, 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
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.IO.Ports;
using System.IO;
using CliFx.Attributes;
using Microsoft.Extensions.Logging;
using CliFx.Attributes;
using Meadow.Hcom;
using Microsoft.Extensions.Logging;
using System.IO.Ports;

namespace Meadow.CLI.Commands.DeviceManagement;

Expand All @@ -20,115 +18,115 @@ public UartProfilerEnableCommand(MeadowConnectionManager connectionManager, ILog
: base(connectionManager, loggerFactory)
{ }

private void StartNewFile(string outputPath, ref FileStream outputFile, byte[] header, ref int headerIndex, ref int totalBytesWritten, ref int headerFileCount)
{
if (outputFile != null)
private void StartNewFile(string outputPath, ref FileStream outputFile, byte[] header, ref int headerIndex, ref int totalBytesWritten, ref int headerFileCount)
{
outputFile.Close();
outputFile.Dispose();
}
outputFile = new FileStream(outputPath, FileMode.Create);
totalBytesWritten = 0;
headerIndex = 0;
headerFileCount++;
foreach (var headerByte in header)
{
outputFile.WriteByte(headerByte);
totalBytesWritten++;
if (outputFile != null)
{
outputFile.Close();
outputFile.Dispose();
}
outputFile = new FileStream(outputPath, FileMode.Create);
totalBytesWritten = 0;
headerIndex = 0;
headerFileCount++;
foreach (var headerByte in header)
{
outputFile.WriteByte(headerByte);
totalBytesWritten++;
}
}
}

private void ReadAndSaveSerialData()
{
// Define the equivalent header bytes sequence to the 32-bit representation of 0x4D505A01
// according to the Mono Profiler LOG_VERSION_MAJOR 3 LOG_VERSION_MINOR 0 LOG_DATA_VERSION 17
var header = new byte[] { 0x01, 0x5A, 0x50, 0x4D };
var headerIndex = 0;
var totalBytesWritten = 0;
var headerFileCount = 0;

var defaultDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
outputDirectory ??= defaultDirectory;
var outputPath = Path.Combine(outputDirectory, "output.mlpd");
private void ReadAndSaveSerialData()
{
// Define the equivalent header bytes sequence to the 32-bit representation of 0x4D505A01
// according to the Mono Profiler LOG_VERSION_MAJOR 3 LOG_VERSION_MINOR 0 LOG_DATA_VERSION 17
var header = new byte[] { 0x01, 0x5A, 0x50, 0x4D };
var headerIndex = 0;
var totalBytesWritten = 0;
var headerFileCount = 0;

SerialPort port = new SerialPort(SerialInterface, SerialConnection.DefaultBaudRate);
FileStream outputFile = null;
var defaultDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
outputDirectory ??= defaultDirectory;
var outputPath = Path.Combine(outputDirectory, "output.mlpd");

try
{
port.Open();
Logger?.LogInformation("Serial connection opened successfully.");
SerialPort port = new SerialPort(SerialInterface, SerialConnection.DefaultBaudRate);
FileStream outputFile = null;

while (true)
try
{
int data = port.ReadByte();
if (data != -1)
port.Open();
Logger?.LogInformation("Serial connection opened successfully.");

while (true)
{
if (headerFileCount == 0)
int data = port.ReadByte();
if (data != -1)
{
// Check if the received data matches the header sequence
if (data == header[headerIndex])
if (headerFileCount == 0)
{
headerIndex++;
// If the entire header sequence is found, start writing to a file
if (headerIndex == header.Length)
// Check if the received data matches the header sequence
if (data == header[headerIndex])
{
headerIndex++;
// If the entire header sequence is found, start writing to a file
if (headerIndex == header.Length)
{
Logger?.LogInformation($"Profiling data header found! Writing to {outputPath}...");
StartNewFile(outputPath, ref outputFile, header, ref headerIndex, ref totalBytesWritten, ref headerFileCount);
}
}
else
{
Logger?.LogInformation($"Profiling data header found! Writing to {outputPath}...");
StartNewFile(outputPath, ref outputFile, header, ref headerIndex, ref totalBytesWritten, ref headerFileCount);
// Reset header index if received byte does not match
headerIndex = 0;
}
}
else
{
// Reset header index if received byte does not match
headerIndex = 0;
}
}
else
{
// Writing to file after a header is found
outputFile.WriteByte((byte)data);
totalBytesWritten++;
// Writing to file after a header is found
outputFile.WriteByte((byte)data);
totalBytesWritten++;

// Check for a new header while writing to a file
if (data == header[headerIndex])
{
headerIndex++;
if (headerIndex == header.Length)
// Check for a new header while writing to a file
if (data == header[headerIndex])
{
// Close the current file, start writing to a new file, and reset counters
// to avoid corrupted profiling data (e.g. device reset while profiling)
var newOutputPath = outputDirectory + "output_" + headerFileCount + ".mlpd";
Logger?.LogInformation($"New profiling data header found! Writing to {newOutputPath}...");
StartNewFile(newOutputPath, ref outputFile, header, ref headerIndex, ref totalBytesWritten, ref headerFileCount);
headerIndex++;
if (headerIndex == header.Length)
{
// Close the current file, start writing to a new file, and reset counters
// to avoid corrupted profiling data (e.g. device reset while profiling)
var newOutputPath = outputDirectory + "output_" + headerFileCount + ".mlpd";
Logger?.LogInformation($"New profiling data header found! Writing to {newOutputPath}...");
StartNewFile(newOutputPath, ref outputFile, header, ref headerIndex, ref totalBytesWritten, ref headerFileCount);
}
}
else
{
// Reset header index if received byte does not match
headerIndex = 0;
}
}
else
{
// Reset header index if received byte does not match
headerIndex = 0;
}

// Log bytes written periodically to not spam the console
if (totalBytesWritten % (4 * 1024) == 0)
{
Logger?.LogInformation($"{totalBytesWritten} bytes written...");
// Log bytes written periodically to not spam the console
if (totalBytesWritten % (4 * 1024) == 0)
{
Logger?.LogInformation($"{totalBytesWritten} bytes written...");
}
}
}
}
}
catch (IOException ex)
{
Logger?.LogError("Failed to open serial port: " + ex.Message);
}
finally
{
outputFile?.Close();
outputFile?.Dispose();
if (port.IsOpen)
port.Close();
}
}
catch (IOException ex)
{
Logger?.LogError("Failed to open serial port: " + ex.Message);
}
finally
{
outputFile?.Close();
outputFile?.Dispose();
if (port.IsOpen)
port.Close();
}
}

protected override async ValueTask ExecuteCommand()
{
Expand Down
4 changes: 1 addition & 3 deletions Source/v2/Meadow.Cli/Commands/Current/BaseCloudCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Meadow.Cloud.Client.Users;
using Microsoft.Extensions.Logging;
using System.Linq;

namespace Meadow.CLI.Commands.DeviceManagement;

Expand All @@ -17,7 +15,7 @@ public abstract class BaseCloudCommand<T> : BaseCommand<T>
protected bool RequiresAuthentication { get; set; } = true;

protected IMeadowCloudClient MeadowCloudClient { get; }

public BaseCloudCommand(
IMeadowCloudClient meadowCloudClient,
ILoggerFactory loggerFactory)
Expand Down
6 changes: 2 additions & 4 deletions Source/v2/Meadow.Cli/Commands/Current/BaseSettingsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using CliFx.Infrastructure;
using Meadow.CLI;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

public abstract class BaseSettingsCommand<T> : BaseCommand<T>
{
protected ISettingsManager SettingsManager { get; }

public BaseSettingsCommand(ISettingsManager settingsManager, ILoggerFactory loggerFactory) : base (loggerFactory)
public BaseSettingsCommand(ISettingsManager settingsManager, ILoggerFactory loggerFactory) : base(loggerFactory)
{
SettingsManager = settingsManager;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;
using System.Text.Json;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Meadow.CLI;
public class ConsoleTable
{
private readonly string[] _columns;
private IList<string[]> _rows;
private readonly IList<string[]> _rows;

public ConsoleTable(params string[] columns)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Meadow.Package;
using Meadow.Software;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Meadow.Software;
using Microsoft.Extensions.Logging;

Expand All @@ -9,7 +8,7 @@ namespace Meadow.CLI.Commands.DeviceManagement;
[Command("firmware download", Description = "Download a firmware package")]
public class FirmwareDownloadCommand : BaseCloudCommand<FirmwareDownloadCommand>
{
private readonly FileManager _fileManager;
private readonly FileManager _fileManager;

public FirmwareDownloadCommand(
FileManager fileManager,
Expand Down Expand Up @@ -89,7 +88,7 @@ protected override async ValueTask ExecuteCloudCommand()
}
else
{
Logger.LogInformation($"Firmware package '{Version}' downloaded");
Logger.LogInformation($"Firmware package '{Version}' downloaded to {collection.PackageFileRoot}");

if (explicitVersion == false)
{
Expand Down
1 change: 0 additions & 1 deletion Source/v2/Meadow.Cli/Commands/Current/LoginCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

Expand Down
1 change: 0 additions & 1 deletion Source/v2/Meadow.Cli/Commands/Current/LogoutCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;

Expand Down
1 change: 0 additions & 1 deletion Source/v2/Meadow.Cli/Commands/Legacy/DownloadOsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Meadow.Software;
using Microsoft.Extensions.Logging;

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.20.0</PackageVersion>
<PackageVersion>2.0.21.0</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Meadow.CLI
{
public static class Constants
{
public const string CLI_VERSION = "2.0.20.0";
public const string CLI_VERSION = "2.0.21.0";
}
}
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
},
"Firmware Download version": {
"commandName": "Project",
"commandLineArgs": "firmware download -v 1.7.1.4 --force"
"commandLineArgs": "firmware download -v 1.9.1.8 --force"
},
"Firmware Default get": {
"commandName": "Project",
Expand Down
Loading
Loading