From 07656e126f09bd49e039db427a434783ebce594c Mon Sep 17 00:00:00 2001 From: Dominique Louis Date: Thu, 6 Jul 2023 11:04:29 +0100 Subject: [PATCH] New attempt to fix DeveloperLevel. --- Meadow.CLI.Core/Devices/IMeadowDevice.cs | 5 +-- Meadow.CLI.Core/Devices/MeadowDeviceHelper.cs | 19 ++-------- .../Devices/MeadowLocalDevice.FileManager.cs | 2 +- Meadow.CLI.Core/Devices/MeadowLocalDevice.cs | 36 ++----------------- .../Internals/MeadowCommunication/Command.cs | 12 ++++--- .../MeadowCommunication/FileCommand.cs | 2 +- .../SimpleCommandBuilder.cs | 10 +++++- .../Trace/SetDeveloperValueCommand.cs | 21 ++++++----- Meadow.Hcom/HcomMeadowRequestType.cs | 9 +++-- 9 files changed, 38 insertions(+), 78 deletions(-) diff --git a/Meadow.CLI.Core/Devices/IMeadowDevice.cs b/Meadow.CLI.Core/Devices/IMeadowDevice.cs index a7aac8c7..ff72f5b3 100644 --- a/Meadow.CLI.Core/Devices/IMeadowDevice.cs +++ b/Meadow.CLI.Core/Devices/IMeadowDevice.cs @@ -39,10 +39,7 @@ public interface IMeadowDevice : IDisposable public Task NshDisable(CancellationToken cancellationToken = default); public Task TraceEnable(CancellationToken cancellationToken = default); public Task SetTraceLevel(uint traceLevel, CancellationToken cancellationToken = default); - public Task SetDeveloper1(uint userData, CancellationToken cancellationToken = default); - public Task SetDeveloper2(uint userData, CancellationToken cancellationToken = default); - public Task SetDeveloper3(uint userData, CancellationToken cancellationToken = default); - public Task SetDeveloper4(uint userData, CancellationToken cancellationToken = default); + public Task SetDeveloper(ushort level, uint userData, CancellationToken cancellationToken = default); public Task Uart1Apps(CancellationToken cancellationToken = default); public Task Uart1Trace(CancellationToken cancellationToken = default); public Task TraceDisable(CancellationToken cancellationToken = default); diff --git a/Meadow.CLI.Core/Devices/MeadowDeviceHelper.cs b/Meadow.CLI.Core/Devices/MeadowDeviceHelper.cs index 8cc6d7fa..434515da 100644 --- a/Meadow.CLI.Core/Devices/MeadowDeviceHelper.cs +++ b/Meadow.CLI.Core/Devices/MeadowDeviceHelper.cs @@ -213,24 +213,9 @@ public Task TraceDisable(CancellationToken cancellationToken = default) return _meadowDevice.TraceDisable(cancellationToken); } - public Task SetDeveloper1(uint userData, CancellationToken cancellationToken = default) + public Task SetDeveloper(ushort level, uint userData, CancellationToken cancellationToken = default) { - return _meadowDevice.SetDeveloper1(userData, cancellationToken); - } - - public Task SetDeveloper2(uint userData, CancellationToken cancellationToken = default) - { - return _meadowDevice.SetDeveloper2(userData, cancellationToken); - } - - public Task SetDeveloper3(uint userData, CancellationToken cancellationToken = default) - { - return _meadowDevice.SetDeveloper3(userData, cancellationToken); - } - - public Task SetDeveloper4(uint userData, CancellationToken cancellationToken = default) - { - return _meadowDevice.SetDeveloper4(userData, cancellationToken); + return _meadowDevice.SetDeveloper(level, userData, cancellationToken); } public Task Uart1Apps(CancellationToken cancellationToken = default) diff --git a/Meadow.CLI.Core/Devices/MeadowLocalDevice.FileManager.cs b/Meadow.CLI.Core/Devices/MeadowLocalDevice.FileManager.cs index 75e16750..01da0693 100644 --- a/Meadow.CLI.Core/Devices/MeadowLocalDevice.FileManager.cs +++ b/Meadow.CLI.Core/Devices/MeadowLocalDevice.FileManager.cs @@ -43,7 +43,7 @@ public async Task> GetFilesAndFolders( } }; - var command = new SimpleCommandBuilder(HcomMeadowRequestType.HCOM_MDOW_REQUEST_DEVELOPER_4) + var command = new SimpleCommandBuilder(HcomMeadowRequestType.HCOM_MDOW_REQUEST_GET_FILES_AND_FOLDERS) .WithResponseHandler(handler) .Build(); diff --git a/Meadow.CLI.Core/Devices/MeadowLocalDevice.cs b/Meadow.CLI.Core/Devices/MeadowLocalDevice.cs index 8fe7f0f3..12cd1437 100644 --- a/Meadow.CLI.Core/Devices/MeadowLocalDevice.cs +++ b/Meadow.CLI.Core/Devices/MeadowLocalDevice.cs @@ -233,40 +233,10 @@ public Task SetTraceLevel(uint traceLevel, CancellationToken cancellationToken = return SendCommand(command, cancellationToken); } - public Task SetDeveloper1(uint userData, CancellationToken cancellationToken = default) + public Task SetDeveloper(ushort level, uint userData, CancellationToken cancellationToken = default) { - var command = - new SimpleCommandBuilder(HcomMeadowRequestType.HCOM_MDOW_REQUEST_DEVELOPER_1) - .WithUserData(userData) - .Build(); - - return SendCommand(command, cancellationToken); - } - - public Task SetDeveloper2(uint userData, CancellationToken cancellationToken = default) - { - var command = - new SimpleCommandBuilder(HcomMeadowRequestType.HCOM_MDOW_REQUEST_DEVELOPER_2) - .WithUserData(userData) - .Build(); - - return SendCommand(command, cancellationToken); - } - - public Task SetDeveloper3(uint userData, CancellationToken cancellationToken = default) - { - var command = - new SimpleCommandBuilder(HcomMeadowRequestType.HCOM_MDOW_REQUEST_DEVELOPER_3) - .WithUserData(userData) - .Build(); - - return SendCommand(command, cancellationToken); - } - - public Task SetDeveloper4(uint userData, CancellationToken cancellationToken = default) - { - var command = - new SimpleCommandBuilder(HcomMeadowRequestType.HCOM_MDOW_REQUEST_DEVELOPER_4) + var command = new SimpleCommandBuilder(HcomMeadowRequestType.HCOM_MDOW_REQUEST_DEVELOPER) + .WithDeveloperLevel(level) .WithUserData(userData) .Build(); diff --git a/Meadow.CLI.Core/Internals/MeadowCommunication/Command.cs b/Meadow.CLI.Core/Internals/MeadowCommunication/Command.cs index ab653be3..8d60c1b1 100644 --- a/Meadow.CLI.Core/Internals/MeadowCommunication/Command.cs +++ b/Meadow.CLI.Core/Internals/MeadowCommunication/Command.cs @@ -8,12 +8,13 @@ public class Command { private protected const int HcomProtocolCommandRequiredHeaderLength = 12; private protected const int HcomProtocolCommandSeqNumber = 0; - private protected const ushort HcomProtocolExtraDataDefaultValue = 0x0000; + // TODO No longer required private protected const ushort HcomProtocolExtraDataDefaultValue = 0x0000; private protected const int HcomProtocolRequestMd5HashLength = 32; static internal ushort HcomProtocolCommunicationVersion = Constants.HCOM_PROTOCOL_CURRENT_VERSION_NUMBER; public Command(HcomMeadowRequestType requestType, TimeSpan timeout, + ushort developerLevel, uint userData, byte[]? data, Predicate responsePredicate, @@ -24,6 +25,7 @@ public Command(HcomMeadowRequestType requestType, { RequestType = requestType; Timeout = timeout; + DeveloperLevel = developerLevel; UserData = userData; Data = data; ResponsePredicate = responsePredicate; @@ -34,9 +36,10 @@ public Command(HcomMeadowRequestType requestType, } public HcomMeadowRequestType RequestType { get; protected set; } + public ushort DeveloperLevel { get; protected set; } public uint UserData { get; protected set; } - public TimeSpan Timeout { get; protected set; } public byte[]? Data { get; protected set; } + public TimeSpan Timeout { get; protected set; } public Predicate ResponsePredicate { get; protected set; } public Predicate CompletionPredicate { get; protected set; } public EventHandler? ResponseHandler { get; protected set; } @@ -77,9 +80,9 @@ protected int ToMessageBytes(ref byte[] messageBytes) offset += sizeof(ushort); - // Extra Data + // DeveloperLevel Array.Copy( - BitConverter.GetBytes(HcomProtocolExtraDataDefaultValue), + BitConverter.GetBytes(DeveloperLevel), 0, messageBytes, offset, @@ -99,7 +102,6 @@ protected int ToMessageBytes(ref byte[] messageBytes) messageBytes, HcomProtocolCommandRequiredHeaderLength, Data.Length); - offset += Data.Length; } diff --git a/Meadow.CLI.Core/Internals/MeadowCommunication/FileCommand.cs b/Meadow.CLI.Core/Internals/MeadowCommunication/FileCommand.cs index 0eb47309..c822d760 100644 --- a/Meadow.CLI.Core/Internals/MeadowCommunication/FileCommand.cs +++ b/Meadow.CLI.Core/Internals/MeadowCommunication/FileCommand.cs @@ -21,7 +21,7 @@ internal FileCommand(HcomMeadowRequestType requestType, Predicate responseHandler, Predicate completionHandler, string commandBuilder) - : base(requestType, timeout, partition, null, responseHandler, completionHandler, null, true, commandBuilder) + : base(requestType, timeout, 0, partition, null, responseHandler, completionHandler, null, true, commandBuilder) { SourceFileName = sourceFileName; DestinationFileName = destinationFileName; diff --git a/Meadow.CLI.Core/Internals/MeadowCommunication/SimpleCommandBuilder.cs b/Meadow.CLI.Core/Internals/MeadowCommunication/SimpleCommandBuilder.cs index b024f6af..23ae5b3a 100644 --- a/Meadow.CLI.Core/Internals/MeadowCommunication/SimpleCommandBuilder.cs +++ b/Meadow.CLI.Core/Internals/MeadowCommunication/SimpleCommandBuilder.cs @@ -17,6 +17,7 @@ public SimpleCommandBuilder(HcomMeadowRequestType requestType) private protected MeadowMessageType? CompletionMessageType; private protected HcomMeadowRequestType RequestType { get; set; } + private protected ushort DeveloperLevel { get; set; } private protected uint UserData { get; set; } private protected TimeSpan Timeout { get; set; } private protected byte[]? Data { get; set; } @@ -31,6 +32,12 @@ public SimpleCommandBuilder WithTimeout(TimeSpan timeout) return this; } + public SimpleCommandBuilder WithDeveloperLevel(ushort level) + { + DeveloperLevel = level; + return this; + } + public SimpleCommandBuilder WithUserData(uint userData) { UserData = userData; @@ -95,13 +102,14 @@ public Command Build() else CompletionPredicate = e => e.MessageType == MeadowMessageType.Concluded; } - return new Command(RequestType, Timeout, UserData, Data, ResponsePredicate, CompletionPredicate, ResponseHandler, IsAcknowledged, ToString()); + return new Command(RequestType, Timeout, DeveloperLevel, UserData, Data, ResponsePredicate, CompletionPredicate, ResponseHandler, IsAcknowledged, ToString()); } public override string ToString() { return $"RequestType: {RequestType} " + $"Timeout: {Timeout} " + + $"DeveloperLevel: {DeveloperLevel} " + $"UserData: {UserData} " + $"ResponseType {ResponseMessageType?.ToString() ?? "none"} " + $"CompletionMessageType: {CompletionMessageType?.ToString() ?? "none"} " diff --git a/Meadow.CLI/Commands/Trace/SetDeveloperValueCommand.cs b/Meadow.CLI/Commands/Trace/SetDeveloperValueCommand.cs index f923fad1..bad4be30 100644 --- a/Meadow.CLI/Commands/Trace/SetDeveloperValueCommand.cs +++ b/Meadow.CLI/Commands/Trace/SetDeveloperValueCommand.cs @@ -19,8 +19,8 @@ public SetDeveloperValueCommand(DownloadManager downloadManager, ILoggerFactory _logger = LoggerFactory.CreateLogger(); } - [CommandOption("developer", 'd', Description = "The developer value to set. Valid values are 1 - 4")] - public uint Developer { get; set; } + [CommandOption("developer", 'd', Description = "The developer value to set.")] + public ushort DeveloperLevel { get; set; } [CommandOption("value", 'v', Description = "The value to apply to the developer value. Valid values are 0 to 4,294,967,295")] public uint Value { get; set; } @@ -31,16 +31,15 @@ public override async ValueTask ExecuteAsync(IConsole console) var cancellationToken = console.RegisterCancellationHandler(); - var task = Developer switch + try { - 1 => Meadow.SetDeveloper1(Value, cancellationToken), - 2 => Meadow.SetDeveloper2(Value, cancellationToken), - 3 => Meadow.SetDeveloper3(Value, cancellationToken), - 4 => Meadow.SetDeveloper4(Value, cancellationToken), - _ => throw new ArgumentOutOfRangeException(nameof(Developer), Developer, "Valid values are 1 - 4") - }; - - await task; + + await Meadow.SetDeveloper(DeveloperLevel, Value, cancellationToken); + } + catch (Exception ex) + { + _logger.LogError($"Error Setting Developer : {ex.Message}"); + } } } } diff --git a/Meadow.Hcom/HcomMeadowRequestType.cs b/Meadow.Hcom/HcomMeadowRequestType.cs index 01e52700..04bb2671 100644 --- a/Meadow.Hcom/HcomMeadowRequestType.cs +++ b/Meadow.Hcom/HcomMeadowRequestType.cs @@ -40,11 +40,10 @@ public enum HcomMeadowRequestType : ushort HCOM_MDOW_REQUEST_GET_DEVICE_NAME = 0x1f | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE, HCOM_MDOW_REQUEST_GET_INITIAL_FILE_BYTES = 0x20 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE, - // Only used for testing - HCOM_MDOW_REQUEST_DEVELOPER_1 = 0xf0 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE, - HCOM_MDOW_REQUEST_DEVELOPER_2 = 0xf1 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE, - HCOM_MDOW_REQUEST_DEVELOPER_3 = 0xf2 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE, - HCOM_MDOW_REQUEST_DEVELOPER_4 = 0xf3 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE, + HCOM_MDOW_REQUEST_GET_FILES_AND_FOLDERS = 0xf3 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE, + + // Only used internally for testing + HCOM_MDOW_REQUEST_DEVELOPER = 0xf8 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE, HCOM_MDOW_REQUEST_S25FL_QSPI_INIT = 0xf4 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE, HCOM_MDOW_REQUEST_S25FL_QSPI_WRITE = 0xf5 | HcomProtocolHeaderTypes.HCOM_PROTOCOL_HEADER_TYPE_SIMPLE,