From f42638a1d46f7d65ebca341d9955c952092ad50e Mon Sep 17 00:00:00 2001 From: DerekGn Date: Tue, 21 Jan 2025 12:28:56 +0000 Subject: [PATCH 1/9] Code cleanup Naming suppressions --- MCP2221IO.UnitTests/DeviceTests.cs | 178 +++--- MCP2221IO.UnitTests/GlobalSuppressions.cs | 18 + MCP2221IO/Commands/BaseCommand.cs | 5 +- .../Commands/WriteSramSettingsCommand.cs | 10 +- MCP2221IO/Device.cs | 605 +++++++++--------- MCP2221IO/DeviceStatus.cs | 10 +- .../CommandExecutionFailedException.cs | 5 - .../Exceptions/GPIONotEnabledException.cs | 21 +- MCP2221IO/Exceptions/I2cOperationException.cs | 30 +- .../InvalidResponseTypeException.cs | 6 +- .../InvalidStreamLengthException.cs | 5 - MCP2221IO/Exceptions/ReadRequiredException.cs | 5 - .../SmBusInvalidAddressSizeException.cs | 5 - .../Exceptions/SmBusInvalidCrcException.cs | 24 +- MCP2221IO/GlobalSuppressions.cs | 50 ++ MCP2221IO/Gpio/GpioPort.cs | 34 +- MCP2221IO/I2CAddress.cs | 4 +- MCP2221IO/IDevice.cs | 56 +- .../Responses/FactorySerialNumberResponse.cs | 1 + MCP2221IO/Responses/GetI2CDataResponse.cs | 2 +- .../Responses/ReadChipSettingsResponse.cs | 2 +- .../Responses/ReadFlashStringResponse.cs | 1 + MCP2221IO/Responses/ReadGpSettingsResponse.cs | 2 +- MCP2221IO/Responses/ReadGpioPortsResponse.cs | 8 +- .../Responses/ReadSramSettingsResponse.cs | 2 +- .../Responses/StatusSetParametersResponse.cs | 2 +- MCP2221IO/Settings/BaseGpPowerSettings.cs | 6 +- MCP2221IO/Settings/GpSettings.cs | 16 +- MCP2221IO/Settings/Password.cs | 11 +- MCP2221IO/Settings/SramSettings.cs | 10 +- MCP2221IO/Usb/HidSharpHidDevice.cs | 16 +- MCP2221IOConsole/Commands/BaseCommand.cs | 10 +- .../Flash/BaseWriteGpSetingsCommand.cs | 4 +- .../Commands/Flash/ReadChipSettingsCommand.cs | 4 +- .../Commands/Flash/ReadGpSettingsCommand.cs | 4 +- .../Flash/ReadUsbDescriptorsCommand.cs | 2 +- .../Flash/WriteChipSettingsCommand.cs | 37 +- .../Commands/Flash/WriteGp0SettingsCommand.cs | 4 +- .../Commands/Flash/WriteGp1SettingsCommand.cs | 4 +- .../Commands/Flash/WriteGp2SettingsCommand.cs | 4 +- .../Commands/Flash/WriteGp3SettingsCommand.cs | 4 +- .../Flash/WriteUsbDescriptorsCommand.cs | 4 +- .../Commands/Gpio/GpioReadSettingsCommand.cs | 2 +- .../Commands/Gpio/GpioWriteSettingsCommand.cs | 14 +- .../Commands/I2c/BaseI2cWriteCommand.cs | 2 +- .../Commands/I2c/I2cCancelTransferCommand.cs | 4 +- .../Commands/I2c/I2cReadDataCommand.cs | 2 +- .../I2c/I2cReadDataRepeatStartCommand.cs | 2 +- .../Commands/I2c/I2cScanBusCommand.cs | 2 +- .../I2c/I2cSetTransferSpeedCommand.cs | 2 +- .../Commands/I2c/I2cWriteDataCommand.cs | 6 +- .../Commands/I2c/I2cWriteDataNoStopCommand.cs | 6 +- .../I2c/I2cWriteDataRepeatStartCommand.cs | 6 +- MCP2221IOConsole/Commands/ResetCommand.cs | 2 +- .../Commands/SmBus/SmBusBlockReadCommand.cs | 2 +- .../Commands/SmBus/SmBusBlockWriteCommand.cs | 8 +- .../Commands/SmBus/SmBusQuickCommand.cs | 2 +- .../Commands/SmBus/SmBusReadByteCommand.cs | 2 +- .../Commands/SmBus/SmBusReadCommand.cs | 2 +- .../Commands/SmBus/SmBusReadIntCommand.cs | 2 +- .../Commands/SmBus/SmBusReadLongCommand.cs | 2 +- .../Commands/SmBus/SmBusReadShortCommand.cs | 2 +- .../Commands/SmBus/SmBusWriteByteCommand.cs | 2 +- .../Commands/SmBus/SmBusWriteCommand.cs | 2 +- .../Commands/SmBus/SmBusWriteIntCommand.cs | 2 +- .../Commands/SmBus/SmBusWriteLongCommand.cs | 2 +- .../Commands/SmBus/SmBusWriteShortCommand.cs | 2 +- .../Sram/BaseSramWriteGpSettingsCommand.cs | 4 +- .../Commands/Sram/SramReadSettingsCommand.cs | 4 +- .../Sram/SramWriteGp0SettingsCommand.cs | 4 +- .../Sram/SramWriteGp1SettingsCommand.cs | 4 +- .../Sram/SramWriteGp2SettingsCommand.cs | 4 +- .../Sram/SramWriteGp3SettingsCommand.cs | 4 +- .../Commands/Sram/SramWriteSettingsCommand.cs | 22 +- .../Commands/Status/StatusReadCommand.cs | 4 +- MCP2221IOConsole/Commands/UnlockCommand.cs | 3 +- MCP2221IOConsole/GlobalSuppressions.cs | 35 + MCP2221IOConsole/MCP2221IOConsole.csproj | 4 +- .../Parsers/BaseIntValueParser.cs | 14 +- .../Parsers/ByteListValueParser.cs | 8 +- MCP2221IOConsole/Program.cs | 8 +- PModAqs/Commands/BaseCommand.cs | 12 +- PModAqs/Commands/DataCommand.cs | 2 +- PModAqs/Commands/ErrorCommand.cs | 2 +- PModAqs/Commands/ReadModeCommand.cs | 2 +- PModAqs/Commands/ReadRawDataCommand.cs | 2 +- PModAqs/Commands/ResetCommand.cs | 2 +- PModAqs/Commands/StartCommand.cs | 2 +- PModAqs/Commands/StatusCommand.cs | 2 +- PModAqs/Commands/VersionCommand.cs | 2 +- PModAqs/Commands/WriteModeCommand.cs | 2 +- PModAqs/GlobalSuppressions.cs | 13 + PModAqs/Program.cs | 8 +- PModAqs/Sensor/Ccs811.cs | 20 +- 94 files changed, 811 insertions(+), 695 deletions(-) create mode 100644 MCP2221IO.UnitTests/GlobalSuppressions.cs create mode 100644 MCP2221IO/GlobalSuppressions.cs create mode 100644 MCP2221IOConsole/GlobalSuppressions.cs create mode 100644 PModAqs/GlobalSuppressions.cs diff --git a/MCP2221IO.UnitTests/DeviceTests.cs b/MCP2221IO.UnitTests/DeviceTests.cs index 6ceac96..f312869 100644 --- a/MCP2221IO.UnitTests/DeviceTests.cs +++ b/MCP2221IO.UnitTests/DeviceTests.cs @@ -42,14 +42,14 @@ namespace MCP2221IO.UnitTests { public class DeviceTests { + private readonly Device _device; private readonly Mock _mockHidDevice; private readonly ITestOutputHelper _output; - private readonly Device _device; public DeviceTests(ITestOutputHelper output) { _mockHidDevice = new Mock(); - _device = new Device(Mock.Of>(), _mockHidDevice.Object); + _device = new Device(Mock.Of>(), _mockHidDevice.Object); _output = output; } @@ -75,7 +75,7 @@ public void TestGetFactorySerialNumberOk() .Returns(TestPayloads.FactorySerialNumberResponse); // Act - string serialNumber = _device.FactorySerialNumber; + string serialNumber = _device.FactorySerialNumber!; // Assert serialNumber.Should().NotBeNull(); @@ -159,7 +159,9 @@ public void TestI2cReadAddressNull() // Arrange // Act +#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. Action act = () => { var buffer = _device.I2cReadData(null, 1); }; +#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. // Assert act.Should().Throw(); @@ -243,7 +245,9 @@ public void TestI2cWriteDataAddressNull() // Arrange // Act +#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. Action act = () => { _device.I2cWriteData(null, new List()); }; +#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. // Assert act.Should().Throw(); @@ -255,7 +259,9 @@ public void TestI2cWriteDataDataNull() // Arrange // Act +#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. Action act = () => { _device.I2cWriteData(new I2cAddress(0x0A), null); }; +#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. // Assert act.Should().Throw(); @@ -340,7 +346,7 @@ public void TestReadChipSettings() // Assert _device.ChipSettings.Should().NotBeNull(); - _output.WriteLine(_device.ChipSettings.ToString()); + _output.WriteLine(_device.ChipSettings!.ToString()); } [Fact] @@ -356,7 +362,7 @@ public void TestReadDeviceStatus() // Assert _device.Status.Should().NotBeNull(); - _output.WriteLine(_device.Status.ToString()); + _output.WriteLine(_device.Status!.ToString()); } [Fact] @@ -391,7 +397,7 @@ public void TestReadGpSettings() // Assert _device.GpSettings.Should().NotBeNull(); - _output.WriteLine(_device.GpSettings.ToString()); + _output.WriteLine(_device.GpSettings!.ToString()); } [Fact] @@ -407,117 +413,98 @@ public void TestReadSramSettings() // Assert _device.SramSettings.Should().NotBeNull(); - _output.WriteLine(_device.SramSettings.ToString()); + _output.WriteLine(_device.SramSettings!.ToString()); } [Fact] - public void TestSetUsbManufacturerDescriptorOk() + public void TestReset() { // Arrange - byte[] descriptor = null; - - _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) - .Returns(WriteFlashWriteResponse(0)) - .Callback(b => descriptor = b); // Act - _device.UsbManufacturerDescriptor = "UPDATED"; + _device.Reset(); // Assert - descriptor.Should().NotBeNull(); + _mockHidDevice.Verify(_ => _.Write(It.IsAny()), Times.Once); } [Fact] - public void TestSetUsbProductDescriptorOk() + public void TestSetI2cBusSpeed() { // Arrange - byte[] descriptor = null; - _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) - .Returns(WriteFlashWriteResponse(0)) - .Callback(b => descriptor = b); + .Returns(TestPayloads.DeviceStatusResponse); // Act - _device.UsbProductDescriptor = "UPDATED"; + _device.SetI2cBusSpeed(100); // Assert - descriptor.Should().NotBeNull(); + _device.Status.Should().NotBeNull(); } [Fact] - public void TestSetUsbSerialNumberDescriptorOk() + public void TestSetI2cBusSpeedTimeout() { // Arrange - byte[] descriptor = null; - _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) - .Returns(WriteFlashWriteResponse(0)) - .Callback(b => descriptor = b); + .Returns(TestPayloads.DeviceStatusSetSpeedFailedResponse); // Act - _device.UsbSerialNumberDescriptor = "SERIAL NUMBER"; + Action act = () => { _device.SetI2cBusSpeed(100); }; // Assert - descriptor.Should().NotBeNull(); + act.Should().Throw(); } [Fact] - public void WriteGpioPorts() + public void TestSetUsbManufacturerDescriptorOk() { // Arrange + byte[]? descriptor = null; + _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) - .Returns(WriteSetGpioValuesResponse()); + .Returns(WriteFlashWriteResponse(0)) + .Callback(b => descriptor = b); - _device._gpioPortsRead = true; - _device.GpioPort0 = new GpioPort(); - _device.GpioPort1 = new GpioPort(); - _device.GpioPort2 = new GpioPort(); - _device.GpioPort3 = new GpioPort(); // Act - _device.WriteGpioPorts(); + _device.UsbManufacturerDescriptor = "UPDATED"; // Assert - _mockHidDevice.Verify(_ => _.WriteRead(It.IsAny()), Times.Once); + descriptor.Should().NotBeNull(); } [Fact] - public void TestReset() + public void TestSetUsbProductDescriptorOk() { // Arrange + byte[]? descriptor = null; - // Act - _device.Reset(); - - // Assert - _mockHidDevice.Verify(_ => _.Write(It.IsAny()), Times.Once); - } - - [Fact] - public void TestSetI2cBusSpeed() - { - // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) - .Returns(TestPayloads.DeviceStatusResponse); + .Returns(WriteFlashWriteResponse(0)) + .Callback(b => descriptor = b); // Act - _device.SetI2cBusSpeed(100); + _device.UsbProductDescriptor = "UPDATED"; // Assert - _device.Status.Should().NotBeNull(); + descriptor.Should().NotBeNull(); } [Fact] - public void TestSetI2cBusSpeedTimeout() + public void TestSetUsbSerialNumberDescriptorOk() { // Arrange + byte[]? descriptor = null; + _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) - .Returns(TestPayloads.DeviceStatusSetSpeedFailedResponse); + .Returns(WriteFlashWriteResponse(0)) + .Callback(b => descriptor = b); // Act - Action act = () => { _device.SetI2cBusSpeed(100); }; + _device.UsbSerialNumberDescriptor = "SERIAL NUMBER"; // Assert - act.Should().Throw(); + descriptor.Should().NotBeNull(); } [Fact] @@ -539,8 +526,8 @@ public void TestSmBusBlockRead() [Fact] public void TestSmBusBlockWrite() { - byte[] writeData = null; - + byte[]? writeData = null; + // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) .Returns(WriteReponse(CommandCodes.WriteI2cData)) @@ -662,7 +649,7 @@ public void TestSmBusReadWordCommand() [Fact] public void TestSmBusWriteByte() { - byte[] writeData = null; + byte[]? writeData = null; // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) @@ -682,7 +669,7 @@ public void TestSmBusWriteByte() [Fact] public void TestSmBusWriteByteCommand() { - byte[] writeData = null; + byte[]? writeData = null; // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) @@ -702,7 +689,7 @@ public void TestSmBusWriteByteCommand() [Fact] public void TestSmBusWriteIntCommand() { - byte[] writeData = null; + byte[]? writeData = null; // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) @@ -722,7 +709,7 @@ public void TestSmBusWriteIntCommand() [Fact] public void TestSmBusWriteLongCommand() { - byte[] writeData = null; + byte[]? writeData = null; // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) @@ -742,7 +729,7 @@ public void TestSmBusWriteLongCommand() [Fact] public void TestSmBusWriteShortCommand() { - byte[] writeData = null; + byte[]? writeData = null; // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) @@ -834,23 +821,36 @@ public void TestWriteSramSettings() _mockHidDevice.Verify(_ => _.WriteRead(It.IsAny()), Times.Once); } - private byte[] WriteSetGpioValuesResponse() + [Fact] + public void WriteGpioPorts() { - MemoryStream stream = GetStream(); - stream.WriteByte((byte)CommandCodes.SetGpioValues); + // Arrange + _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) + .Returns(WriteSetGpioValuesResponse()); - return stream.ToArray(); + _device._gpioPortsRead = true; + _device.GpioPort0 = new GpioPort(); + _device.GpioPort1 = new GpioPort(); + _device.GpioPort2 = new GpioPort(); + _device.GpioPort3 = new GpioPort(); + // Act + _device.WriteGpioPorts(); + + // Assert + _mockHidDevice.Verify(_ => _.WriteRead(It.IsAny()), Times.Once); } - private byte[] WriteReponse(CommandCodes commandCode) + private static MemoryStream GetStream() { - MemoryStream stream = GetStream(); - stream.WriteByte((byte)commandCode); + MemoryStream stream = new MemoryStream(); - return stream.ToArray(); + stream.Write(new byte[65], 0, 65); + stream.Position = 0; + stream.WriteByte(0); + return stream; } - private byte[] WriteFlashUnlockResponse() + private static byte[] WriteFlashUnlockResponse() { MemoryStream stream = GetStream(); stream.WriteByte((byte)CommandCodes.SendFlashAccessPassword); @@ -859,7 +859,7 @@ private byte[] WriteFlashUnlockResponse() return stream.ToArray(); } - private byte[] WriteFlashWriteResponse(int status) + private static byte[] WriteFlashWriteResponse(int status) { MemoryStream stream = GetStream(); stream.WriteByte((byte)CommandCodes.WriteFlashData); @@ -868,17 +868,7 @@ private byte[] WriteFlashWriteResponse(int status) return stream.ToArray(); } - private static MemoryStream GetStream() - { - MemoryStream stream = new MemoryStream(); - - stream.Write(new byte[65], 0, 65); - stream.Position = 0; - stream.WriteByte(0); - return stream; - } - - private byte[] WriteGetI2cDataResponse(CommandCodes commandCode, params byte[] data) + private static byte[] WriteGetI2cDataResponse(CommandCodes commandCode, params byte[] data) { MemoryStream stream = GetStream(); stream.WriteByte((byte)commandCode); @@ -890,7 +880,7 @@ private byte[] WriteGetI2cDataResponse(CommandCodes commandCode, params byte[] d return stream.ToArray(); } - private byte[] WriteGetI2cDataResponse(CommandCodes commandCode, int length) + private static byte[] WriteGetI2cDataResponse(CommandCodes commandCode, int length) { MemoryStream stream = GetStream(); stream.WriteByte((byte)commandCode); @@ -901,5 +891,21 @@ private byte[] WriteGetI2cDataResponse(CommandCodes commandCode, int length) return stream.ToArray(); } + + private static byte[] WriteReponse(CommandCodes commandCode) + { + MemoryStream stream = GetStream(); + stream.WriteByte((byte)commandCode); + + return stream.ToArray(); + } + + private static byte[] WriteSetGpioValuesResponse() + { + MemoryStream stream = GetStream(); + stream.WriteByte((byte)CommandCodes.SetGpioValues); + + return stream.ToArray(); + } } } \ No newline at end of file diff --git a/MCP2221IO.UnitTests/GlobalSuppressions.cs b/MCP2221IO.UnitTests/GlobalSuppressions.cs new file mode 100644 index 0000000..24017ac --- /dev/null +++ b/MCP2221IO.UnitTests/GlobalSuppressions.cs @@ -0,0 +1,18 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IO.UnitTests")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.TestGetUsbManufacturerDescriptorOk")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.TestGetUsbProductDescriptorOk")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.TestReadGpioSettings")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.WriteGpioPorts")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.TestReadSramSettings")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.TestGetUsbSerialNumberDescriptorOk")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.TestSetUsbProductDescriptorOk")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.TestSetUsbManufacturerDescriptorOk")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.TestSetUsbSerialNumberDescriptorOk")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.UnitTests.DeviceTests.TestWriteSramSettings")] diff --git a/MCP2221IO/Commands/BaseCommand.cs b/MCP2221IO/Commands/BaseCommand.cs index fa156f9..54915ab 100644 --- a/MCP2221IO/Commands/BaseCommand.cs +++ b/MCP2221IO/Commands/BaseCommand.cs @@ -44,10 +44,7 @@ protected BaseCommand(CommandCodes commandCode) // public virtual void Serialize(Stream stream) { - if (stream == null) - { - throw new ArgumentNullException(nameof(stream)); - } + ArgumentNullException.ThrowIfNull(stream); if (stream.Length != 64) { diff --git a/MCP2221IO/Commands/WriteSramSettingsCommand.cs b/MCP2221IO/Commands/WriteSramSettingsCommand.cs index 48ebcbc..c7a74d3 100644 --- a/MCP2221IO/Commands/WriteSramSettingsCommand.cs +++ b/MCP2221IO/Commands/WriteSramSettingsCommand.cs @@ -29,7 +29,7 @@ namespace MCP2221IO.Commands { /// - /// + /// Write sram settings command /// internal class WriteSramSettingsCommand : BaseCommand { @@ -79,10 +79,10 @@ public override void Serialize(Stream stream) stream.WriteByte(0x80); - SramSettings.Gp0Settings.Serialize(stream); - SramSettings.Gp1Settings.Serialize(stream); - SramSettings.Gp2Settings.Serialize(stream); - SramSettings.Gp3Settings.Serialize(stream); + SramSettings.Gp0Settings!.Serialize(stream); + SramSettings.Gp1Settings!.Serialize(stream); + SramSettings.Gp2Settings!.Serialize(stream); + SramSettings.Gp3Settings!.Serialize(stream); } } } diff --git a/MCP2221IO/Device.cs b/MCP2221IO/Device.cs index 3630a3d..2765514 100644 --- a/MCP2221IO/Device.cs +++ b/MCP2221IO/Device.cs @@ -43,45 +43,48 @@ namespace MCP2221IO /// public class Device : IDevice { - internal const int MaxPacketSize = 64; internal const int MaxBlockSize = 59; + internal const int MaxPacketSize = 64; internal bool _gpioPortsRead = false; private const int MaxRetries = 5; private readonly ILogger _logger; - private string _factorySerialNumber; - private IHidDevice _hidDevice; + private string? _factorySerialNumber; + private IHidDevice? _hidDevice; - public Device(ILogger logger, IHidDevice hidDevice) + public Device(ILogger logger, IHidDevice hidDevice) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _hidDevice = hidDevice ?? throw new ArgumentNullException(nameof(hidDevice)); } // - public DeviceStatus Status { get; internal set; } + public ChipSettings? ChipSettings { get; internal set; } + + // + public string? FactorySerialNumber => GetFactorySerialNumber(); // - public ChipSettings ChipSettings { get; internal set; } + public GpioPort? GpioPort0 { get; internal set; } // - public GpSettings GpSettings { get; internal set; } + public GpioPort? GpioPort1 { get; internal set; } // - public SramSettings SramSettings { get; internal set; } + public GpioPort? GpioPort2 { get; internal set; } // - public GpioPort GpioPort0 { get; internal set; } + public GpioPort? GpioPort3 { get; internal set; } // - public GpioPort GpioPort1 { get; internal set; } + public GpSettings? GpSettings { get; internal set; } // - public GpioPort GpioPort2 { get; internal set; } + public SramSettings? SramSettings { get; internal set; } // - public GpioPort GpioPort3 { get; internal set; } + public DeviceStatus? Status { get; internal set; } // public string UsbManufacturerDescriptor @@ -136,164 +139,161 @@ public string UsbSerialNumberDescriptor } // - public string FactorySerialNumber => GetFactorySerialNumber(); - - // - public void UnlockFlash(Password password) + public void CancelI2cBusTransfer() { HandleOperationExecution( nameof(Device), () => { - ExecuteCommand(new UnlockFlashCommand(password)); + Status = ExecuteCommand(new CancelI2cBusTransferCommand()).DeviceStatus; }); } // - public void ReadDeviceStatus() + public void Close() { - HandleOperationExecution( + HandleOperationExecution(nameof(Device), () => Dispose()); + } + + // + public IList I2cReadData(I2cAddress address, ushort length) + { + return HandleOperationExecution( nameof(Device), () => { - Status = ExecuteCommand(new ReadStatusSetParametersCommand()).DeviceStatus; + return I2cReadData(CommandCodes.ReadI2cData, address, length); }); } // - public void ReadChipSettings() + public IList I2cReadDataRepeatedStart(I2cAddress address, ushort length) { - HandleOperationExecution( + return HandleOperationExecution( nameof(Device), () => { - ChipSettings = ExecuteCommand(new ReadChipSettingsCommand()).ChipSettings; + return I2cReadData(CommandCodes.ReadI2cDataRepeatedStart, address, length); }); } // - public void ReadGpSettings() + public IList I2cScanBus(bool useTenBitAddressing) + { + uint upperAddress = useTenBitAddressing ? I2cAddress.TenBitRangeUpper : I2cAddress.SevenBitRangeUpper; + + return I2cScanBusInternal(useTenBitAddressing, upperAddress); + } + + // + public void I2cWriteData(I2cAddress address, IList data) { HandleOperationExecution( nameof(Device), () => { - GpSettings = ExecuteCommand(new ReadGpSettingsCommand()).GpSettings; + I2cWriteData(CommandCodes.WriteI2cData, address, data); }); } // - public void ReadSramSettings() + public void I2cWriteDataNoStop(I2cAddress address, IList data) { HandleOperationExecution( nameof(Device), () => { - SramSettings = ExecuteCommand(new ReadSramSettingsCommand()).SramSettings; + I2cWriteData(CommandCodes.WriteI2cDataNoStop, address, data); }); } // - public void ReadGpioPorts() + public void I2cWriteDataRepeatStart(I2cAddress address, IList data) { HandleOperationExecution( nameof(Device), () => { - var response = ExecuteCommand(new ReadGpioPortsCommand()); - - GpioPort0 = response.GpioPort0; - GpioPort1 = response.GpioPort1; - GpioPort2 = response.GpioPort2; - GpioPort3 = response.GpioPort3; - - _gpioPortsRead = true; + I2cWriteData(CommandCodes.WriteI2cDataRepeatedStart, address, data); }); } // - public void WriteChipSettings(Password password) + public void Open() + { + HandleOperationExecution(nameof(Device), () => _hidDevice!.Open()); + } + + // + public void ReadChipSettings() { HandleOperationExecution( nameof(Device), () => { - if (ChipSettings == null) - { - throw new ReadRequiredException($"{nameof(ChipSettings)} must be read from the device"); - } - - ExecuteCommand(new WriteChipSettingsCommand(ChipSettings, password)); + ChipSettings = ExecuteCommand(new ReadChipSettingsCommand()).ChipSettings; }); } // - public void WriteGpSettings() + public void ReadDeviceStatus() { HandleOperationExecution( nameof(Device), () => { - if (GpSettings == null) - { - throw new ReadRequiredException($"{nameof(GpSettings)} must be read from the device"); - } - - ExecuteCommand(new WriteGpSettingsCommand(GpSettings)); + Status = ExecuteCommand(new ReadStatusSetParametersCommand()).DeviceStatus; }); } // - public void WriteSramSettings(bool clearInterrupts) + public void ReadGpioPorts() { HandleOperationExecution( nameof(Device), () => { - if (SramSettings == null) - { - throw new ReadRequiredException($"{nameof(SramSettings)} must be read from the device"); - } + var response = ExecuteCommand(new ReadGpioPortsCommand()); - ExecuteCommand(new WriteSramSettingsCommand(SramSettings, clearInterrupts)); + GpioPort0 = response.GpioPort0; + GpioPort1 = response.GpioPort1; + GpioPort2 = response.GpioPort2; + GpioPort3 = response.GpioPort3; + + _gpioPortsRead = true; }); } // - public void WriteGpioPorts() + public void ReadGpSettings() { HandleOperationExecution( nameof(Device), () => { - if (!_gpioPortsRead) - { - throw new ReadRequiredException($"Gpio ports must be read from the device"); - } - - ExecuteCommand(new WriteGpioPortsCommand(GpioPort0, GpioPort1, GpioPort2, GpioPort3)); + GpSettings = ExecuteCommand(new ReadGpSettingsCommand()).GpSettings; }); } // - public void Reset() + public void ReadSramSettings() { HandleOperationExecution( nameof(Device), () => { - ExecuteCommand(new ResetCommand()); + SramSettings = ExecuteCommand(new ReadSramSettingsCommand()).SramSettings; }); } // - public void CancelI2cBusTransfer() + public void Reset() { HandleOperationExecution( nameof(Device), () => { - Status = ExecuteCommand(new CancelI2cBusTransferCommand()).DeviceStatus; + ExecuteCommand(new ResetCommand()); }); } @@ -311,8 +311,7 @@ public void SetI2cBusSpeed(int speed) Status = ExecuteCommand(new SetI2cBusSpeedCommand(speed)).DeviceStatus; retryCount++; - - } while (Status.SpeedStatus != I2cSpeedStatus.Set && retryCount < MaxRetries); + } while (Status!.SpeedStatus != I2cSpeedStatus.Set && retryCount < MaxRetries); if (retryCount == MaxRetries) { @@ -322,78 +321,51 @@ public void SetI2cBusSpeed(int speed) } // - public void Open() + public IList SmBusBlockRead(I2cAddress address, byte command, byte count, bool errorCheck = false) { - HandleOperationExecution(nameof(Device), () => _hidDevice.Open()); - } - - // - public void Close() - { - HandleOperationExecution(nameof(Device), () => Dispose()); - } - - // - public void I2cWriteData(I2cAddress address, IList data) - { - HandleOperationExecution( + return HandleOperationExecution( nameof(Device), () => { - I2cWriteData(CommandCodes.WriteI2cData, address, data); - }); - } + List writeData = new List() { command }; - // - public void I2cWriteDataRepeatStart(I2cAddress address, IList data) - { - HandleOperationExecution( - nameof(Device), - () => - { - I2cWriteData(CommandCodes.WriteI2cDataRepeatedStart, address, data); + I2cWriteData(CommandCodes.WriteI2cDataNoStop, address, writeData); + + var result = I2cReadData(CommandCodes.ReadI2cDataRepeatedStart, address, count); + + if (errorCheck) + { + AssertPec(result); + } + + return result; }); } // - public void I2cWriteDataNoStop(I2cAddress address, IList data) + public void SmBusBlockWrite(I2cAddress address, byte command, IList block, bool errorCheck = false) { HandleOperationExecution( nameof(Device), () => { - I2cWriteData(CommandCodes.WriteI2cDataNoStop, address, data); - }); - } + AssertAddress(address); - // - public IList I2cReadData(I2cAddress address, ushort length) - { - return HandleOperationExecution( - nameof(Device), - () => - { - return I2cReadData(CommandCodes.ReadI2cData, address, length); - }); - } + if (block.Count > IDevice.MaxSmBusBlockSize) + { + throw new ArgumentOutOfRangeException(nameof(block), $"Must be less than [0x{IDevice.MaxSmBusBlockSize}]"); + } - // - public IList I2cReadDataRepeatedStart(I2cAddress address, ushort length) - { - return HandleOperationExecution( - nameof(Device), - () => - { - return I2cReadData(CommandCodes.ReadI2cDataRepeatedStart, address, length); - }); - } + List writeData = new List() { command, (byte)block.Count }; + writeData.AddRange(block); - // - public IList I2cScanBus(bool useTenBitAddressing) - { - uint upperAddress = useTenBitAddressing ? I2cAddress.TenBitRangeUpper : I2cAddress.SevenBitRangeUpper; + if (errorCheck) + { + writeData.Add(Crc8.ComputeChecksum(writeData)); + } - return I2cScanBusInternal(useTenBitAddressing, upperAddress); + I2cWriteData(address, writeData); + }); } // @@ -417,7 +389,7 @@ public void SmBusQuickCommand(I2cAddress address, bool write) } // - public byte SmBusReadByte(I2cAddress address, bool pec = false) + public byte SmBusReadByte(I2cAddress address, bool errorCheck = false) { return HandleOperationExecution( nameof(Device), @@ -427,7 +399,7 @@ public byte SmBusReadByte(I2cAddress address, bool pec = false) var result = I2cReadData(CommandCodes.ReadI2cData, address, sizeof(byte)); - if (pec) + if (errorCheck) { AssertPec(result); } @@ -437,7 +409,31 @@ public byte SmBusReadByte(I2cAddress address, bool pec = false) } // - public void SmBusWriteByte(I2cAddress address, byte data, bool pec = false) + public byte SmBusReadByteCommand(I2cAddress address, byte command, bool errorCheck = false) + { + return SmBusReadCommand(address, command, sizeof(byte), errorCheck)[0]; + } + + // + public int SmBusReadIntCommand(I2cAddress address, byte command, bool errorCheck = false) + { + return BitConverter.ToInt32(SmBusReadCommand(address, command, sizeof(int), errorCheck).Take(sizeof(int)).ToArray()); + } + + // + public long SmBusReadLongCommand(I2cAddress address, byte command, bool errorCheck = false) + { + return BitConverter.ToInt64(SmBusReadCommand(address, command, sizeof(long), errorCheck).Take(sizeof(long)).ToArray()); + } + + // + public short SmBusReadShortCommand(I2cAddress address, byte command, bool errorCheck = false) + { + return BitConverter.ToInt16(SmBusReadCommand(address, command, sizeof(short), errorCheck).Take(sizeof(short)).ToArray()); + } + + // + public void SmBusWriteByte(I2cAddress address, byte data, bool errorCheck = false) { HandleOperationExecution( nameof(Device), @@ -447,7 +443,7 @@ public void SmBusWriteByte(I2cAddress address, byte data, bool pec = false) List writeData = new List() { data }; - if (pec) + if (errorCheck) { writeData.Add(Crc8.ComputeChecksum(new List() { data })); } @@ -457,98 +453,101 @@ public void SmBusWriteByte(I2cAddress address, byte data, bool pec = false) } // - public byte SmBusReadByteCommand(I2cAddress address, byte command, bool pec = false) + public void SmBusWriteByteCommand(I2cAddress address, byte command, byte data, bool errorCheck = false) { - return SmBusReadCommand(address, command, sizeof(byte), pec).First(); + SmBusWriteCommand(address, command, errorCheck, data); } // - public void SmBusWriteByteCommand(I2cAddress address, byte command, byte data, bool pec = false) + public void SmBusWriteIntCommand(I2cAddress address, byte command, int data, bool errorCheck = false) { - SmBusWriteCommand(address, command, pec, data); + SmBusWriteCommand(address, command, errorCheck, BitConverter.GetBytes(data)); } // - public short SmBusReadShortCommand(I2cAddress address, byte command, bool pec = false) + public void SmBusWriteLongCommand(I2cAddress address, byte command, long data, bool errorCheck = false) { - return BitConverter.ToInt16(SmBusReadCommand(address, command, sizeof(short), pec).Take(sizeof(short)).ToArray()); + SmBusWriteCommand(address, command, errorCheck, BitConverter.GetBytes(data)); } // - public void SmBusWriteShortCommand(I2cAddress address, byte command, short data, bool pec = false) + public void SmBusWriteShortCommand(I2cAddress address, byte command, short data, bool errorCheck = false) { - SmBusWriteCommand(address, command, pec, BitConverter.GetBytes(data)); + SmBusWriteCommand(address, command, errorCheck, BitConverter.GetBytes(data)); } // - public int SmBusReadIntCommand(I2cAddress address, byte command, bool pec = false) - { - return BitConverter.ToInt32(SmBusReadCommand(address, command, sizeof(int), pec).Take(sizeof(int)).ToArray()); - } - - // - public void SmBusWriteIntCommand(I2cAddress address, byte command, int data, bool pec = false) + public void UnlockFlash(Password password) { - SmBusWriteCommand(address, command, pec, BitConverter.GetBytes(data)); + HandleOperationExecution( + nameof(Device), + () => + { + ExecuteCommand(new UnlockFlashCommand(password)); + }); } // - public long SmBusReadLongCommand(I2cAddress address, byte command, bool pec = false) + public void WriteChipSettings(Password password) { - return BitConverter.ToInt64(SmBusReadCommand(address, command, sizeof(long), pec).Take(sizeof(long)).ToArray()); - } + HandleOperationExecution( + nameof(Device), + () => + { + if (ChipSettings == null) + { + throw new ReadRequiredException($"{nameof(ChipSettings)} must be read from the device"); + } - // - public void SmBusWriteLongCommand(I2cAddress address, byte command, long data, bool pec = false) - { - SmBusWriteCommand(address, command, pec, BitConverter.GetBytes(data)); + ExecuteCommand(new WriteChipSettingsCommand(ChipSettings, password)); + }); } // - public IList SmBusBlockRead(I2cAddress address, byte command, byte count, bool pec = false) + public void WriteGpioPorts() { - return HandleOperationExecution( + HandleOperationExecution( nameof(Device), () => { - List writeData = new List() { command }; - - I2cWriteData(CommandCodes.WriteI2cDataNoStop, address, writeData); - - var result = I2cReadData(CommandCodes.ReadI2cDataRepeatedStart, address, count); - - if (pec) + if (!_gpioPortsRead) { - AssertPec(result); + throw new ReadRequiredException($"Gpio ports must be read from the device"); } - return result; + ExecuteCommand(new WriteGpioPortsCommand(GpioPort0!, GpioPort1!, GpioPort2!, GpioPort3!)); }); } // - public void SmBusBlockWrite(I2cAddress address, byte command, IList block, bool pec = false) + public void WriteGpSettings() { HandleOperationExecution( nameof(Device), () => { - AssertAddress(address); - - if(block.Count > IDevice.MaxSmBusBlockSize) + if (GpSettings == null) { - throw new ArgumentOutOfRangeException(nameof(block), $"Must be less than [0x{IDevice.MaxSmBusBlockSize}]"); + throw new ReadRequiredException($"{nameof(GpSettings)} must be read from the device"); } - List writeData = new List() { command, (byte)block.Count }; - writeData.AddRange(block); + ExecuteCommand(new WriteGpSettingsCommand(GpSettings)); + }); + } - if (pec) + // + public void WriteSramSettings(bool clearInterrupts) + { + HandleOperationExecution( + nameof(Device), + () => + { + if (SramSettings == null) { - writeData.Add(Crc8.ComputeChecksum(writeData)); + throw new ReadRequiredException($"{nameof(SramSettings)} must be read from the device"); } - I2cWriteData(address, writeData ); + ExecuteCommand(new WriteSramSettingsCommand(SramSettings, clearInterrupts)); }); } @@ -562,13 +561,13 @@ internal IList I2cScanBusInternal(bool useTenBitAddressing, uint upp try { - _logger.LogDebug($"Probing Device Address [0x{address.Value:X}]"); + _logger.LogDebug("Probing Device Address [0x{Address:X}]", address.Value); var response = I2cReadData(address, 1); if (response.Count > 0) { - _logger.LogDebug($"Read [{response.Count}] byte from Device Address [0x{address.Value:X}]"); + _logger.LogDebug("Read [{Count}] byte from Device Address [0x{Address:X}]", response.Count, address.Value); } result.Add(address); @@ -586,84 +585,140 @@ internal IList I2cScanBusInternal(bool useTenBitAddressing, uint upp return result; } - private IList SmBusReadCommand(I2cAddress address, byte command, ushort length, bool pec) + private static void AssertAddress(I2cAddress address) { - return HandleOperationExecution( - nameof(Device), - () => + if (address == null) + { + throw new ArgumentNullException(nameof(address)); + } + + if (address.Size != I2cAddressSize.SevenBit) + { + throw new SmBusInvalidAddressSizeException($"{nameof(I2cAddress)} size must be {I2cAddressSize.SevenBit}"); + } + } + + private static void AssertPec(IList result) + { + if (result.Count > 1) + { + byte expected = Crc8.ComputeChecksum(result.Take(result.Count - 1).ToList()); + + if (result[result.Count - 1] != expected) { - List writeData = new List() { command }; + throw new SmBusInvalidCrcException(expected, result[result.Count - 1], "PEC does not match"); + } + } + } - I2cWriteData(CommandCodes.WriteI2cDataNoStop, address, writeData); + private void ExecuteCommand(ICommand command) + { + var memoryStream = new MemoryStream(new byte[MaxPacketSize], true); - var result = I2cReadData(CommandCodes.ReadI2cDataRepeatedStart, address, length); + command.Serialize(memoryStream); - if (pec) - { - AssertPec(result); - } + _hidDevice!.Write(memoryStream.ToArray()); + } - return result; - }); + private T ExecuteCommand(ICommand command, bool checkResult = true) where T : IResponse, new() + { + var outStream = new MemoryStream(new byte[MaxPacketSize], true); + var inStream = new MemoryStream(); + + command.Serialize(outStream); + + inStream.Write(_hidDevice!.WriteRead(outStream.ToArray())); + + var result = new T(); + + result.Deserialize(inStream); + + if (checkResult && result.ExecutionResult != 0) + { + throw new CommandExecutionFailedException($"Unexpected command execution status Expected: [0x00] Actual [0x{result.ExecutionResult:x}]"); + } + + return result; } - private void SmBusWriteCommand(I2cAddress address, byte command, bool pec, params byte[] data) + private string GetFactorySerialNumber() { - HandleOperationExecution( + return HandleOperationExecution( nameof(Device), () => { - List writeData = new List() { command }; - writeData.AddRange(data); - - if (pec) + if (String.IsNullOrWhiteSpace(_factorySerialNumber)) { - writeData.Add(Crc8.ComputeChecksum(writeData)); + var response = ExecuteCommand(new ReadFactorySerialNumberCommand()); + + _factorySerialNumber = response.SerialNumber; } - I2cWriteData(CommandCodes.WriteI2cData, address, writeData); + return _factorySerialNumber; }); } - private void HandleScanError(I2cAddress address, Exception ex) + [DebuggerStepThrough] + private void HandleOperationExecution(string className, Action operation, [CallerMemberName] string memberName = "") { - _logger.LogWarning(ex, $"Device Address [0x{address.Value:X2}] did not respond"); + Stopwatch sw = new Stopwatch(); - CancelI2cBusTransfer(); + try + { + sw.Start(); + operation(); + sw.Stop(); + + _logger.LogDebug("Executed [{ClassName}].[{MemberName}] in [{TotalMilliseconds}] ms", + className, + memberName, + sw.Elapsed.TotalMilliseconds); + } + catch (Exception ex) + { + _logger.LogError(ex, "An exception occurred executing [{ClassName}].[{MemberName}]", className, memberName); + + throw; + } } - private void AssertAddress(I2cAddress address) + [DebuggerStepThrough] + private T HandleOperationExecution(string className, Func operation, [CallerMemberName] string memberName = "") { - if(address == null) + Stopwatch sw = new Stopwatch(); + T result; + + try { - throw new ArgumentNullException(nameof(address)); - } + sw.Start(); + result = operation(); + sw.Stop(); - if (address.Size != I2cAddressSize.SevenBit) + _logger.LogDebug("Executed [{ClassName}].[{MemberName}] in [{TotalMilliseconds}] ms", + className, + memberName, + sw.Elapsed.TotalMilliseconds); + } + catch (Exception ex) { - throw new SmBusInvalidAddressSizeException($"{nameof(I2cAddress)} size must be {I2cAddressSize.SevenBit}"); + _logger.LogError(ex, "An exception occurred executing [{ClassName}].[{MemberName}]", className, memberName); + + throw; } + + return result; } - private void AssertPec(IList result) + private void HandleScanError(I2cAddress address, Exception ex) { - if (result.Count > 1) - { - byte expected = Crc8.ComputeChecksum(result.Take(result.Count - 1).ToList()); + _logger.LogWarning(ex, "Device Address [0x{Address:X2}] did not respond", address.Value); - if (result[result.Count - 1] != expected) - { - throw new SmBusInvalidCrcException(expected, result[result.Count - 1], "PEC does not match"); - } - } + CancelI2cBusTransfer(); } - private IList I2cReadData(CommandCodes commandCode, I2cAddress address, ushort length) where T : IResponse, new() + private List I2cReadData(CommandCodes commandCode, I2cAddress address, ushort length) where T : IResponse, new() { - if (address == null) - { - throw new ArgumentNullException(nameof(address)); - } + ArgumentNullException.ThrowIfNull(address); return HandleOperationExecution( nameof(Device), @@ -687,7 +742,7 @@ private void AssertPec(IList result) throw new I2cOperationException(getResponse.ExecutionResult, $"{nameof(I2cReadData)} The read of i2c data failed with execution result code [0x{getResponse.ExecutionResult:x}]"); } - if (getResponse.Data.Count > 0) + if (getResponse.Data!.Count > 0) { result.AddRange(getResponse.Data); } @@ -738,101 +793,56 @@ private void AssertPec(IList result) }); } - private string GetFactorySerialNumber() + private IList SmBusReadCommand(I2cAddress address, byte command, ushort length, bool errorCheck) { return HandleOperationExecution( nameof(Device), () => { - if (String.IsNullOrWhiteSpace(_factorySerialNumber)) - { - var response = ExecuteCommand(new ReadFactorySerialNumberCommand()); - - _factorySerialNumber = response.SerialNumber; - } + List writeData = new List() { command }; - return _factorySerialNumber; - }); - } + I2cWriteData(CommandCodes.WriteI2cDataNoStop, address, writeData); - private void ExecuteCommand(ICommand command) - { - var memoryStream = new MemoryStream(new byte[MaxPacketSize], true); + var result = I2cReadData(CommandCodes.ReadI2cDataRepeatedStart, address, length); - command.Serialize(memoryStream); + if (errorCheck) + { + AssertPec(result); + } - _hidDevice.Write(memoryStream.ToArray()); + return result; + }); } - private T ExecuteCommand(ICommand command, bool checkResult = true) where T : IResponse, new() + private void SmBusWriteCommand(I2cAddress address, byte command, bool errorCheck, params byte[] data) { - var outStream = new MemoryStream(new byte[MaxPacketSize], true); - var inStream = new MemoryStream(); - - command.Serialize(outStream); - - inStream.Write(_hidDevice.WriteRead(outStream.ToArray())); - - var result = new T(); - - result.Deserialize(inStream); + HandleOperationExecution( + nameof(Device), + () => + { + List writeData = new List() { command }; + writeData.AddRange(data); - if (checkResult && result.ExecutionResult != 0) - { - throw new CommandExecutionFailedException($"Unexpected command execution status Expected: [0x00] Actual [0x{result.ExecutionResult:x}]"); - } + if (errorCheck) + { + writeData.Add(Crc8.ComputeChecksum(writeData)); + } - return result; + I2cWriteData(CommandCodes.WriteI2cData, address, writeData); + }); } - [DebuggerStepThrough] - private void HandleOperationExecution(string className, Action operation, [CallerMemberName] string memberName = "") - { - Stopwatch sw = new Stopwatch(); - - try - { - sw.Start(); - operation(); - sw.Stop(); - - _logger.LogDebug($"Executed [{className}].[{memberName}] in [{sw.Elapsed.TotalMilliseconds}] ms"); - } - catch (Exception ex) - { - _logger.LogError($"An exception occurred executing [{className}].[{memberName}] Reason: [{ex.Message}]"); + #region Dispose - throw; - } - } + private bool disposedValue; - [DebuggerStepThrough] - private T HandleOperationExecution(string className, Func operation, [CallerMemberName] string memberName = "") + public void Dispose() { - Stopwatch sw = new Stopwatch(); - T result; - - try - { - sw.Start(); - result = operation(); - sw.Stop(); - - _logger.LogDebug($"Executed [{className}].[{memberName}] in [{sw.Elapsed.TotalMilliseconds}] ms"); - } - catch (Exception ex) - { - _logger.LogError($"An exception occurred executing [{className}].[{memberName}] Reason: [{ex.Message}]"); - - throw; - } - - return result; + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); } - #region Dispose - private bool disposedValue; - protected virtual void Dispose(bool disposing) { _logger.LogDebug($"Disposing {nameof(Device)}"); @@ -848,13 +858,6 @@ protected virtual void Dispose(bool disposing) } } - public void Dispose() - { - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - Dispose(disposing: true); - GC.SuppressFinalize(this); - } - - #endregion + #endregion Dispose } -} +} \ No newline at end of file diff --git a/MCP2221IO/DeviceStatus.cs b/MCP2221IO/DeviceStatus.cs index 868db6b..0ad0c20 100644 --- a/MCP2221IO/DeviceStatus.cs +++ b/MCP2221IO/DeviceStatus.cs @@ -87,7 +87,7 @@ public class DeviceStatus public ushort I2cAddress { get; private set; } /// - /// + /// If ACK was received from client value is false, else true /// public bool AckStatus { get; private set; } @@ -114,17 +114,17 @@ public class DeviceStatus /// /// The Hardware revision /// - public string HardwareRevision { get; private set; } + public string? HardwareRevision { get; private set; } /// /// The Firmware revision /// - public string FirmwareRevision { get; private set; } + public string? FirmwareRevision { get; private set; } /// /// ADC values /// - public IReadOnlyList Adc { get; private set; } + public IReadOnlyList? Adc { get; private set; } public override string ToString() { @@ -149,7 +149,7 @@ public override string ToString() stringBuilder.AppendLine($"{nameof(HardwareRevision)}: {HardwareRevision}"); stringBuilder.AppendLine($"{nameof(FirmwareRevision)}: {FirmwareRevision}"); - stringBuilder.AppendLine($"{nameof(Adc)}:\r\n[0x{Adc[0]:X4}]\r\n[0x{Adc[1]:X4}]\r\n[0x{Adc[2]:X4}]"); + stringBuilder.AppendLine($"{nameof(Adc)}:\r\n[0x{Adc![0]:X4}]\r\n[0x{Adc[1]:X4}]\r\n[0x{Adc[2]:X4}]"); return stringBuilder.ToString(); } diff --git a/MCP2221IO/Exceptions/CommandExecutionFailedException.cs b/MCP2221IO/Exceptions/CommandExecutionFailedException.cs index 4b956dc..e7bd6fd 100644 --- a/MCP2221IO/Exceptions/CommandExecutionFailedException.cs +++ b/MCP2221IO/Exceptions/CommandExecutionFailedException.cs @@ -26,15 +26,10 @@ namespace MCP2221IO.Exceptions { - - [Serializable] public class CommandExecutionFailedException : Exception { public CommandExecutionFailedException() { } public CommandExecutionFailedException(string message) : base(message) { } public CommandExecutionFailedException(string message, Exception inner) : base(message, inner) { } - protected CommandExecutionFailedException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } } diff --git a/MCP2221IO/Exceptions/GPIONotEnabledException.cs b/MCP2221IO/Exceptions/GPIONotEnabledException.cs index a751f09..9ecbd57 100644 --- a/MCP2221IO/Exceptions/GPIONotEnabledException.cs +++ b/MCP2221IO/Exceptions/GPIONotEnabledException.cs @@ -26,23 +26,28 @@ namespace MCP2221IO.Exceptions { - - [Serializable] public class GpioNotEnabledException : Exception { public GpioNotEnabledException() { + Port = 0; + } + + public GpioNotEnabledException(int port) + { + Port = port; } - public GpioNotEnabledException(string message) : base(message) + + public GpioNotEnabledException(int port, string message) : base(message) { + Port = port; } - public GpioNotEnabledException(string message, Exception inner) : base(message, inner) + + public GpioNotEnabledException(int port, string message, Exception inner) : base(message, inner) { + Port = port; } - protected GpioNotEnabledException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } public int Port { get; private set; } } -} +} \ No newline at end of file diff --git a/MCP2221IO/Exceptions/I2cOperationException.cs b/MCP2221IO/Exceptions/I2cOperationException.cs index 80695f3..55e6fe0 100644 --- a/MCP2221IO/Exceptions/I2cOperationException.cs +++ b/MCP2221IO/Exceptions/I2cOperationException.cs @@ -27,19 +27,29 @@ namespace MCP2221IO.Exceptions { - - [Serializable] [SuppressMessage("Minor Code Smell", "S101:Types should be named in PascalCase", Justification = "")] public class I2cOperationException : Exception { - public I2cOperationException(byte executionResult) { ExecutionResult = executionResult; } - public I2cOperationException(string message) : this(0, message) { } - public I2cOperationException(byte executionResult, string message) : base(message) { ExecutionResult = executionResult; } - public I2cOperationException(byte executionResult, string message, Exception inner) : base(message, inner) { ExecutionResult = executionResult; } - protected I2cOperationException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + public I2cOperationException(byte executionResult) + { + ExecutionResult = executionResult; + } + + public I2cOperationException(string message) : base(message) + { + ExecutionResult = 0; + } + + public I2cOperationException(byte executionResult, string message) : base(message) + { + ExecutionResult = executionResult; + } + + public I2cOperationException(byte executionResult, string message, Exception inner) : base(message, inner) + { + ExecutionResult = executionResult; + } public byte ExecutionResult { get; private set; } } -} +} \ No newline at end of file diff --git a/MCP2221IO/Exceptions/InvalidResponseTypeException.cs b/MCP2221IO/Exceptions/InvalidResponseTypeException.cs index 65fc81b..421d005 100644 --- a/MCP2221IO/Exceptions/InvalidResponseTypeException.cs +++ b/MCP2221IO/Exceptions/InvalidResponseTypeException.cs @@ -26,14 +26,10 @@ namespace MCP2221IO.Exceptions { - [Serializable] public class InvalidResponseTypeException : Exception { public InvalidResponseTypeException() { } public InvalidResponseTypeException(string message) : base(message) { } - public InvalidResponseTypeException(string message, System.Exception inner) : base(message, inner) { } - protected InvalidResponseTypeException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + public InvalidResponseTypeException(string message, Exception inner) : base(message, inner) { } } } diff --git a/MCP2221IO/Exceptions/InvalidStreamLengthException.cs b/MCP2221IO/Exceptions/InvalidStreamLengthException.cs index d0dad42..b493dc8 100644 --- a/MCP2221IO/Exceptions/InvalidStreamLengthException.cs +++ b/MCP2221IO/Exceptions/InvalidStreamLengthException.cs @@ -26,15 +26,10 @@ namespace MCP2221IO.Exceptions { - - [System.Serializable] public class InvalidStreamLengthException : Exception { public InvalidStreamLengthException() { } public InvalidStreamLengthException(string message) : base(message) { } public InvalidStreamLengthException(string message, Exception inner) : base(message, inner) { } - protected InvalidStreamLengthException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } } diff --git a/MCP2221IO/Exceptions/ReadRequiredException.cs b/MCP2221IO/Exceptions/ReadRequiredException.cs index 1d6dbae..1159beb 100644 --- a/MCP2221IO/Exceptions/ReadRequiredException.cs +++ b/MCP2221IO/Exceptions/ReadRequiredException.cs @@ -26,15 +26,10 @@ namespace MCP2221IO.Exceptions { - - [Serializable] public class ReadRequiredException : Exception { public ReadRequiredException() { } public ReadRequiredException(string message) : base(message) { } public ReadRequiredException(string message, Exception inner) : base(message, inner) { } - protected ReadRequiredException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } } diff --git a/MCP2221IO/Exceptions/SmBusInvalidAddressSizeException.cs b/MCP2221IO/Exceptions/SmBusInvalidAddressSizeException.cs index e4fdd47..6e763cf 100644 --- a/MCP2221IO/Exceptions/SmBusInvalidAddressSizeException.cs +++ b/MCP2221IO/Exceptions/SmBusInvalidAddressSizeException.cs @@ -26,15 +26,10 @@ namespace MCP2221IO.Exceptions { - - [Serializable] public class SmBusInvalidAddressSizeException : Exception { public SmBusInvalidAddressSizeException() { } public SmBusInvalidAddressSizeException(string message) : base(message) { } public SmBusInvalidAddressSizeException(string message, Exception inner) : base(message, inner) { } - protected SmBusInvalidAddressSizeException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } } diff --git a/MCP2221IO/Exceptions/SmBusInvalidCrcException.cs b/MCP2221IO/Exceptions/SmBusInvalidCrcException.cs index 861a292..cfd12bd 100644 --- a/MCP2221IO/Exceptions/SmBusInvalidCrcException.cs +++ b/MCP2221IO/Exceptions/SmBusInvalidCrcException.cs @@ -26,24 +26,28 @@ namespace MCP2221IO.Exceptions { - - [Serializable] - public class SmBusInvalidCrcException : System.Exception + public class SmBusInvalidCrcException : Exception { - public SmBusInvalidCrcException(byte expected, byte actual) : this(expected, actual, string.Empty) { } - public SmBusInvalidCrcException(byte expected, byte actual, string message) : this(expected, actual, message, null) { } - public SmBusInvalidCrcException(byte expected, byte actual, string message, System.Exception inner) : base(message, inner) + public SmBusInvalidCrcException(byte expected, byte actual) + { + Expected = expected; + Actual = actual; + } + + public SmBusInvalidCrcException(byte expected, byte actual, string message) : base(message) { Expected = expected; Actual = actual; } - protected SmBusInvalidCrcException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + public SmBusInvalidCrcException(byte expected, byte actual, string message, Exception inner) : base(message, inner) + { + Expected = expected; + Actual = actual; + } public byte Expected { get; private set; } public byte Actual { get; private set; } } -} +} \ No newline at end of file diff --git a/MCP2221IO/GlobalSuppressions.cs b/MCP2221IO/GlobalSuppressions.cs new file mode 100644 index 0000000..24ebc5f --- /dev/null +++ b/MCP2221IO/GlobalSuppressions.cs @@ -0,0 +1,50 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IO")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IO.Usb")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IO.Gpio")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IO.Commands")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IO.Exceptions")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IO.Responses")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IO.Settings")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Device.SramSettings")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Device.GpioPort0")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Device.GpioPort1")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Device.GpioPort2")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Device.GpioPort3")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Device.UsbManufacturerDescriptor")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Device.UsbProductDescriptor")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Device.UsbSerialNumberDescriptor")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Device.ReadSramSettings")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Device.ReadGpioPorts")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Device.WriteSramSettings(System.Boolean)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Device.WriteGpioPorts")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.DeviceStatus.AckStatus")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.DeviceStatus.SdaLineState")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.DeviceStatus.SclLineState")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.StatusSetParametersResponse.Deserialize(System.IO.Stream)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadChipSettingsResponse.Deserialize(System.IO.Stream)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Exceptions.SmBusInvalidCrcException")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Exceptions.GpioNotEnabledException")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Gpio.GpioPort")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Settings.SramSettings")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.GetI2cDataResponse.Deserialize(System.IO.Stream)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadGpioPortsResponse.#ctor")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadGpSettingsResponse.Deserialize(System.IO.Stream)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Responses.ReadGpioPortsResponse.GpioPort0")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Responses.ReadGpioPortsResponse.GpioPort1")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Responses.ReadGpioPortsResponse.GpioPort2")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Responses.ReadGpioPortsResponse.GpioPort3")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadGpioPortsResponse.Deserialize(System.IO.Stream)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Commands.WriteSramSettingsCommand.#ctor(MCP2221IO.Settings.SramSettings,System.Boolean)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Commands.WriteSramSettingsCommand.SramSettings")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.FactorySerialNumberResponse.Deserialize(System.IO.Stream)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadSramSettingsResponse.#ctor")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Responses.ReadSramSettingsResponse.SramSettings")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadSramSettingsResponse.Deserialize(System.IO.Stream)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadFlashStringResponse.Deserialize(System.IO.Stream)")] diff --git a/MCP2221IO/Gpio/GpioPort.cs b/MCP2221IO/Gpio/GpioPort.cs index 8a1d3d9..0e70c17 100644 --- a/MCP2221IO/Gpio/GpioPort.cs +++ b/MCP2221IO/Gpio/GpioPort.cs @@ -40,10 +40,15 @@ public class GpioPort /// public bool Enabled { get; private set; } + /// + /// The GPIO port direction + /// + public bool? IsInput { get; set; } + /// /// The GPIO port value /// - public bool? Value + public bool? Value { get => _value; set @@ -59,11 +64,6 @@ public bool? Value } } - /// - /// The GPIO port direction - /// - public bool? IsInput { get; set; } - public override string ToString() { StringBuilder stringBuilder = new StringBuilder(); @@ -76,6 +76,15 @@ public override string ToString() return stringBuilder.ToString(); } + internal static void Write(Stream stream, bool? value) + { + if (value.HasValue) + { + stream.WriteByte(0xFF); + stream.WriteByte((byte)(value.Value ? 0x01 : 0x00)); + } + } + internal void Deserialize(Stream stream) { int value = stream.ReadByte(); @@ -97,7 +106,7 @@ internal void Deserialize(Stream stream) internal void Serialize(Stream stream) { - if(Enabled) + if (Enabled) { Write(stream, Value); Write(stream, IsInput); @@ -107,14 +116,5 @@ internal void Serialize(Stream stream) stream.Write(new byte[] { 0x00, 0x00, 0x00, 0x00 }); } } - - internal void Write(Stream stream, bool? value) - { - if(value.HasValue) - { - stream.WriteByte(0xFF); - stream.WriteByte((byte)(value.Value ? 0x01 : 0x00)); - } - } } -} +} \ No newline at end of file diff --git a/MCP2221IO/I2CAddress.cs b/MCP2221IO/I2CAddress.cs index da2fa02..1ceb5f8 100644 --- a/MCP2221IO/I2CAddress.cs +++ b/MCP2221IO/I2CAddress.cs @@ -66,9 +66,9 @@ public I2cAddress(uint address, I2cAddressSize size) public uint Value { get; private set; } - public IReadOnlyCollection ReadAddress { get; private set; } + public IReadOnlyCollection? ReadAddress { get; private set; } - public IReadOnlyCollection WriteAddress { get; private set; } + public IReadOnlyCollection? WriteAddress { get; private set; } public I2cAddressSize Size { get; } diff --git a/MCP2221IO/IDevice.cs b/MCP2221IO/IDevice.cs index 2221f1a..9651454 100644 --- a/MCP2221IO/IDevice.cs +++ b/MCP2221IO/IDevice.cs @@ -54,42 +54,42 @@ public interface IDevice : IDisposable /// /// Get the device /// - DeviceStatus Status { get; } + DeviceStatus? Status { get; } /// /// Get the device /// - ChipSettings ChipSettings { get; } + ChipSettings? ChipSettings { get; } /// /// The Flash /// - GpSettings GpSettings { get; } + GpSettings? GpSettings { get; } /// /// The /// - SramSettings SramSettings { get; } + SramSettings? SramSettings { get; } /// /// Gpio 0 port settings /// - GpioPort GpioPort0 { get; } + GpioPort? GpioPort0 { get; } /// /// Gpio 1 port settings /// - GpioPort GpioPort1 { get; } + GpioPort? GpioPort1 { get; } /// /// Gpio 2 port settings /// - GpioPort GpioPort2 { get; } + GpioPort? GpioPort2 { get; } /// /// Gpio 3 port settings /// - GpioPort GpioPort3 { get; } + GpioPort? GpioPort3 { get; } /// /// The USB manufacturer descriptor value @@ -109,7 +109,7 @@ public interface IDevice : IDisposable /// /// The factory serial number as a hex string /// - string FactorySerialNumber { get; } + string? FactorySerialNumber { get; } /// /// Unlock the flash @@ -244,17 +244,17 @@ public interface IDevice : IDisposable /// Read a byte from the bus /// /// The - /// Read data with packet error check + /// Perform packet error check on response /// The byte read from the bus - byte SmBusReadByte(I2cAddress address, bool pec = false); + byte SmBusReadByte(I2cAddress address, bool errorCheck = false); /// /// Write a byte to the bus /// /// The /// The data to write - /// Send data with packet error check - void SmBusWriteByte(I2cAddress address, byte data, bool pec = false); + /// Perform packet error check on response + void SmBusWriteByte(I2cAddress address, byte data, bool errorCheck = false); /// /// Read a byte from the bus with a command code @@ -263,7 +263,7 @@ public interface IDevice : IDisposable /// The command /// Read data with packet error check /// The byte read from the bus - byte SmBusReadByteCommand(I2cAddress address, byte command, bool pec = false); + byte SmBusReadByteCommand(I2cAddress address, byte command, bool errorCheck = false); /// /// Write a byte to the bus with a command code @@ -271,8 +271,8 @@ public interface IDevice : IDisposable /// The /// The command /// The data - /// Send data with packet error check - void SmBusWriteByteCommand(I2cAddress address, byte command, byte data, bool pec = false); + /// Perform packet error check on response + void SmBusWriteByteCommand(I2cAddress address, byte command, byte data, bool errorCheck = false); /// /// Read a from the bus with a command code @@ -281,7 +281,7 @@ public interface IDevice : IDisposable /// The command /// Read data with packet error check /// The short read from the bus - short SmBusReadShortCommand(I2cAddress address, byte command, bool pec = false); + short SmBusReadShortCommand(I2cAddress address, byte command, bool errorCheck = false); /// /// Write a to the bus with a command code @@ -289,8 +289,8 @@ public interface IDevice : IDisposable /// The /// The command /// The data - /// Send data with packet error check - void SmBusWriteShortCommand(I2cAddress address, byte command, short data, bool pec = false); + /// Perform packet error check on response + void SmBusWriteShortCommand(I2cAddress address, byte command, short data, bool errorCheck = false); /// /// Read a from the bus with a command code @@ -299,7 +299,7 @@ public interface IDevice : IDisposable /// The command /// Read data with packet error check /// Returns an read from the bus - int SmBusReadIntCommand(I2cAddress address, byte command, bool pec = false); + int SmBusReadIntCommand(I2cAddress address, byte command, bool errorCheck = false); /// /// Write a to the bus with a command code @@ -307,8 +307,8 @@ public interface IDevice : IDisposable /// The /// The command /// The data - /// Send data with packet error check - void SmBusWriteIntCommand(I2cAddress address, byte command, int data, bool pec = false); + /// Perform packet error check on response + void SmBusWriteIntCommand(I2cAddress address, byte command, int data, bool errorCheck = false); /// /// Read a from the bus with a command code @@ -317,7 +317,7 @@ public interface IDevice : IDisposable /// The command /// Read data with packet error check /// The long int value read from the bus - long SmBusReadLongCommand(I2cAddress address, byte command, bool pec = false); + long SmBusReadLongCommand(I2cAddress address, byte command, bool errorCheck = false); /// /// Write a to the bus with a command code @@ -325,8 +325,8 @@ public interface IDevice : IDisposable /// The /// The command /// The data - /// Send data with packet error check - void SmBusWriteLongCommand(I2cAddress address, byte command, long data, bool pec = false); + /// Perform packet error check on response + void SmBusWriteLongCommand(I2cAddress address, byte command, long data, bool errorCheck = false); /// /// Read a block of bytes from the bus with a command code @@ -336,7 +336,7 @@ public interface IDevice : IDisposable /// The number of bytes to read from the bus /// Read data with packet error check /// The of read from the bus - IList SmBusBlockRead(I2cAddress address, byte command, byte count, bool pec = false); + IList SmBusBlockRead(I2cAddress address, byte command, byte count, bool errorCheck = false); /// /// Write a block of bytes to the bus with a command code @@ -344,7 +344,7 @@ public interface IDevice : IDisposable /// The /// The command /// The of - /// Send data with packet error check - void SmBusBlockWrite(I2cAddress address, byte command, IList block, bool pec = false); + /// Perform packet error check on response + void SmBusBlockWrite(I2cAddress address, byte command, IList block, bool errorCheck = false); } } diff --git a/MCP2221IO/Responses/FactorySerialNumberResponse.cs b/MCP2221IO/Responses/FactorySerialNumberResponse.cs index 29db2e6..04bc029 100644 --- a/MCP2221IO/Responses/FactorySerialNumberResponse.cs +++ b/MCP2221IO/Responses/FactorySerialNumberResponse.cs @@ -32,6 +32,7 @@ internal class FactorySerialNumberResponse : BaseResponse { public FactorySerialNumberResponse() : base(CommandCodes.ReadFlashData) { + SerialNumber = string.Empty; } public string SerialNumber { get; set; } diff --git a/MCP2221IO/Responses/GetI2CDataResponse.cs b/MCP2221IO/Responses/GetI2CDataResponse.cs index 4bb17d3..9c6911f 100644 --- a/MCP2221IO/Responses/GetI2CDataResponse.cs +++ b/MCP2221IO/Responses/GetI2CDataResponse.cs @@ -40,7 +40,7 @@ public GetI2cDataResponse() : base(Commands.CommandCodes.GetI2cData) { } - public IList Data { get; private set; } + public IList? Data { get; private set; } public override void Deserialize(Stream stream) { diff --git a/MCP2221IO/Responses/ReadChipSettingsResponse.cs b/MCP2221IO/Responses/ReadChipSettingsResponse.cs index 9ca65fc..9c348b5 100644 --- a/MCP2221IO/Responses/ReadChipSettingsResponse.cs +++ b/MCP2221IO/Responses/ReadChipSettingsResponse.cs @@ -37,7 +37,7 @@ public ReadChipSettingsResponse() : base(CommandCodes.ReadFlashData) { } - public ChipSettings ChipSettings { get; private set; } + public ChipSettings? ChipSettings { get; private set; } public override void Deserialize(Stream stream) { diff --git a/MCP2221IO/Responses/ReadFlashStringResponse.cs b/MCP2221IO/Responses/ReadFlashStringResponse.cs index 7ef641d..c2e8deb 100644 --- a/MCP2221IO/Responses/ReadFlashStringResponse.cs +++ b/MCP2221IO/Responses/ReadFlashStringResponse.cs @@ -32,6 +32,7 @@ internal class ReadFlashStringResponse : BaseResponse { public ReadFlashStringResponse() : base(CommandCodes.ReadFlashData) { + Value = string.Empty; } public string Value { get; private set; } diff --git a/MCP2221IO/Responses/ReadGpSettingsResponse.cs b/MCP2221IO/Responses/ReadGpSettingsResponse.cs index 48d6b0c..2dd41ab 100644 --- a/MCP2221IO/Responses/ReadGpSettingsResponse.cs +++ b/MCP2221IO/Responses/ReadGpSettingsResponse.cs @@ -40,7 +40,7 @@ public ReadGpSettingsResponse() : base(CommandCodes.ReadFlashData) /// /// The /// - public GpSettings GpSettings { get; set; } + public GpSettings? GpSettings { get; set; } public override void Deserialize(Stream stream) { diff --git a/MCP2221IO/Responses/ReadGpioPortsResponse.cs b/MCP2221IO/Responses/ReadGpioPortsResponse.cs index 2473ffb..d66937f 100644 --- a/MCP2221IO/Responses/ReadGpioPortsResponse.cs +++ b/MCP2221IO/Responses/ReadGpioPortsResponse.cs @@ -34,13 +34,13 @@ public ReadGpioPortsResponse() : base(CommandCodes.GetGpioValues) { } - public GpioPort GpioPort0 { get; private set; } + public GpioPort? GpioPort0 { get; private set; } - public GpioPort GpioPort1 { get; private set; } + public GpioPort? GpioPort1 { get; private set; } - public GpioPort GpioPort2 { get; private set; } + public GpioPort? GpioPort2 { get; private set; } - public GpioPort GpioPort3 { get; private set; } + public GpioPort? GpioPort3 { get; private set; } public override void Deserialize(Stream stream) { diff --git a/MCP2221IO/Responses/ReadSramSettingsResponse.cs b/MCP2221IO/Responses/ReadSramSettingsResponse.cs index 48d2d5b..e8569ed 100644 --- a/MCP2221IO/Responses/ReadSramSettingsResponse.cs +++ b/MCP2221IO/Responses/ReadSramSettingsResponse.cs @@ -34,7 +34,7 @@ public ReadSramSettingsResponse() : base(CommandCodes.GetSram) { } - public SramSettings SramSettings { get; private set; } + public SramSettings? SramSettings { get; private set; } public override void Deserialize(Stream stream) { diff --git a/MCP2221IO/Responses/StatusSetParametersResponse.cs b/MCP2221IO/Responses/StatusSetParametersResponse.cs index 322e0ba..281bb18 100644 --- a/MCP2221IO/Responses/StatusSetParametersResponse.cs +++ b/MCP2221IO/Responses/StatusSetParametersResponse.cs @@ -39,7 +39,7 @@ public StatusSetParametersResponse() : base(CommandCodes.StatusSetParameters) /// /// The /// - public DeviceStatus DeviceStatus { get; private set; } + public DeviceStatus? DeviceStatus { get; private set; } public override void Deserialize(Stream stream) { diff --git a/MCP2221IO/Settings/BaseGpPowerSettings.cs b/MCP2221IO/Settings/BaseGpPowerSettings.cs index 154ac7f..5bb9d58 100644 --- a/MCP2221IO/Settings/BaseGpPowerSettings.cs +++ b/MCP2221IO/Settings/BaseGpPowerSettings.cs @@ -32,7 +32,7 @@ namespace MCP2221IO.Settings /// A Gpio setting type /// /// - public class GpSetting where T : System.Enum + public class GpSetting where T : Enum { /// /// The current output value on the Gpio port @@ -47,7 +47,7 @@ public class GpSetting where T : System.Enum /// /// The current Gp settings port designation /// - public T Designation { get; set; } + public T? Designation { get; set; } public override string ToString() { @@ -75,7 +75,7 @@ internal void Serialize(Stream stream) { int update = Value ? 0x10 : 0x00; update |= IsInput ? 0x08 : 0x00; - update |= (int)(object)Designation & 0b111; + update |= (int)(object)Designation! & 0b111; stream.WriteByte((byte)update); } diff --git a/MCP2221IO/Settings/GpSettings.cs b/MCP2221IO/Settings/GpSettings.cs index 1650ce4..6ef0fc0 100644 --- a/MCP2221IO/Settings/GpSettings.cs +++ b/MCP2221IO/Settings/GpSettings.cs @@ -33,13 +33,13 @@ namespace MCP2221IO.Settings /// public class GpSettings { - public GpSetting Gp0PowerUpSetting { get; internal set; } + public GpSetting? Gp0PowerUpSetting { get; internal set; } - public GpSetting Gp1PowerUpSetting { get; internal set; } + public GpSetting? Gp1PowerUpSetting { get; internal set; } - public GpSetting Gp2PowerUpSetting { get; internal set; } + public GpSetting? Gp2PowerUpSetting { get; internal set; } - public GpSetting Gp3PowerUpSetting { get; internal set; } + public GpSetting? Gp3PowerUpSetting { get; internal set; } public override string ToString() { @@ -68,10 +68,10 @@ internal void Deserialize(Stream stream) internal void Serialize(Stream stream) { - Gp0PowerUpSetting.Serialize(stream); - Gp1PowerUpSetting.Serialize(stream); - Gp2PowerUpSetting.Serialize(stream); - Gp3PowerUpSetting.Serialize(stream); + Gp0PowerUpSetting!.Serialize(stream); + Gp1PowerUpSetting!.Serialize(stream); + Gp2PowerUpSetting!.Serialize(stream); + Gp3PowerUpSetting!.Serialize(stream); } } } diff --git a/MCP2221IO/Settings/Password.cs b/MCP2221IO/Settings/Password.cs index 6df4822..43bf09f 100644 --- a/MCP2221IO/Settings/Password.cs +++ b/MCP2221IO/Settings/Password.cs @@ -41,10 +41,7 @@ public Password(string value) public Password(IList bytes) { - if (bytes is null) - { - throw new ArgumentNullException(nameof(bytes)); - } + ArgumentNullException.ThrowIfNull(bytes); if (bytes.Count > 8) { @@ -55,13 +52,13 @@ public Password(IList bytes) Value = BitConverter.ToString(bytes.ToArray()).Replace("-", ""); } - public string Value { get; private set; } + public string? Value { get; private set; } - public IReadOnlyList Bytes { get; private set; } + public IReadOnlyList? Bytes { get; private set; } public override string ToString() { - return Value; + return Value!; } /// diff --git a/MCP2221IO/Settings/SramSettings.cs b/MCP2221IO/Settings/SramSettings.cs index 0e6e06f..9d89a7d 100644 --- a/MCP2221IO/Settings/SramSettings.cs +++ b/MCP2221IO/Settings/SramSettings.cs @@ -64,27 +64,27 @@ public class SramSettings : BaseSettings /// /// The current password expressed as an 8 byte hex number /// - public Password Password { get; protected set; } + public Password? Password { get; protected set; } /// /// The GP0 settings /// - public GpSetting Gp0Settings { get; internal set; } + public GpSetting? Gp0Settings { get; internal set; } /// /// The GP1 settings /// - public GpSetting Gp1Settings { get; internal set; } + public GpSetting? Gp1Settings { get; internal set; } /// /// The GP2 settings /// - public GpSetting Gp2Settings { get; internal set; } + public GpSetting? Gp2Settings { get; internal set; } /// /// The GP3 settings /// - public GpSetting Gp3Settings { get; internal set; } + public GpSetting? Gp3Settings { get; internal set; } public override string ToString() { diff --git a/MCP2221IO/Usb/HidSharpHidDevice.cs b/MCP2221IO/Usb/HidSharpHidDevice.cs index 2e0d4ac..f04ee53 100644 --- a/MCP2221IO/Usb/HidSharpHidDevice.cs +++ b/MCP2221IO/Usb/HidSharpHidDevice.cs @@ -31,11 +31,11 @@ namespace MCP2221IO.Usb public class HidSharpHidDevice : IHidDevice { private readonly ILogger _logger; - private HidDevice _hidDevice; - private HidStream _hidStream; + private HidDevice? _hidDevice; + private HidStream? _hidStream; private bool disposedValue; - public HidSharpHidDevice(ILogger logger, HidDevice hidDevice) + public HidSharpHidDevice(ILogger logger, HidDevice hidDevice) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _hidDevice = hidDevice ?? throw new ArgumentNullException(nameof(hidDevice)); @@ -43,23 +43,23 @@ public HidSharpHidDevice(ILogger logger, HidDevice hidDevice) public void Open() { - _hidStream = _hidDevice.Open(); + _hidStream = _hidDevice!.Open(); } public void Write(byte[] outBytes) { - _hidStream.Write(outBytes); + _hidStream!.Write(outBytes); } public byte[] WriteRead(byte[] outBytes) { - _logger.LogDebug($"Output HID Packet: [{BitConverter.ToString(outBytes).Replace("-", ",0x")}]"); + _logger.LogDebug("Output HID Packet: [{Packet}]", BitConverter.ToString(outBytes).Replace("-", ",0x")); - _hidStream.Write(outBytes); + _hidStream!.Write(outBytes); var inBytes = _hidStream.Read(); - _logger.LogDebug($"Input HID Packet: [{BitConverter.ToString(inBytes).Replace("-", ",0x")}]"); + _logger.LogDebug("Input HID Packet: [{Packet}]", BitConverter.ToString(inBytes).Replace("-", ",0x")); return inBytes; } diff --git a/MCP2221IOConsole/Commands/BaseCommand.cs b/MCP2221IOConsole/Commands/BaseCommand.cs index c599ae8..dcca3f3 100644 --- a/MCP2221IOConsole/Commands/BaseCommand.cs +++ b/MCP2221IOConsole/Commands/BaseCommand.cs @@ -47,7 +47,7 @@ protected BaseCommand(IServiceProvider serviceProvider) public int Pid { get; set; } = 0x00DD; [Option(Templates.SerialNumber, "The MCP2221 instance serial number", CommandOptionType.SingleValue)] - public string Serial { get; set; } + public string Serial { get; set; } = string.Empty; protected int ExecuteCommand(Func action) { @@ -59,8 +59,8 @@ protected int ExecuteCommand(Func action) if (hidDevice != null) { - using HidSharpHidDevice hidSharpHidDevice = new HidSharpHidDevice((ILogger)_serviceProvider.GetService(typeof(ILogger)), hidDevice); - using MCP2221IO.Device device = new MCP2221IO.Device((ILogger)_serviceProvider.GetService(typeof(ILogger)), hidSharpHidDevice); + using HidSharpHidDevice hidSharpHidDevice = new HidSharpHidDevice((ILogger)_serviceProvider.GetService(typeof(ILogger)), hidDevice); + using MCP2221IO.Device device = new MCP2221IO.Device((ILogger)_serviceProvider.GetService(typeof(ILogger)), hidSharpHidDevice); device.Open(); @@ -79,10 +79,10 @@ protected int ExecuteCommand(Func action) return result; } - protected virtual int OnExecute(CommandLineApplication app, IConsole console) + protected virtual int OnExecute(CommandLineApplication application, IConsole console) { console.Error.WriteLine("You must specify a command. See --help for more details."); - app.ShowHelp(); + application.ShowHelp(); return 0; } diff --git a/MCP2221IOConsole/Commands/Flash/BaseWriteGpSetingsCommand.cs b/MCP2221IOConsole/Commands/Flash/BaseWriteGpSetingsCommand.cs index cce607a..2e5f252 100644 --- a/MCP2221IOConsole/Commands/Flash/BaseWriteGpSetingsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/BaseWriteGpSetingsCommand.cs @@ -41,12 +41,12 @@ protected BaseWriteGpSettingsCommand(IServiceProvider serviceProvider) : base(se [Option(Templates.GpValue, "The GP output value at power up when pin is set as output", CommandOptionType.SingleValue)] public (bool HasValue, bool Value) Value { get; set; } - internal void UpdateGpSetting(CommandLineApplication app, IConsole console, IDevice device, GpSetting gpio, (bool HasValue, T Value) designation) where T : Enum + internal void UpdateGpSetting(CommandLineApplication application, IConsole console, IDevice device, GpSetting gpio, (bool HasValue, T Value) designation) where T : Enum { if (!(IsInput.HasValue || Value.HasValue || designation.HasValue)) { console.Error.WriteLine("No update values specified"); - app.ShowHelp(); + application.ShowHelp(); } else { diff --git a/MCP2221IOConsole/Commands/Flash/ReadChipSettingsCommand.cs b/MCP2221IOConsole/Commands/Flash/ReadChipSettingsCommand.cs index d3dc2ce..6f64e0b 100644 --- a/MCP2221IOConsole/Commands/Flash/ReadChipSettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/ReadChipSettingsCommand.cs @@ -34,13 +34,13 @@ public ReadChipSettingsCommand(IServiceProvider serviceProvider) : base(serviceP { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadChipSettings(); - console.WriteLine(device.ChipSettings); + console.WriteLine(device.ChipSettings!); return 0; }); diff --git a/MCP2221IOConsole/Commands/Flash/ReadGpSettingsCommand.cs b/MCP2221IOConsole/Commands/Flash/ReadGpSettingsCommand.cs index 3c4a098..9d4619c 100644 --- a/MCP2221IOConsole/Commands/Flash/ReadGpSettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/ReadGpSettingsCommand.cs @@ -34,13 +34,13 @@ public ReadGpSettingsCommand(IServiceProvider serviceProvider) : base(servicePro { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadGpSettings(); - console.WriteLine(device.GpSettings); + console.WriteLine(device.GpSettings!); return 0; }); diff --git a/MCP2221IOConsole/Commands/Flash/ReadUsbDescriptorsCommand.cs b/MCP2221IOConsole/Commands/Flash/ReadUsbDescriptorsCommand.cs index e5e6f6b..21b832d 100644 --- a/MCP2221IOConsole/Commands/Flash/ReadUsbDescriptorsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/ReadUsbDescriptorsCommand.cs @@ -34,7 +34,7 @@ public ReadUsbDescriptorsCommand(IServiceProvider serviceProvider) : base(servic { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/Flash/WriteChipSettingsCommand.cs b/MCP2221IOConsole/Commands/Flash/WriteChipSettingsCommand.cs index 73fd078..26af907 100644 --- a/MCP2221IOConsole/Commands/Flash/WriteChipSettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/WriteChipSettingsCommand.cs @@ -89,7 +89,7 @@ public WriteChipSettingsCommand(IServiceProvider serviceProvider) : base(service [Option(Templates.Password, "The 8-byte password for Flash modifications protection", CommandOptionType.SingleValue)] public (bool HasValue, string Value) Password { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { @@ -97,97 +97,97 @@ protected override int OnExecute(CommandLineApplication app, IConsole console) device.ReadChipSettings(); - if (AdcRef.HasValue) + if (AdcRef.HasValue && device.ChipSettings != null) { device.ChipSettings.AdcRefOption = AdcRef.Value; modified = true; } - if (AdcVrmRef.HasValue) + if (AdcVrmRef.HasValue && device.ChipSettings != null) { device.ChipSettings.AdcRefVrm = AdcVrmRef.Value; modified |= true; } - if (CdcSerialNumberEnable.HasValue) + if (CdcSerialNumberEnable.HasValue && device.ChipSettings != null) { device.ChipSettings.CdcSerialNumberEnable = CdcSerialNumberEnable.Value; modified |= true; } - if (ChipSecurity.HasValue) + if (ChipSecurity.HasValue && device.ChipSettings != null) { device.ChipSettings.ChipSecurity = ChipSecurity.Value; modified |= true; } - if (ClockDivider.HasValue) + if (ClockDivider.HasValue && device.ChipSettings != null) { device.ChipSettings.ClockDivider = ClockDivider.Value; modified |= true; } - if (DutyCycle.HasValue) + if (DutyCycle.HasValue && device.ChipSettings != null) { device.ChipSettings.ClockDutyCycle = DutyCycle.Value; modified |= true; } - if (DacOutput.HasValue) + if (DacOutput.HasValue && device.ChipSettings != null) { device.ChipSettings.DacOutput = DacOutput.Value; modified |= true; } - if (DacRef.HasValue) + if (DacRef.HasValue && device.ChipSettings != null) { device.ChipSettings.DacRefOption = DacRef.Value; modified |= true; } - if (DacRefVrm.HasValue) + if (DacRefVrm.HasValue && device.ChipSettings != null) { device.ChipSettings.DacRefVrm = DacRefVrm.Value; modified |= true; } - if (InterruptNegativeEdge.HasValue) + if (InterruptNegativeEdge.HasValue && device.ChipSettings != null) { device.ChipSettings.InterruptNegativeEdge = InterruptNegativeEdge.Value; modified |= true; } - if (InterruptPositiveEdge.HasValue) + if (InterruptPositiveEdge.HasValue && device.ChipSettings != null) { device.ChipSettings.InterruptPositiveEdge = InterruptPositiveEdge.Value; modified |= true; } - if (PowerRequestMa.HasValue) + if (PowerRequestMa.HasValue && device.ChipSettings != null) { device.ChipSettings.PowerRequestMa = PowerRequestMa.Value; modified |= true; } - if (NewPid.HasValue) + if (NewPid.HasValue && device.ChipSettings != null) { device.ChipSettings.Pid = NewPid.Value; modified |= true; } - if (NewVid.HasValue) + if (NewVid.HasValue && device.ChipSettings != null) { device.ChipSettings.Vid = NewVid.Value; modified |= true; } - if (RemoteWake.HasValue) + if (RemoteWake.HasValue && device.ChipSettings != null) { device.ChipSettings.RemoteWake = RemoteWake.Value; modified |= true; } - if (SelfPowered.HasValue) + if (SelfPowered.HasValue && device.ChipSettings != null) { device.ChipSettings.SelfPowered = SelfPowered.Value; modified |= true; @@ -195,7 +195,6 @@ protected override int OnExecute(CommandLineApplication app, IConsole console) if (modified) { - Password password = MCP2221IO.Settings.Password.DefaultPassword; if(Password.HasValue) @@ -210,7 +209,7 @@ protected override int OnExecute(CommandLineApplication app, IConsole console) else { console.Error.WriteLine("No update values specified"); - app.ShowHelp(); + application.ShowHelp(); } return 0; diff --git a/MCP2221IOConsole/Commands/Flash/WriteGp0SettingsCommand.cs b/MCP2221IOConsole/Commands/Flash/WriteGp0SettingsCommand.cs index a606795..113bd3c 100644 --- a/MCP2221IOConsole/Commands/Flash/WriteGp0SettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/WriteGp0SettingsCommand.cs @@ -38,13 +38,13 @@ public WriteGp0SettingsCommand(IServiceProvider serviceProvider) : base(serviceP [Option(Templates.GpDesignation, "The GP0 power up designation", CommandOptionType.SingleValue)] public (bool HasValue, Gp0Designation Value) Designation { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadGpSettings(); - UpdateGpSetting(app, console, device, device.GpSettings.Gp0PowerUpSetting, Designation); + UpdateGpSetting(application, console, device, device.GpSettings!.Gp0PowerUpSetting!, Designation); return 0; }); diff --git a/MCP2221IOConsole/Commands/Flash/WriteGp1SettingsCommand.cs b/MCP2221IOConsole/Commands/Flash/WriteGp1SettingsCommand.cs index d4b0893..bd037e4 100644 --- a/MCP2221IOConsole/Commands/Flash/WriteGp1SettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/WriteGp1SettingsCommand.cs @@ -38,13 +38,13 @@ public WriteGp1SettingsCommand(IServiceProvider serviceProvider) : base(serviceP [Option(Templates.GpDesignation, "The GP1 power up designation", CommandOptionType.SingleValue)] public (bool HasValue, Gp1Designation Value) Designation { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadGpSettings(); - UpdateGpSetting(app, console, device, device.GpSettings.Gp1PowerUpSetting, Designation); + UpdateGpSetting(application, console, device, device.GpSettings!.Gp1PowerUpSetting!, Designation); return 0; }); diff --git a/MCP2221IOConsole/Commands/Flash/WriteGp2SettingsCommand.cs b/MCP2221IOConsole/Commands/Flash/WriteGp2SettingsCommand.cs index e7e75c5..9987196 100644 --- a/MCP2221IOConsole/Commands/Flash/WriteGp2SettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/WriteGp2SettingsCommand.cs @@ -38,13 +38,13 @@ public WriteGp2SettingsCommand(IServiceProvider serviceProvider) : base(serviceP [Option(Templates.GpDesignation, "The GP2 power up designation", CommandOptionType.SingleValue)] public (bool HasValue, Gp2Designation Value) Designation { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadGpSettings(); - UpdateGpSetting(app, console, device, device.GpSettings.Gp2PowerUpSetting, Designation); + UpdateGpSetting(application, console, device, device.GpSettings!.Gp2PowerUpSetting, Designation); return 0; }); diff --git a/MCP2221IOConsole/Commands/Flash/WriteGp3SettingsCommand.cs b/MCP2221IOConsole/Commands/Flash/WriteGp3SettingsCommand.cs index cb36015..65b516b 100644 --- a/MCP2221IOConsole/Commands/Flash/WriteGp3SettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/WriteGp3SettingsCommand.cs @@ -15,13 +15,13 @@ public WriteGp3SettingsCommand(IServiceProvider serviceProvider) : base(serviceP [Option(Templates.GpDesignation, "The GP3 power up designation", CommandOptionType.SingleValue)] public (bool HasValue, Gp3Designation Value) Designation { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadGpSettings(); - UpdateGpSetting(app, console, device, device.GpSettings.Gp3PowerUpSetting, Designation); + UpdateGpSetting(application, console, device, device.GpSettings!.Gp3PowerUpSetting!, Designation); return 0; }); diff --git a/MCP2221IOConsole/Commands/Flash/WriteUsbDescriptorsCommand.cs b/MCP2221IOConsole/Commands/Flash/WriteUsbDescriptorsCommand.cs index ca7d2f2..3085dd9 100644 --- a/MCP2221IOConsole/Commands/Flash/WriteUsbDescriptorsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/WriteUsbDescriptorsCommand.cs @@ -43,7 +43,7 @@ public WriteUsbDescriptorsCommand(IServiceProvider serviceProvider) : base(servi [Option(Templates.UsbSerialNumber, "The USB serial number string descriptor", CommandOptionType.SingleValue)] public (bool HasValue, string Value) SerialNumber { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { @@ -65,7 +65,7 @@ protected override int OnExecute(CommandLineApplication app, IConsole console) if(!(Manufacturer.HasValue || Product.HasValue || SerialNumber.HasValue)) { console.Error.WriteLine("No update values specified"); - app.ShowHelp(); + application.ShowHelp(); } return Manufacturer.HasValue || Product.HasValue || SerialNumber.HasValue ? 0 : -1; diff --git a/MCP2221IOConsole/Commands/Gpio/GpioReadSettingsCommand.cs b/MCP2221IOConsole/Commands/Gpio/GpioReadSettingsCommand.cs index b512037..8df43ff 100644 --- a/MCP2221IOConsole/Commands/Gpio/GpioReadSettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Gpio/GpioReadSettingsCommand.cs @@ -34,7 +34,7 @@ public GpioReadSettingsCommand(IServiceProvider serviceProvider) : base(serviceP { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/Gpio/GpioWriteSettingsCommand.cs b/MCP2221IOConsole/Commands/Gpio/GpioWriteSettingsCommand.cs index 511b618..4f9dbee 100644 --- a/MCP2221IOConsole/Commands/Gpio/GpioWriteSettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Gpio/GpioWriteSettingsCommand.cs @@ -39,7 +39,7 @@ public GpioWriteSettingsCommand(IServiceProvider serviceProvider) : base(service [Range(0, 3)] [Option(Templates.GpioPorts, Description = "The GPIO ports")] - public int[] Ports { get; set; } + public int[]? Ports { get; set; } [Option(Templates.GpioIsInput, Description = "The GPIO pin is set as input if set")] public (bool HasValue, bool Value) IsInput { get; set; } @@ -47,7 +47,7 @@ public GpioWriteSettingsCommand(IServiceProvider serviceProvider) : base(service [Option(Templates.GpioValue, Description = "The GPIO pin value")] public (bool HasValue, bool Value) Value { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { @@ -65,19 +65,19 @@ protected override int OnExecute(CommandLineApplication app, IConsole console) { if (port == 0) { - updated |= UpdatePort(0, device.GpioPort0, console); + updated |= UpdatePort(0, device.GpioPort0!, console); } else if (port == 1) { - updated |= UpdatePort(1, device.GpioPort1, console); + updated |= UpdatePort(1, device.GpioPort1!, console); } else if (port == 2) { - updated |= UpdatePort(2, device.GpioPort2, console); + updated |= UpdatePort(2, device.GpioPort2!, console); } else if (port == 3) { - updated |= UpdatePort(3, device.GpioPort3, console); + updated |= UpdatePort(3, device.GpioPort3!, console); } } @@ -96,7 +96,7 @@ protected override int OnExecute(CommandLineApplication app, IConsole console) else { console.Error.WriteLine("No update values specified"); - app.ShowHelp(); + application.ShowHelp(); } return result; diff --git a/MCP2221IOConsole/Commands/I2c/BaseI2cWriteCommand.cs b/MCP2221IOConsole/Commands/I2c/BaseI2cWriteCommand.cs index 16a3674..a39a2f0 100644 --- a/MCP2221IOConsole/Commands/I2c/BaseI2cWriteCommand.cs +++ b/MCP2221IOConsole/Commands/I2c/BaseI2cWriteCommand.cs @@ -39,6 +39,6 @@ public BaseI2cWriteCommand(IServiceProvider serviceProvider) : base(serviceProvi [Required] [Option(Templates.I2cData, "The data to write. Input as a Hex string", CommandOptionType.SingleValue)] - public IList Data { get; set; } + public IList? Data { get; set; } } } \ No newline at end of file diff --git a/MCP2221IOConsole/Commands/I2c/I2cCancelTransferCommand.cs b/MCP2221IOConsole/Commands/I2c/I2cCancelTransferCommand.cs index 9bdafc1..e8cfb13 100644 --- a/MCP2221IOConsole/Commands/I2c/I2cCancelTransferCommand.cs +++ b/MCP2221IOConsole/Commands/I2c/I2cCancelTransferCommand.cs @@ -35,13 +35,13 @@ public I2cCancelTransferCommand(IServiceProvider serviceProvider) : base(service { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.CancelI2cBusTransfer(); - console.WriteLine($"Transfer state: {device.Status.CancelTransferState}"); + console.WriteLine($"Transfer state: {device.Status!.CancelTransferState}"); return 0; }); diff --git a/MCP2221IOConsole/Commands/I2c/I2cReadDataCommand.cs b/MCP2221IOConsole/Commands/I2c/I2cReadDataCommand.cs index 9831ab4..42932e1 100644 --- a/MCP2221IOConsole/Commands/I2c/I2cReadDataCommand.cs +++ b/MCP2221IOConsole/Commands/I2c/I2cReadDataCommand.cs @@ -37,7 +37,7 @@ public I2cReadDataCommand(IServiceProvider serviceProvider) : base(serviceProvid { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/I2c/I2cReadDataRepeatStartCommand.cs b/MCP2221IOConsole/Commands/I2c/I2cReadDataRepeatStartCommand.cs index a508049..a9a4285 100644 --- a/MCP2221IOConsole/Commands/I2c/I2cReadDataRepeatStartCommand.cs +++ b/MCP2221IOConsole/Commands/I2c/I2cReadDataRepeatStartCommand.cs @@ -37,7 +37,7 @@ public I2cReadDataRepeatStartCommand(IServiceProvider serviceProvider) : base(se { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/I2c/I2cScanBusCommand.cs b/MCP2221IOConsole/Commands/I2c/I2cScanBusCommand.cs index 379c47b..85e2ef9 100644 --- a/MCP2221IOConsole/Commands/I2c/I2cScanBusCommand.cs +++ b/MCP2221IOConsole/Commands/I2c/I2cScanBusCommand.cs @@ -39,7 +39,7 @@ public I2cScanBusCommand(IServiceProvider serviceProvider) : base(serviceProvide [Option(Templates.TenBitAddressing, "Use ten bit device addressing", CommandOptionType.SingleValue)] public (bool HasValue, bool Value) TenBitAddressing { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/I2c/I2cSetTransferSpeedCommand.cs b/MCP2221IOConsole/Commands/I2c/I2cSetTransferSpeedCommand.cs index 7b3cbb8..ef182a5 100644 --- a/MCP2221IOConsole/Commands/I2c/I2cSetTransferSpeedCommand.cs +++ b/MCP2221IOConsole/Commands/I2c/I2cSetTransferSpeedCommand.cs @@ -42,7 +42,7 @@ public I2cSetTransferSpeedCommand(IServiceProvider serviceProvider) : base(servi [Option(Templates.Speed, "The I2C speed", CommandOptionType.SingleValue)] public int Speed { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/I2c/I2cWriteDataCommand.cs b/MCP2221IOConsole/Commands/I2c/I2cWriteDataCommand.cs index 9c02f51..17670d9 100644 --- a/MCP2221IOConsole/Commands/I2c/I2cWriteDataCommand.cs +++ b/MCP2221IOConsole/Commands/I2c/I2cWriteDataCommand.cs @@ -36,17 +36,17 @@ public I2cWriteDataCommand(IServiceProvider serviceProvider) : base(serviceProvi { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { var address = ParseAddress(); - console.WriteLine($"Writing [{Data.Count}] bytes To device [{address}]"); + console.WriteLine("Writing [{Count}] bytes To device [{Address}]", Data!.Count, address); device.I2cWriteData(address, Data); - console.WriteLine($"Wrote [{Data.Count}] bytes To device"); + console.WriteLine("Wrote [{Data}] bytes To device", Data.Count); return 0; }); diff --git a/MCP2221IOConsole/Commands/I2c/I2cWriteDataNoStopCommand.cs b/MCP2221IOConsole/Commands/I2c/I2cWriteDataNoStopCommand.cs index 3093374..ce35b2a 100644 --- a/MCP2221IOConsole/Commands/I2c/I2cWriteDataNoStopCommand.cs +++ b/MCP2221IOConsole/Commands/I2c/I2cWriteDataNoStopCommand.cs @@ -36,17 +36,17 @@ public I2cWriteDataNoStopCommand(IServiceProvider serviceProvider) : base(servic { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { var address = ParseAddress(); - console.WriteLine($"Writing [{Data.Count}] bytes to device [{address}] with NO-STOP"); + console.WriteLine("Writing [{Count}] bytes to device [{Address}] with NO-STOP", Data!.Count, address); device.I2cWriteDataNoStop(address, Data); - console.WriteLine($"Wrote [{Data.Count}] bytes to device"); + console.WriteLine("Wrote [{Count}] bytes to device", Data.Count); return 0; }); diff --git a/MCP2221IOConsole/Commands/I2c/I2cWriteDataRepeatStartCommand.cs b/MCP2221IOConsole/Commands/I2c/I2cWriteDataRepeatStartCommand.cs index d48c5ae..aa8414a 100644 --- a/MCP2221IOConsole/Commands/I2c/I2cWriteDataRepeatStartCommand.cs +++ b/MCP2221IOConsole/Commands/I2c/I2cWriteDataRepeatStartCommand.cs @@ -36,17 +36,17 @@ public I2cWriteDataRepeatStartCommand(IServiceProvider serviceProvider) : base(s { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { var address = ParseAddress(); - console.WriteLine($"Writing [{Data.Count}] bytes to device [{address}]"); + console.WriteLine("Writing [{Count}] bytes to device [{Address}]", Data!.Count, address); device.I2cWriteDataRepeatStart(address, Data); - console.WriteLine($"Wrote [{Data.Count}] bytes to device"); + console.WriteLine("Wrote [{Count}] bytes to device", Data.Count); return 0; }); diff --git a/MCP2221IOConsole/Commands/ResetCommand.cs b/MCP2221IOConsole/Commands/ResetCommand.cs index 93c392e..3f61c4e 100644 --- a/MCP2221IOConsole/Commands/ResetCommand.cs +++ b/MCP2221IOConsole/Commands/ResetCommand.cs @@ -34,7 +34,7 @@ public ResetCommand(IServiceProvider serviceProvider) : base(serviceProvider) { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusBlockReadCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusBlockReadCommand.cs index 3067f8c..d99b17f 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusBlockReadCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusBlockReadCommand.cs @@ -42,7 +42,7 @@ public SmBusBlockReadCommand(IServiceProvider serviceProvider) : base(servicePro [Option(Templates.Count, "The SmBus block count", CommandOptionType.SingleValue)] public byte Count { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusBlockWriteCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusBlockWriteCommand.cs index 91b5ecd..710e794 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusBlockWriteCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusBlockWriteCommand.cs @@ -39,9 +39,9 @@ public SmBusBlockWriteCommand(IServiceProvider serviceProvider) : base(servicePr [Required] [Option(Templates.I2cData, "The data to write. Input as a Hex string", CommandOptionType.SingleValue)] - public IList Data { get; set; } + public IList? Data { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { @@ -49,9 +49,9 @@ protected override int OnExecute(CommandLineApplication app, IConsole console) console.WriteLine($"Writing a block of data to the SmBus device address [{deviceAddress}]"); - device.SmBusBlockWrite(deviceAddress, Command, Data, Pec); + device.SmBusBlockWrite(deviceAddress, Command, Data!, Pec); - console.WriteLine($"Write [{Data.Count}] bytes to the SmBus device"); + console.WriteLine("Write [{Count}] bytes to the SmBus device", Data!.Count); return 0; }); diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusQuickCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusQuickCommand.cs index 80cded8..08da843 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusQuickCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusQuickCommand.cs @@ -38,7 +38,7 @@ public SmBusQuickCommand(IServiceProvider serviceProvider) : base(serviceProvide [Option(Templates.Write, "A write quick command", CommandOptionType.SingleOrNoValue)] public bool Write { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusReadByteCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusReadByteCommand.cs index aeddc34..f002589 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusReadByteCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusReadByteCommand.cs @@ -35,7 +35,7 @@ public SmBusReadByteCommand(IServiceProvider serviceProvider) : base(serviceProv { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusReadCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusReadCommand.cs index 3c19c80..6347eb2 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusReadCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusReadCommand.cs @@ -40,7 +40,7 @@ public SmBusReadCommand(IServiceProvider serviceProvider) : base(serviceProvider [Option(Templates.Pec, "Use SmBus pec", CommandOptionType.SingleValue)] public bool Pec { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusReadIntCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusReadIntCommand.cs index a16d5bf..68f20c0 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusReadIntCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusReadIntCommand.cs @@ -35,7 +35,7 @@ public SmBusReadIntCommand(IServiceProvider serviceProvider) : base(serviceProvi { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusReadLongCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusReadLongCommand.cs index c3ba748..9ccf27d 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusReadLongCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusReadLongCommand.cs @@ -35,7 +35,7 @@ public SmBusReadLongCommand(IServiceProvider serviceProvider) : base(serviceProv { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusReadShortCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusReadShortCommand.cs index 5282779..d55a850 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusReadShortCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusReadShortCommand.cs @@ -35,7 +35,7 @@ public SmBusReadShortCommand(IServiceProvider serviceProvider) : base(servicePro { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusWriteByteCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusWriteByteCommand.cs index fecd6e9..e0ef6a4 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusWriteByteCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusWriteByteCommand.cs @@ -40,7 +40,7 @@ public SmBusWriteByteCommand(IServiceProvider serviceProvider) : base(servicePro [Option(Templates.Data, "The byte value to write", CommandOptionType.SingleOrNoValue)] public byte Data { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusWriteCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusWriteCommand.cs index 69917c5..73a5baa 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusWriteCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusWriteCommand.cs @@ -44,7 +44,7 @@ public SmBusWriteCommand(IServiceProvider serviceProvider) : base(serviceProvide [Option(Templates.Pec, "Use SmBus pec", CommandOptionType.SingleValue)] public bool Pec { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusWriteIntCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusWriteIntCommand.cs index 64fe6fc..2c42e80 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusWriteIntCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusWriteIntCommand.cs @@ -40,7 +40,7 @@ public SmBusWriteIntCommand(IServiceProvider serviceProvider) : base(serviceProv [Option(Templates.Data, "The long value to write", CommandOptionType.SingleOrNoValue)] public int Data { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusWriteLongCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusWriteLongCommand.cs index 310bf00..b80f6b8 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusWriteLongCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusWriteLongCommand.cs @@ -40,7 +40,7 @@ public SmBusWriteLongCommand(IServiceProvider serviceProvider) : base(servicePro [Option(Templates.Data, "The long value to write", CommandOptionType.SingleOrNoValue)] public long Data { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/SmBus/SmBusWriteShortCommand.cs b/MCP2221IOConsole/Commands/SmBus/SmBusWriteShortCommand.cs index 83e159a..173d16c 100644 --- a/MCP2221IOConsole/Commands/SmBus/SmBusWriteShortCommand.cs +++ b/MCP2221IOConsole/Commands/SmBus/SmBusWriteShortCommand.cs @@ -40,7 +40,7 @@ public SmBusWriteShortCommand(IServiceProvider serviceProvider) : base(servicePr [Option(Templates.Data, "The short value to write", CommandOptionType.SingleOrNoValue)] public short Data { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/Commands/Sram/BaseSramWriteGpSettingsCommand.cs b/MCP2221IOConsole/Commands/Sram/BaseSramWriteGpSettingsCommand.cs index d139e14..9d4407e 100644 --- a/MCP2221IOConsole/Commands/Sram/BaseSramWriteGpSettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Sram/BaseSramWriteGpSettingsCommand.cs @@ -41,12 +41,12 @@ protected BaseSramWriteGpSettingsCommand(IServiceProvider serviceProvider) : bas [Option(Templates.SramGpValue, "The GP output value of the pin if set as output", CommandOptionType.SingleValue)] public (bool HasValue, bool Value) Value { get; set; } - internal void UpdateSramGpSetting(CommandLineApplication app, IConsole console, IDevice device, GpSetting gpio, (bool HasValue, T Value) designation) where T : Enum + internal void UpdateSramGpSetting(CommandLineApplication application, IConsole console, IDevice device, GpSetting gpio, (bool HasValue, T Value) designation) where T : Enum { if (!(IsInput.HasValue || Value.HasValue || designation.HasValue)) { console.Error.WriteLine("No update values specified"); - app.ShowHelp(); + application.ShowHelp(); } else { diff --git a/MCP2221IOConsole/Commands/Sram/SramReadSettingsCommand.cs b/MCP2221IOConsole/Commands/Sram/SramReadSettingsCommand.cs index bbbb690..a800365 100644 --- a/MCP2221IOConsole/Commands/Sram/SramReadSettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Sram/SramReadSettingsCommand.cs @@ -34,13 +34,13 @@ public SramReadSettingsCommand(IServiceProvider serviceProvider) : base(serviceP { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadSramSettings(); - console.WriteLine(device.SramSettings); + console.WriteLine(device.SramSettings!); return 0; }); diff --git a/MCP2221IOConsole/Commands/Sram/SramWriteGp0SettingsCommand.cs b/MCP2221IOConsole/Commands/Sram/SramWriteGp0SettingsCommand.cs index e0259b8..0baed1d 100644 --- a/MCP2221IOConsole/Commands/Sram/SramWriteGp0SettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Sram/SramWriteGp0SettingsCommand.cs @@ -38,13 +38,13 @@ public SramWriteGp0SettingsCommand(IServiceProvider serviceProvider) : base(serv [Option(Templates.SramGpDesignation, "The GP 0 designation", CommandOptionType.SingleValue)] public (bool HasValue, Gp0Designation Value) Designation { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadSramSettings(); - UpdateSramGpSetting(app, console, device, device.SramSettings.Gp0Settings, Designation); + UpdateSramGpSetting(application, console, device, device.SramSettings!.Gp0Settings!, Designation); return 0; }); diff --git a/MCP2221IOConsole/Commands/Sram/SramWriteGp1SettingsCommand.cs b/MCP2221IOConsole/Commands/Sram/SramWriteGp1SettingsCommand.cs index 596a873..c8fe9d0 100644 --- a/MCP2221IOConsole/Commands/Sram/SramWriteGp1SettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Sram/SramWriteGp1SettingsCommand.cs @@ -38,13 +38,13 @@ public SramWriteGp1SettingsCommand(IServiceProvider serviceProvider) : base(serv [Option(Templates.SramGpDesignation, "The GP 1 designation", CommandOptionType.SingleValue)] public (bool HasValue, Gp1Designation Value) Designation { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadSramSettings(); - UpdateSramGpSetting(app, console, device, device.SramSettings.Gp1Settings, Designation); + UpdateSramGpSetting(application, console, device, device.SramSettings!.Gp1Settings!, Designation); return 0; }); diff --git a/MCP2221IOConsole/Commands/Sram/SramWriteGp2SettingsCommand.cs b/MCP2221IOConsole/Commands/Sram/SramWriteGp2SettingsCommand.cs index aa88675..0b03919 100644 --- a/MCP2221IOConsole/Commands/Sram/SramWriteGp2SettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Sram/SramWriteGp2SettingsCommand.cs @@ -38,13 +38,13 @@ public SramWriteGp2SettingsCommand(IServiceProvider serviceProvider) : base(serv [Option(Templates.SramGpDesignation, "The GP 2 designation", CommandOptionType.SingleValue)] public (bool HasValue, Gp2Designation Value) Designation { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadSramSettings(); - UpdateSramGpSetting(app, console, device, device.SramSettings.Gp2Settings, Designation); + UpdateSramGpSetting(application, console, device, device.SramSettings!.Gp2Settings!, Designation); return 0; }); diff --git a/MCP2221IOConsole/Commands/Sram/SramWriteGp3SettingsCommand.cs b/MCP2221IOConsole/Commands/Sram/SramWriteGp3SettingsCommand.cs index 51ad99d..e611d38 100644 --- a/MCP2221IOConsole/Commands/Sram/SramWriteGp3SettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Sram/SramWriteGp3SettingsCommand.cs @@ -38,13 +38,13 @@ public SramWriteGp3SettingsCommand(IServiceProvider serviceProvider) : base(serv [Option(Templates.SramGpDesignation, "The GP 3 designation", CommandOptionType.SingleValue)] public (bool HasValue, Gp3Designation Value) Designation { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadSramSettings(); - UpdateSramGpSetting(app, console, device, device.SramSettings.Gp3Settings, Designation); + UpdateSramGpSetting(application, console, device, device.SramSettings!.Gp3Settings!, Designation); return 0; }); diff --git a/MCP2221IOConsole/Commands/Sram/SramWriteSettingsCommand.cs b/MCP2221IOConsole/Commands/Sram/SramWriteSettingsCommand.cs index cbee948..5145d15 100644 --- a/MCP2221IOConsole/Commands/Sram/SramWriteSettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Sram/SramWriteSettingsCommand.cs @@ -67,7 +67,7 @@ public SramWriteSettingsCommand(IServiceProvider serviceProvider) : base(service [Option(Templates.ClearInterrupt, "Clear Interrupt", CommandOptionType.SingleValue)] public bool ClearInterrupt { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { @@ -75,55 +75,55 @@ protected override int OnExecute(CommandLineApplication app, IConsole console) device.ReadSramSettings(); - if (AdcRef.HasValue) + if (AdcRef.HasValue && device.SramSettings != null) { device.SramSettings.AdcRefOption = AdcRef.Value; modified = true; } - if(AdcVrmRef.HasValue) + if(AdcVrmRef.HasValue && device.SramSettings != null) { device.SramSettings.AdcRefVrm = AdcVrmRef.Value; modified |= true; } - if (ClockDivider.HasValue) + if (ClockDivider.HasValue && device.SramSettings != null) { device.SramSettings.ClockDivider = ClockDivider.Value; modified |= true; } - if (DutyCycle.HasValue) + if (DutyCycle.HasValue && device.SramSettings != null) { device.SramSettings.ClockDutyCycle = DutyCycle.Value; modified |= true; } - if (DacOutput.HasValue) + if (DacOutput.HasValue && device.SramSettings != null) { device.SramSettings.DacOutput = DacOutput.Value; modified |= true; } - if (DacRef.HasValue) + if (DacRef.HasValue && device.SramSettings != null) { device.SramSettings.DacRefOption = DacRef.Value; modified |= true; } - if (DacRefVrm.HasValue) + if (DacRefVrm.HasValue && device.SramSettings != null) { device.SramSettings.DacRefVrm = DacRefVrm.Value; modified |= true; } - if (InterruptNegativeEdge.HasValue) + if (InterruptNegativeEdge.HasValue && device.SramSettings != null) { device.SramSettings.InterruptNegativeEdge = InterruptNegativeEdge.Value; modified |= true; } - if (InterruptPositiveEdge.HasValue) + if (InterruptPositiveEdge.HasValue && device.SramSettings != null) { device.SramSettings.InterruptPositiveEdge = InterruptPositiveEdge.Value; modified |= true; @@ -139,7 +139,7 @@ protected override int OnExecute(CommandLineApplication app, IConsole console) else { console.Error.WriteLine("No update values specified"); - app.ShowHelp(); + application.ShowHelp(); } return 0; diff --git a/MCP2221IOConsole/Commands/Status/StatusReadCommand.cs b/MCP2221IOConsole/Commands/Status/StatusReadCommand.cs index 62f7a5c..2643e98 100644 --- a/MCP2221IOConsole/Commands/Status/StatusReadCommand.cs +++ b/MCP2221IOConsole/Commands/Status/StatusReadCommand.cs @@ -34,13 +34,13 @@ public StatusReadCommand(IServiceProvider serviceProvider) : base(serviceProvide { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { device.ReadDeviceStatus(); - console.WriteLine(device.Status); + console.WriteLine(device.Status!); return 0; }); diff --git a/MCP2221IOConsole/Commands/UnlockCommand.cs b/MCP2221IOConsole/Commands/UnlockCommand.cs index e8e931b..d480f40 100644 --- a/MCP2221IOConsole/Commands/UnlockCommand.cs +++ b/MCP2221IOConsole/Commands/UnlockCommand.cs @@ -34,13 +34,14 @@ internal class UnlockCommand : BaseCommand { public UnlockCommand(IServiceProvider serviceProvider) : base(serviceProvider) { + Password = string.Empty; } [Required] [Option(Templates.Password, "The 8 byte password value", CommandOptionType.SingleValue)] public string Password { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((device) => { diff --git a/MCP2221IOConsole/GlobalSuppressions.cs b/MCP2221IOConsole/GlobalSuppressions.cs new file mode 100644 index 0000000..2ce68d7 --- /dev/null +++ b/MCP2221IOConsole/GlobalSuppressions.cs @@ -0,0 +1,35 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Flash")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.SmBus")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Parsers")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.BaseCommand.Vid")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.BaseCommand.Pid")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Flash.WriteChipSettingsCommand.DacOutput")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Flash.WriteChipSettingsCommand.DacRef")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Flash.WriteChipSettingsCommand.DacRefVrm")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Flash.WriteChipSettingsCommand.NewPid")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Flash.WriteChipSettingsCommand.NewVid")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Flash.WriteChipSettingsCommand.AdcVrmRef")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Sram")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramWriteGp0SettingsCommand.#ctor(System.IServiceProvider)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.DacOutput")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.DacRef")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Gpio")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.BaseSramWriteGpSettingsCommand.#ctor(System.IServiceProvider)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramWriteGp2SettingsCommand.#ctor(System.IServiceProvider)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.DacRefVrm")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.#ctor(System.IServiceProvider)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.AdcVrmRef")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.I2c")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Gpio.GpioWriteSettingsCommand.#ctor(System.IServiceProvider)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramWriteGp1SettingsCommand.#ctor(System.IServiceProvider)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Status")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramReadSettingsCommand.#ctor(System.IServiceProvider)")] diff --git a/MCP2221IOConsole/MCP2221IOConsole.csproj b/MCP2221IOConsole/MCP2221IOConsole.csproj index c90af33..1d3d3fa 100644 --- a/MCP2221IOConsole/MCP2221IOConsole.csproj +++ b/MCP2221IOConsole/MCP2221IOConsole.csproj @@ -24,8 +24,8 @@ - - + + diff --git a/MCP2221IOConsole/Parsers/BaseIntValueParser.cs b/MCP2221IOConsole/Parsers/BaseIntValueParser.cs index 3c3e2b9..9b89709 100644 --- a/MCP2221IOConsole/Parsers/BaseIntValueParser.cs +++ b/MCP2221IOConsole/Parsers/BaseIntValueParser.cs @@ -32,19 +32,19 @@ internal abstract class BaseIntValueParser : IValueParser { public Type TargetType => typeof(T); - public object Parse(string argName, string value, CultureInfo culture) + public object Parse(string? argName, string? value, CultureInfo culture) { - return ParseInternal(argName, value, culture, ParseFunction); + return BaseIntValueParser.ParseInternal(argName, value, culture, ParseFunction); } protected abstract (bool, T) ParseFunction(string value, CultureInfo culture, NumberStyles numberStyles); - private object ParseInternal(string argName, string value, CultureInfo culture, + private static object ParseInternal(string? argName, string? value, CultureInfo culture, Func valueParser) { (bool, T) parseResult; - - if (value.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase)) + + if (value!.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase)) { var strippedHexValue = value.Replace("0x", string.Empty).Replace("0X", string.Empty); @@ -60,7 +60,7 @@ private object ParseInternal(string argName, string value, CultureInfo culture, throw new FormatException($"Invalid value specified for {argName}. '{value}' is not a valid number."); } - return parseResult.Item2; + return parseResult.Item2!; } } -} +} \ No newline at end of file diff --git a/MCP2221IOConsole/Parsers/ByteListValueParser.cs b/MCP2221IOConsole/Parsers/ByteListValueParser.cs index 6e27664..3b00497 100644 --- a/MCP2221IOConsole/Parsers/ByteListValueParser.cs +++ b/MCP2221IOConsole/Parsers/ByteListValueParser.cs @@ -33,9 +33,11 @@ public class ByteListValueParser : IValueParser { public Type TargetType => typeof(IList); - public object Parse(string argName, string value, CultureInfo culture) + public object Parse(string? argName, string? value, CultureInfo culture) { - if(value.Length % 2 != 0) + ArgumentNullException.ThrowIfNull(value); + + if (value.Length % 2 != 0) { throw new FormatException($"Invalid value specified for {argName}. '{value}' is not multiple of 2"); } @@ -46,7 +48,7 @@ public object Parse(string argName, string value, CultureInfo culture) { if(!byte.TryParse(value.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out byte parsedValue)) { - throw new FormatException($"Invalid value specified for {argName}. '{value}' is an invalid hex string"); + throw new FormatException($"Invalid value specified for {argName}.'{value}' is an invalid hex string"); } data.Add(parsedValue); diff --git a/MCP2221IOConsole/Program.cs b/MCP2221IOConsole/Program.cs index 907be9a..5f17604 100644 --- a/MCP2221IOConsole/Program.cs +++ b/MCP2221IOConsole/Program.cs @@ -50,7 +50,7 @@ namespace MCP2221IOConsole [Subcommand(typeof(UnlockCommand))] class Program { - private static IConfiguration _configuration; + private static IConfiguration? _configuration; public static int Main(string[] args) { @@ -95,7 +95,7 @@ private static ServiceProvider BuildServiceProvider() { var serviceCollection = new ServiceCollection() .AddLogging(builder => builder.AddSerilog()) - .AddSingleton(_configuration) + .AddSingleton(_configuration!) .AddLogging(); return serviceCollection.BuildServiceProvider(); @@ -111,10 +111,10 @@ private static IConfiguration SetupConfiguration(string[] args) .Build(); } - private int OnExecute(CommandLineApplication app, IConsole console) + private int OnExecute(CommandLineApplication application, IConsole console) { console.WriteLine("You must specify a subcommand."); - app.ShowHelp(); + application.ShowHelp(); return 1; } } diff --git a/PModAqs/Commands/BaseCommand.cs b/PModAqs/Commands/BaseCommand.cs index 33f2a6f..4240c91 100644 --- a/PModAqs/Commands/BaseCommand.cs +++ b/PModAqs/Commands/BaseCommand.cs @@ -49,7 +49,7 @@ protected BaseCommand(IServiceProvider serviceProvider) public int Pid { get; set; } = 0x00DD; [Option(Templates.SerialNumber, "The MCP2221 instance serial number", CommandOptionType.SingleValue)] - public string Serial { get; set; } + public string Serial { get; set; } = string.Empty; [Required] [Range(typeof(uint), "0x07", "0x78")] @@ -66,12 +66,12 @@ protected int ExecuteCommand(Func action) if (hidDevice != null) { - using HidSharpHidDevice hidSharpHidDevice = new HidSharpHidDevice((ILogger)_serviceProvider.GetService(typeof(ILogger)), hidDevice); - using MCP2221IO.Device device = new MCP2221IO.Device((ILogger)_serviceProvider.GetService(typeof(ILogger)), hidSharpHidDevice); + using HidSharpHidDevice hidSharpHidDevice = new HidSharpHidDevice((ILogger)_serviceProvider.GetService(typeof(ILogger)), hidDevice); + using MCP2221IO.Device device = new MCP2221IO.Device((ILogger)_serviceProvider.GetService(typeof(ILogger)), hidSharpHidDevice); device.Open(); - using Ccs811 sensor = new Ccs811((ILogger)_serviceProvider.GetService(typeof(ILogger)), ParseAddress(), device); + using Ccs811 sensor = new Ccs811((ILogger)_serviceProvider.GetService(typeof(ILogger)), ParseAddress(), device); result = action(sensor); } @@ -88,10 +88,10 @@ protected int ExecuteCommand(Func action) return result; } - protected virtual int OnExecute(CommandLineApplication app, IConsole console) + protected virtual int OnExecute(CommandLineApplication application, IConsole console) { console.Error.WriteLine("You must specify a command. See --help for more details."); - app.ShowHelp(); + application.ShowHelp(); return 0; } diff --git a/PModAqs/Commands/DataCommand.cs b/PModAqs/Commands/DataCommand.cs index b3d1259..0a8788a 100644 --- a/PModAqs/Commands/DataCommand.cs +++ b/PModAqs/Commands/DataCommand.cs @@ -35,7 +35,7 @@ public DataCommand(IServiceProvider serviceProvider) : base(serviceProvider) { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((sensor) => { diff --git a/PModAqs/Commands/ErrorCommand.cs b/PModAqs/Commands/ErrorCommand.cs index 75a1c21..a1f8622 100644 --- a/PModAqs/Commands/ErrorCommand.cs +++ b/PModAqs/Commands/ErrorCommand.cs @@ -34,7 +34,7 @@ public ErrorCommand(IServiceProvider serviceProvider) : base(serviceProvider) { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((sensor) => { diff --git a/PModAqs/Commands/ReadModeCommand.cs b/PModAqs/Commands/ReadModeCommand.cs index 9532071..abd7b95 100644 --- a/PModAqs/Commands/ReadModeCommand.cs +++ b/PModAqs/Commands/ReadModeCommand.cs @@ -34,7 +34,7 @@ public ReadModeCommand(IServiceProvider serviceProvider) : base(serviceProvider) { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((sensor) => { diff --git a/PModAqs/Commands/ReadRawDataCommand.cs b/PModAqs/Commands/ReadRawDataCommand.cs index 08a92d4..330b75a 100644 --- a/PModAqs/Commands/ReadRawDataCommand.cs +++ b/PModAqs/Commands/ReadRawDataCommand.cs @@ -34,7 +34,7 @@ public ReadRawDataCommand(IServiceProvider serviceProvider) : base(serviceProvid { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((sensor) => { diff --git a/PModAqs/Commands/ResetCommand.cs b/PModAqs/Commands/ResetCommand.cs index 8ced372..61ff891 100644 --- a/PModAqs/Commands/ResetCommand.cs +++ b/PModAqs/Commands/ResetCommand.cs @@ -34,7 +34,7 @@ public ResetCommand(IServiceProvider serviceProvider) : base(serviceProvider) { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((sensor) => { diff --git a/PModAqs/Commands/StartCommand.cs b/PModAqs/Commands/StartCommand.cs index 90e018f..107d05f 100644 --- a/PModAqs/Commands/StartCommand.cs +++ b/PModAqs/Commands/StartCommand.cs @@ -34,7 +34,7 @@ public StartCommand(IServiceProvider serviceProvider) : base(serviceProvider) { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((sensor) => { diff --git a/PModAqs/Commands/StatusCommand.cs b/PModAqs/Commands/StatusCommand.cs index 5b54b46..eba0869 100644 --- a/PModAqs/Commands/StatusCommand.cs +++ b/PModAqs/Commands/StatusCommand.cs @@ -34,7 +34,7 @@ public StatusCommand(IServiceProvider serviceProvider) : base(serviceProvider) { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((sensor) => { diff --git a/PModAqs/Commands/VersionCommand.cs b/PModAqs/Commands/VersionCommand.cs index 0615ade..ee85c84 100644 --- a/PModAqs/Commands/VersionCommand.cs +++ b/PModAqs/Commands/VersionCommand.cs @@ -34,7 +34,7 @@ public VersionCommand(IServiceProvider serviceProvider) : base(serviceProvider) { } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((sensor) => { diff --git a/PModAqs/Commands/WriteModeCommand.cs b/PModAqs/Commands/WriteModeCommand.cs index 1358769..30cbe43 100644 --- a/PModAqs/Commands/WriteModeCommand.cs +++ b/PModAqs/Commands/WriteModeCommand.cs @@ -40,7 +40,7 @@ public WriteModeCommand(IServiceProvider serviceProvider) : base(serviceProvider [Option(Templates.Mode, "The sensor drive mode", CommandOptionType.SingleValue)] public DriveMode Mode { get; set; } - protected override int OnExecute(CommandLineApplication app, IConsole console) + protected override int OnExecute(CommandLineApplication application, IConsole console) { return ExecuteCommand((sensor) => { diff --git a/PModAqs/GlobalSuppressions.cs b/PModAqs/GlobalSuppressions.cs new file mode 100644 index 0000000..aa52b66 --- /dev/null +++ b/PModAqs/GlobalSuppressions.cs @@ -0,0 +1,13 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:PModAqs")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:PModAqs.Sensor")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:PModAqs.Commands")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:PModAqs.Commands.BaseCommand.Vid")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:PModAqs.Commands.BaseCommand.Pid")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:PModAqs.Sensor.Ccs811.#ctor(Microsoft.Extensions.Logging.ILogger{PModAqs.Sensor.Ccs811},MCP2221IO.I2cAddress,MCP2221IO.IDevice)")] diff --git a/PModAqs/Program.cs b/PModAqs/Program.cs index a94e79d..929fdb0 100644 --- a/PModAqs/Program.cs +++ b/PModAqs/Program.cs @@ -43,7 +43,7 @@ namespace PModAqs [Subcommand(typeof(VersionCommand))] class Program { - private static IConfiguration _configuration; + private static IConfiguration? _configuration; public static int Main(string[] args) { @@ -84,7 +84,7 @@ private static ServiceProvider BuildServiceProvider() { var serviceCollection = new ServiceCollection() .AddLogging(builder => builder.AddSerilog()) - .AddSingleton(_configuration) + .AddSingleton(_configuration!) .AddLogging(); return serviceCollection.BuildServiceProvider(); @@ -100,10 +100,10 @@ private static IConfiguration SetupConfiguration(string[] args) .Build(); } - private int OnExecute(CommandLineApplication app, IConsole console) + private int OnExecute(CommandLineApplication application, IConsole console) { console.WriteLine("You must specify a subcommand."); - app.ShowHelp(); + application.ShowHelp(); return 1; } } diff --git a/PModAqs/Sensor/Ccs811.cs b/PModAqs/Sensor/Ccs811.cs index 8cdd9c0..7273eee 100644 --- a/PModAqs/Sensor/Ccs811.cs +++ b/PModAqs/Sensor/Ccs811.cs @@ -32,13 +32,11 @@ namespace PModAqs.Sensor { internal class Ccs811 : ICcs811 { - private const int Resistor = 100000; - private readonly ILogger _logger; private readonly I2cAddress _i2cAddress; - private IDevice _device; + private IDevice? _device; - public Ccs811(ILogger logger, I2cAddress i2cAddress, IDevice device) + public Ccs811(ILogger logger, I2cAddress i2cAddress, IDevice device) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _i2cAddress = i2cAddress ?? throw new ArgumentNullException(nameof(i2cAddress)); @@ -64,7 +62,7 @@ public VersionInfo GetVersion() // public Mode GetMode() { - return new Mode(ReadRegister(Registers.Mode, 1).First()); + return new Mode(ReadRegister(Registers.Mode, 1)[0]); } // @@ -82,25 +80,25 @@ public SensorData GetData() // public Status GetStatus() { - return (Status)ReadRegister(Registers.Status, 1).First(); + return (Status)ReadRegister(Registers.Status, 1)[0]; } // public Error GetError() { - return (Error)ReadRegister(Registers.ErrorId, 1).First(); + return (Error)ReadRegister(Registers.ErrorId, 1)[0]; } // public void StartApplication() { - _device.I2cWriteData(_i2cAddress, new List() { (byte)Registers.ApplicationStart } ); + _device!.I2cWriteData(_i2cAddress, new List() { (byte)Registers.ApplicationStart } ); } // public void Reset() { - _device.I2cWriteData(_i2cAddress, new List() { 0x11, 0xE5, 0x72, 0x8A }); + _device!.I2cWriteData(_i2cAddress, new List() { 0x11, 0xE5, 0x72, 0x8A }); } // @@ -112,7 +110,7 @@ public double GetTemperature() // public void SetMode(DriveMode mode) { - _device.I2cWriteDataNoStop(_i2cAddress, new List() { (byte)Registers.Mode }); + _device!.I2cWriteDataNoStop(_i2cAddress, new List() { (byte)Registers.Mode }); var data = _device.I2cReadDataRepeatedStart(_i2cAddress, 1); @@ -126,7 +124,7 @@ public void SetMode(DriveMode mode) private IList ReadRegister(Registers register, ushort count) { - _device.I2cWriteDataNoStop(_i2cAddress, new List() { (byte)register }); + _device!.I2cWriteDataNoStop(_i2cAddress, new List() { (byte)register }); return _device.I2cReadDataRepeatedStart(_i2cAddress, count); } From fad0361b563df91f53ea11b99885d4075d7ed10c Mon Sep 17 00:00:00 2001 From: DerekGn Date: Tue, 21 Jan 2025 12:29:16 +0000 Subject: [PATCH 2/9] Naming Suppression --- MCP2221IO/GlobalSuppressions.cs | 8 ++++---- MCP2221IOConsole/GlobalSuppressions.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/MCP2221IO/GlobalSuppressions.cs b/MCP2221IO/GlobalSuppressions.cs index 24ebc5f..4daf4c7 100644 --- a/MCP2221IO/GlobalSuppressions.cs +++ b/MCP2221IO/GlobalSuppressions.cs @@ -29,10 +29,6 @@ [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.DeviceStatus.SclLineState")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.StatusSetParametersResponse.Deserialize(System.IO.Stream)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadChipSettingsResponse.Deserialize(System.IO.Stream)")] -[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Exceptions.SmBusInvalidCrcException")] -[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Exceptions.GpioNotEnabledException")] -[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Gpio.GpioPort")] -[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Settings.SramSettings")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.GetI2cDataResponse.Deserialize(System.IO.Stream)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadGpioPortsResponse.#ctor")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadGpSettingsResponse.Deserialize(System.IO.Stream)")] @@ -48,3 +44,7 @@ [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Responses.ReadSramSettingsResponse.SramSettings")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadSramSettingsResponse.Deserialize(System.IO.Stream)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Responses.ReadFlashStringResponse.Deserialize(System.IO.Stream)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Exceptions.SmBusInvalidCrcException")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Exceptions.GpioNotEnabledException")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Gpio.GpioPort")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Settings.SramSettings")] diff --git a/MCP2221IOConsole/GlobalSuppressions.cs b/MCP2221IOConsole/GlobalSuppressions.cs index 2ce68d7..288c6e5 100644 --- a/MCP2221IOConsole/GlobalSuppressions.cs +++ b/MCP2221IOConsole/GlobalSuppressions.cs @@ -8,7 +8,11 @@ [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Flash")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Gpio")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Status")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.SmBus")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Sram")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.I2c")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Parsers")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.BaseCommand.Vid")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.BaseCommand.Pid")] @@ -18,18 +22,14 @@ [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Flash.WriteChipSettingsCommand.NewPid")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Flash.WriteChipSettingsCommand.NewVid")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Flash.WriteChipSettingsCommand.AdcVrmRef")] -[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Sram")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramWriteGp0SettingsCommand.#ctor(System.IServiceProvider)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.DacOutput")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.DacRef")] -[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Gpio")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.BaseSramWriteGpSettingsCommand.#ctor(System.IServiceProvider)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramWriteGp2SettingsCommand.#ctor(System.IServiceProvider)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.DacRefVrm")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.#ctor(System.IServiceProvider)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IOConsole.Commands.Sram.SramWriteSettingsCommand.AdcVrmRef")] -[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.I2c")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Gpio.GpioWriteSettingsCommand.#ctor(System.IServiceProvider)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramWriteGp1SettingsCommand.#ctor(System.IServiceProvider)")] -[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "namespace", Target = "~N:MCP2221IOConsole.Commands.Status")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramReadSettingsCommand.#ctor(System.IServiceProvider)")] From e77c573f62e81e46be56ddab92b904a410377787 Mon Sep 17 00:00:00 2001 From: DerekGn Date: Tue, 21 Jan 2025 18:26:54 +0000 Subject: [PATCH 3/9] Remove fluent assertions Updated package versions --- MCP2221IO.UnitTests/DeviceTests.cs | 113 +++++++++--------- MCP2221IO.UnitTests/I2CAddressTests.cs | 25 ++-- .../MCP2221IO.UnitTests.csproj | 14 +-- MCP2221IO.UnitTests/PasswordTests.cs | 11 +- MCP2221IO/MCP2221IO.csproj | 5 +- MCP2221IOConsole/MCP2221IOConsole.csproj | 25 ++-- PModAqs/PModAqs.csproj | 23 ++-- 7 files changed, 106 insertions(+), 110 deletions(-) diff --git a/MCP2221IO.UnitTests/DeviceTests.cs b/MCP2221IO.UnitTests/DeviceTests.cs index f312869..2fa0fc5 100644 --- a/MCP2221IO.UnitTests/DeviceTests.cs +++ b/MCP2221IO.UnitTests/DeviceTests.cs @@ -22,7 +22,6 @@ * SOFTWARE. */ -using FluentAssertions; using MCP2221IO.Commands; using MCP2221IO.Exceptions; using MCP2221IO.Gp; @@ -35,6 +34,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Linq; using Xunit; using Xunit.Abstractions; @@ -64,7 +64,7 @@ public void TestCancelI2cBusTransfer() _device.CancelI2cBusTransfer(); // Assert - _device.Status.Should().NotBeNull(); + Assert.NotNull(_device.Status); } [Fact] @@ -78,7 +78,7 @@ public void TestGetFactorySerialNumberOk() string serialNumber = _device.FactorySerialNumber!; // Assert - serialNumber.Should().NotBeNull(); + Assert.NotNull(serialNumber); _output.WriteLine(serialNumber); } @@ -94,7 +94,7 @@ public void TestGetUsbManufacturerDescriptorOk() string descriptor = _device.UsbManufacturerDescriptor; // Assert - descriptor.Should().NotBeNull(); + Assert.NotNull(descriptor); _output.WriteLine(descriptor); } @@ -110,7 +110,7 @@ public void TestGetUsbProductDescriptorOk() string descriptor = _device.UsbProductDescriptor; // Assert - descriptor.Should().NotBeNull(); + Assert.NotNull(descriptor); _output.WriteLine(descriptor); } @@ -126,7 +126,7 @@ public void TestGetUsbSerialNumberDescriptorOk() string descriptor = _device.UsbSerialNumberDescriptor; // Assert - descriptor.Should().NotBeNull(); + Assert.NotNull(descriptor); _output.WriteLine(descriptor); } @@ -149,7 +149,8 @@ public void TestI2cRead() // Assert _mockHidDevice.Verify(_ => _.WriteRead(It.IsAny()), Times.Exactly(4)); - buffer.Should().NotBeNullOrEmpty(); + Assert.NotNull(buffer); + Assert.NotEmpty(buffer); } [Fact] @@ -159,12 +160,10 @@ public void TestI2cReadAddressNull() // Arrange // Act -#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. Action act = () => { var buffer = _device.I2cReadData(null, 1); }; -#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. // Assert - act.Should().Throw(); + Assert.Throws(act); } [Fact] @@ -185,7 +184,9 @@ public void TestI2cReadRepeatedStart() // Assert _mockHidDevice.Verify(_ => _.WriteRead(It.IsAny()), Times.Exactly(4)); - buffer.Should().NotBeNullOrEmpty(); + + Assert.NotNull(buffer); + Assert.NotEmpty(buffer); } [Fact] @@ -200,7 +201,7 @@ public void TestI2cScanBusSevenBitAddress() var result = _device.I2cScanBusInternal(false, 9); // Assert - result.Should().NotBeEmpty(); + Assert.NotEmpty(result); } [Fact] @@ -215,7 +216,7 @@ public void TestI2cScanBusTenBitAddress() var result = _device.I2cScanBusInternal(true, 9); // Assert - result.Should().NotBeEmpty(); + Assert.NotEmpty(result); } [Fact] @@ -245,12 +246,10 @@ public void TestI2cWriteDataAddressNull() // Arrange // Act -#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. Action act = () => { _device.I2cWriteData(null, new List()); }; -#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. // Assert - act.Should().Throw(); + Assert.Throws(() => act()); } [Fact] @@ -259,12 +258,10 @@ public void TestI2cWriteDataDataNull() // Arrange // Act -#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. Action act = () => { _device.I2cWriteData(new I2cAddress(0x0A), null); }; -#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. // Assert - act.Should().Throw(); + Assert.Throws(() => act()); } [Fact] @@ -276,7 +273,7 @@ public void TestI2cWriteDataDataSize() Action act = () => { _device.I2cWriteData(new I2cAddress(0x0A), new byte[0xFFFFF]); }; // Assert - act.Should().Throw(); + Assert.Throws(() => act()); } [Fact] @@ -344,7 +341,7 @@ public void TestReadChipSettings() _device.ReadChipSettings(); // Assert - _device.ChipSettings.Should().NotBeNull(); + Assert.NotNull(_device.ChipSettings); _output.WriteLine(_device.ChipSettings!.ToString()); } @@ -360,7 +357,7 @@ public void TestReadDeviceStatus() _device.ReadDeviceStatus(); // Assert - _device.Status.Should().NotBeNull(); + Assert.NotNull(_device.Status); _output.WriteLine(_device.Status!.ToString()); } @@ -395,7 +392,7 @@ public void TestReadGpSettings() _device.ReadGpSettings(); // Assert - _device.GpSettings.Should().NotBeNull(); + Assert.NotNull(_device.GpSettings); _output.WriteLine(_device.GpSettings!.ToString()); } @@ -411,7 +408,7 @@ public void TestReadSramSettings() _device.ReadSramSettings(); // Assert - _device.SramSettings.Should().NotBeNull(); + Assert.NotNull(_device.SramSettings); _output.WriteLine(_device.SramSettings!.ToString()); } @@ -439,7 +436,7 @@ public void TestSetI2cBusSpeed() _device.SetI2cBusSpeed(100); // Assert - _device.Status.Should().NotBeNull(); + Assert.NotNull( _device.Status); } [Fact] @@ -453,7 +450,7 @@ public void TestSetI2cBusSpeedTimeout() Action act = () => { _device.SetI2cBusSpeed(100); }; // Assert - act.Should().Throw(); + Assert.Throws(() => act()); } [Fact] @@ -470,7 +467,7 @@ public void TestSetUsbManufacturerDescriptorOk() _device.UsbManufacturerDescriptor = "UPDATED"; // Assert - descriptor.Should().NotBeNull(); + Assert.NotNull(descriptor); } [Fact] @@ -487,7 +484,7 @@ public void TestSetUsbProductDescriptorOk() _device.UsbProductDescriptor = "UPDATED"; // Assert - descriptor.Should().NotBeNull(); + Assert.NotNull(descriptor); } [Fact] @@ -504,7 +501,7 @@ public void TestSetUsbSerialNumberDescriptorOk() _device.UsbSerialNumberDescriptor = "SERIAL NUMBER"; // Assert - descriptor.Should().NotBeNull(); + Assert.NotNull(descriptor); } [Fact] @@ -520,27 +517,28 @@ public void TestSmBusBlockRead() var result = _device.SmBusBlockRead(new I2cAddress(0x0B), 0x55, 5, true); // Assert - result.Should().NotBeEmpty(); + Assert.NotEmpty(result); } [Fact] public void TestSmBusBlockWrite() { - byte[]? writeData = null; + List writeData = new List(); // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) .Returns(WriteReponse(CommandCodes.WriteI2cData)) .Callback((byte[] b) => { - writeData = b; + writeData.AddRange(b); }); // Act _device.SmBusBlockWrite(new I2cAddress(0x0B), 0xAA, new List() { 0x01, 0x02, 0x45 }, true); // Assert - writeData.Should().Contain(new List() { 0x16, 0xAA, 0x03, 0x01, 0x02, 0x45, 0x89 }); + var expected = new List() { 0x16, 0xAA, 0x03, 0x01, 0x02, 0x45, 0x89 }; + Assert.Equal(expected.Count, writeData.Intersect(expected).Count()); } [Theory] @@ -579,7 +577,7 @@ public void TestSmBusReadByte() var result = _device.SmBusReadByte(new I2cAddress(10, I2cAddressSize.SevenBit), true); // Assert - result.Should().Be(0); + Assert.Equal(0, result); } [Fact] @@ -595,7 +593,7 @@ public void TestSmBusReadByteCommand() var result = _device.SmBusReadByteCommand(new I2cAddress(10, I2cAddressSize.SevenBit), 0x55, true); // Assert - result.Should().Be(0x55); + Assert.Equal(0x55, result); } [Fact] @@ -611,7 +609,7 @@ public void TestSmBusReadIntCommand() var result = _device.SmBusReadIntCommand(new I2cAddress(10, I2cAddressSize.SevenBit), 0x55, true); // Assert - result.Should().Be(0x55555555); + Assert.Equal(0x55555555, result); } [Fact] @@ -627,7 +625,7 @@ public void TestSmBusReadLongCommand() var result = _device.SmBusReadLongCommand(new I2cAddress(10, I2cAddressSize.SevenBit), 0x55, true); // Assert - result.Should().Be(-6148914692668172971L); + Assert.Equal(-6148914692668172971L, result); } [Fact] @@ -643,107 +641,112 @@ public void TestSmBusReadWordCommand() var result = _device.SmBusReadShortCommand(new I2cAddress(10, I2cAddressSize.SevenBit), 0x55, true); // Assert - result.Should().Be(-21931); + Assert.Equal(-21931, result); } [Fact] public void TestSmBusWriteByte() { - byte[]? writeData = null; + List writeData = new List(); // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) .Returns(WriteReponse(CommandCodes.WriteI2cData)) .Callback((byte[] b) => { - writeData = b; + writeData.AddRange(b); }); // Act _device.SmBusWriteByte(new I2cAddress(10, I2cAddressSize.SevenBit), 0x55, true); // Assert - writeData.Should().Contain(new List() { 0x14, 0x55, 0xAC }); + var expected = new List() { 0x14, 0x55, 0xAC }; + Assert.Equal(expected.Count, writeData.Intersect(expected).Count()); } [Fact] public void TestSmBusWriteByteCommand() { - byte[]? writeData = null; + List writeData = new List(); // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) .Returns(WriteReponse(CommandCodes.WriteI2cData)) .Callback((byte[] b) => { - writeData = b; + writeData.AddRange(b); }); // Act _device.SmBusWriteByteCommand(new I2cAddress(10, I2cAddressSize.SevenBit), 0x55, 0xAA, true); // Assert - writeData.Should().Contain(new List() { 0x14, 0x55, 0xAA, 0x12 }); + var expected = new List() { 0x14, 0x55, 0xAA, 0x12 }; + Assert.Equal(expected.Count, writeData.Intersect(expected).Count()); } [Fact] public void TestSmBusWriteIntCommand() { - byte[]? writeData = null; + List writeData = new List(); // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) .Returns(WriteReponse(CommandCodes.WriteI2cData)) .Callback((byte[] b) => { - writeData = b; + writeData.AddRange(b); }); // Act _device.SmBusWriteIntCommand(new I2cAddress(10, I2cAddressSize.SevenBit), 0x55, 0xFEED, true); // Assert - writeData.Should().Contain(new List() { 0x14, 0x55, 0xED, 0xFE, 0x00 }); + var expected = new List() { 0x14, 0x55, 0xED, 0xFE, 0x00 }; + Assert.Equal(expected.Count, writeData.Intersect(expected).Count()); } [Fact] public void TestSmBusWriteLongCommand() { - byte[]? writeData = null; + List writeData = new List(); // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) .Returns(WriteReponse(CommandCodes.WriteI2cData)) .Callback((byte[] b) => { - writeData = b; + writeData.AddRange(b); }); // Act _device.SmBusWriteLongCommand(new I2cAddress(10, I2cAddressSize.SevenBit), 0x55, 0xFEED, true); // Assert - writeData.Should().Contain(new List() { 0x14, 0x55, 0xED, 0xFE, 0x00 }); + var expected = new List() { 0x14, 0x55, 0xED, 0xFE, 0x00 }; + Assert.Equal(expected.Count, writeData.Intersect(expected).Count()); } [Fact] public void TestSmBusWriteShortCommand() { - byte[]? writeData = null; + List writeData = new List(); // Arrange _mockHidDevice.Setup(_ => _.WriteRead(It.IsAny())) .Returns(WriteReponse(CommandCodes.WriteI2cData)) .Callback((byte[] b) => { - writeData = b; + writeData.AddRange(b); }); // Act _device.SmBusWriteShortCommand(new I2cAddress(10, I2cAddressSize.SevenBit), 0x55, 2000, true); // Assert - writeData.Should().Contain(new List() { 0x14, 0x55, 0xD0, 0x07, 0x4B }); + var expected = new List() { 0x14, 0x55, 0xD0, 0x07, 0x4B }; + Assert.Equal(expected.Count, writeData.Intersect(expected).Count()); } [Fact] @@ -755,10 +758,10 @@ public void TestUnlockFlash() .Returns(WriteFlashUnlockResponse()); // Act - Action act = () => { _device.UnlockFlash(new Password("AA55AA55DEADBEEF")); }; + var exception = Record.Exception(() => _device.UnlockFlash(new Password("AA55AA55DEADBEEF"))); // Assert - act.Should().NotThrow(); + Assert.Null(exception); } [Fact] diff --git a/MCP2221IO.UnitTests/I2CAddressTests.cs b/MCP2221IO.UnitTests/I2CAddressTests.cs index d6c9427..3354ff4 100644 --- a/MCP2221IO.UnitTests/I2CAddressTests.cs +++ b/MCP2221IO.UnitTests/I2CAddressTests.cs @@ -22,7 +22,6 @@ * SOFTWARE. */ -using FluentAssertions; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -47,20 +46,16 @@ public void TestSevenBitAddress() I2cAddress address = new I2cAddress(0x08); // Assert - address.Size.Should().Be(I2cAddressSize.SevenBit); - address.WriteAddress.Should().Equal(new List { 0x10 }); - address.ReadAddress.Should().Equal(new List { 0x11 }); + Assert.Equal(I2cAddressSize.SevenBit, address.Size); + Assert.Equal(new List { 0x10 }, address.WriteAddress); + Assert.Equal(new List { 0x11 }, address.ReadAddress); } [Fact] [SuppressMessage("Minor Code Smell", "S1481:Unused local variables should be removed", Justification = "")] public void TestSevenBitAddressException() { - // Arrange Act - Action act = () => { I2cAddress address = new I2cAddress(0x07); }; - - // Assert - act.Should().Throw(); + ArgumentException exception = Assert.Throws(() => { I2cAddress address = new I2cAddress(0x07); }); } [Theory] @@ -74,20 +69,16 @@ public void TestTenBitAddress(uint address, byte msb, byte lsb) I2cAddress i2cAddress = new I2cAddress(address, I2cAddressSize.TenBit); // Assert - i2cAddress.Size.Should().Be(I2cAddressSize.TenBit); - i2cAddress.WriteAddress.Should().Equal(new List { msb, lsb }); - i2cAddress.ReadAddress.Should().Equal(new List { (byte)(msb + 1), lsb }); + Assert.Equal(I2cAddressSize.TenBit, i2cAddress.Size); + Assert.Equal(new List { msb, lsb }, i2cAddress.WriteAddress); + Assert.Equal(new List { (byte)(msb + 1), lsb }, i2cAddress.ReadAddress); } [Fact] [SuppressMessage("Minor Code Smell", "S1481:Unused local variables should be removed", Justification = "")] public void TestTenBitAddressException() { - // Arrange Act - Action act = () => { I2cAddress address = new I2cAddress(0x4FF, I2cAddressSize.TenBit); }; - - // Assert - act.Should().Throw(); + Assert.Throws(() => { I2cAddress address = new I2cAddress(0x4FF, I2cAddressSize.TenBit); }); } } } diff --git a/MCP2221IO.UnitTests/MCP2221IO.UnitTests.csproj b/MCP2221IO.UnitTests/MCP2221IO.UnitTests.csproj index 699f0c5..8bf4ce3 100644 --- a/MCP2221IO.UnitTests/MCP2221IO.UnitTests.csproj +++ b/MCP2221IO.UnitTests/MCP2221IO.UnitTests.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -7,15 +7,15 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/MCP2221IO.UnitTests/PasswordTests.cs b/MCP2221IO.UnitTests/PasswordTests.cs index a75ae1a..48eb42e 100644 --- a/MCP2221IO.UnitTests/PasswordTests.cs +++ b/MCP2221IO.UnitTests/PasswordTests.cs @@ -22,7 +22,6 @@ * SOFTWARE. */ -using FluentAssertions; using MCP2221IO.Settings; using System.Collections.Generic; using Xunit; @@ -49,23 +48,23 @@ public void TestOk() Password password = new Password(bytes); // Assert - password.Value.Should().Be("AA55AA55AA55AA55"); - password.Bytes.Should().Contain(bytes); + Assert.Equal("AA55AA55AA55AA55", password.Value); + Assert.Equal(bytes, password.Bytes); } [Fact] public void TestStringOk() { // Arrange - List bytes = new List() { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 }; + List bytes = new List() { 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA }; string test = "AA55AA55AA55AA55"; // Act Password password = new Password(test); // Assert - password.Value.Should().Be("AA55AA55AA55AA55"); - password.Bytes.Should().Contain(bytes); + Assert.Equal(test, password.Value); + Assert.Equal(bytes, password.Bytes); } } } diff --git a/MCP2221IO/MCP2221IO.csproj b/MCP2221IO/MCP2221IO.csproj index bd99ed0..0c72219 100644 --- a/MCP2221IO/MCP2221IO.csproj +++ b/MCP2221IO/MCP2221IO.csproj @@ -21,8 +21,9 @@ - - + + + diff --git a/MCP2221IOConsole/MCP2221IOConsole.csproj b/MCP2221IOConsole/MCP2221IOConsole.csproj index 1d3d3fa..22a372f 100644 --- a/MCP2221IOConsole/MCP2221IOConsole.csproj +++ b/MCP2221IOConsole/MCP2221IOConsole.csproj @@ -24,19 +24,20 @@ - - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/PModAqs/PModAqs.csproj b/PModAqs/PModAqs.csproj index 56d872f..3610642 100644 --- a/PModAqs/PModAqs.csproj +++ b/PModAqs/PModAqs.csproj @@ -9,18 +9,19 @@ - - - - - - - - - - - + + + + + + + + + + + + From 0aa42d0d9a6546defc400776ab33dc52080b7cc8 Mon Sep 17 00:00:00 2001 From: DerekGn Date: Tue, 21 Jan 2025 19:50:49 +0000 Subject: [PATCH 4/9] Removed flash unlock command FIxed warning --- MCP2221IO/Commands/UnlockFlashCommand.cs | 2 +- MCP2221IOConsole/Commands/Flash/FlashCommand.cs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/MCP2221IO/Commands/UnlockFlashCommand.cs b/MCP2221IO/Commands/UnlockFlashCommand.cs index eeb98a3..f03f0c8 100644 --- a/MCP2221IO/Commands/UnlockFlashCommand.cs +++ b/MCP2221IO/Commands/UnlockFlashCommand.cs @@ -49,7 +49,7 @@ public override void Serialize(Stream stream) { base.Serialize(stream); - stream.Write(Password.Bytes.ToArray()); + stream.Write(Password.Bytes!.ToArray()); } } } diff --git a/MCP2221IOConsole/Commands/Flash/FlashCommand.cs b/MCP2221IOConsole/Commands/Flash/FlashCommand.cs index 5bf3a52..aa01301 100644 --- a/MCP2221IOConsole/Commands/Flash/FlashCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/FlashCommand.cs @@ -34,7 +34,6 @@ namespace MCP2221IOConsole.Commands.Flash [Subcommand(typeof(WriteChipSettingsCommand))] [Subcommand(typeof(ReadUsbDescriptorsCommand))] [Subcommand(typeof(WriteUsbDescriptorsCommand))] - [Subcommand(typeof(UnlockCommand))] internal class FlashCommand : BaseCommand { public FlashCommand(IServiceProvider serviceProvider) : base(serviceProvider) From 9613f11d38f9faccee9ca4aab856764f33d239bb Mon Sep 17 00:00:00 2001 From: DerekGn Date: Tue, 21 Jan 2025 20:22:38 +0000 Subject: [PATCH 5/9] Fixed warnings --- MCP2221IO/Commands/I2cReadDataCommand.cs | 2 +- MCP2221IO/Commands/I2cWriteDataCommand.cs | 2 +- MCP2221IO/Commands/WriteChipSettingsCommand.cs | 2 +- MCP2221IO/GlobalSuppressions.cs | 5 +++++ MCP2221IOConsole/Commands/Flash/WriteGp2SettingsCommand.cs | 2 +- MCP2221IOConsole/GlobalSuppressions.cs | 2 ++ 6 files changed, 11 insertions(+), 4 deletions(-) diff --git a/MCP2221IO/Commands/I2cReadDataCommand.cs b/MCP2221IO/Commands/I2cReadDataCommand.cs index 0c632b4..57a6801 100644 --- a/MCP2221IO/Commands/I2cReadDataCommand.cs +++ b/MCP2221IO/Commands/I2cReadDataCommand.cs @@ -55,7 +55,7 @@ public override void Serialize(Stream stream) base.Serialize(stream); stream.WriteUShort(Length); - stream.Write(Address.ReadAddress.ToArray()); + stream.Write(Address.ReadAddress!.ToArray()); } } } diff --git a/MCP2221IO/Commands/I2cWriteDataCommand.cs b/MCP2221IO/Commands/I2cWriteDataCommand.cs index bdd612e..e39fe2a 100644 --- a/MCP2221IO/Commands/I2cWriteDataCommand.cs +++ b/MCP2221IO/Commands/I2cWriteDataCommand.cs @@ -55,7 +55,7 @@ public override void Serialize(Stream stream) { base.Serialize(stream); stream.WriteUShort((ushort)Data.Count); - stream.Write(Address.WriteAddress.ToArray()); + stream.Write(Address.WriteAddress!.ToArray()); stream.Write(Data.ToArray(), 0, Data.Count); } } diff --git a/MCP2221IO/Commands/WriteChipSettingsCommand.cs b/MCP2221IO/Commands/WriteChipSettingsCommand.cs index 354e9ef..a61526d 100644 --- a/MCP2221IO/Commands/WriteChipSettingsCommand.cs +++ b/MCP2221IO/Commands/WriteChipSettingsCommand.cs @@ -47,7 +47,7 @@ public override void Serialize(Stream stream) ChipSettings.Serialize(stream); - stream.Write(Password.Bytes.ToArray()); + stream.Write(Password.Bytes!.ToArray()); } } } diff --git a/MCP2221IO/GlobalSuppressions.cs b/MCP2221IO/GlobalSuppressions.cs index 4daf4c7..9b96136 100644 --- a/MCP2221IO/GlobalSuppressions.cs +++ b/MCP2221IO/GlobalSuppressions.cs @@ -48,3 +48,8 @@ [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Exceptions.GpioNotEnabledException")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Gpio.GpioPort")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "type", Target = "~T:MCP2221IO.Settings.SramSettings")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IO.Commands.WriteGpioPortsCommand.#ctor(MCP2221IO.Gpio.GpioPort,MCP2221IO.Gpio.GpioPort,MCP2221IO.Gpio.GpioPort,MCP2221IO.Gpio.GpioPort)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Commands.WriteGpioPortsCommand.GpioPort0")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Commands.WriteGpioPortsCommand.GpioPort1")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Commands.WriteGpioPortsCommand.GpioPort2")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~P:MCP2221IO.Commands.WriteGpioPortsCommand.GpioPort3")] diff --git a/MCP2221IOConsole/Commands/Flash/WriteGp2SettingsCommand.cs b/MCP2221IOConsole/Commands/Flash/WriteGp2SettingsCommand.cs index 9987196..1239f3a 100644 --- a/MCP2221IOConsole/Commands/Flash/WriteGp2SettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Flash/WriteGp2SettingsCommand.cs @@ -44,7 +44,7 @@ protected override int OnExecute(CommandLineApplication application, IConsole co { device.ReadGpSettings(); - UpdateGpSetting(application, console, device, device.GpSettings!.Gp2PowerUpSetting, Designation); + UpdateGpSetting(application, console, device, device.GpSettings!.Gp2PowerUpSetting!, Designation); return 0; }); diff --git a/MCP2221IOConsole/GlobalSuppressions.cs b/MCP2221IOConsole/GlobalSuppressions.cs index 288c6e5..b0cb27b 100644 --- a/MCP2221IOConsole/GlobalSuppressions.cs +++ b/MCP2221IOConsole/GlobalSuppressions.cs @@ -33,3 +33,5 @@ [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Gpio.GpioWriteSettingsCommand.#ctor(System.IServiceProvider)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramWriteGp1SettingsCommand.#ctor(System.IServiceProvider)")] [assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Sram.SramReadSettingsCommand.#ctor(System.IServiceProvider)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Gpio.GpioReadSettingsCommand.#ctor(System.IServiceProvider)")] +[assembly: SuppressMessage("Naming", "VSSpell001:Spell Check", Justification = "", Scope = "member", Target = "~M:MCP2221IOConsole.Commands.Gpio.GpioCommand.#ctor(System.IServiceProvider)")] From ae4fc0cad7f8a47c60b8b22ea05359d3931eb333 Mon Sep 17 00:00:00 2001 From: DerekGn Date: Tue, 21 Jan 2025 20:33:49 +0000 Subject: [PATCH 6/9] Added write for do not care byte Fixed compiler warnings Updated null validations --- MCP2221IO/Commands/BaseCommand.cs | 5 +++++ MCP2221IO/Commands/CancelI2CBusTransferCommand.cs | 3 ++- .../Commands/ReadStatusSetParametersCommand.cs | 3 ++- MCP2221IO/Commands/UnlockFlashCommand.cs | 2 ++ MCP2221IO/Commands/UpdateI2CBusSpeedCommand.cs | 3 ++- MCP2221IO/Commands/WriteGpioPortsCommand.cs | 2 ++ MCP2221IO/Commands/WriteSramSettingsCommand.cs | 2 ++ .../Commands/WriteStatusSetParametersCommand.cs | 4 +++- MCP2221IO/Device.cs | 15 +++------------ MCP2221IO/Responses/BaseResponse.cs | 5 +---- PModAqs/Sensor/RawData.cs | 5 +---- PModAqs/Sensor/SensorData.cs | 5 +---- PModAqs/Sensor/VersionInfo.cs | 7 ++----- PModAqs/Sensor/VersionNumber.cs | 5 +---- 14 files changed, 29 insertions(+), 37 deletions(-) diff --git a/MCP2221IO/Commands/BaseCommand.cs b/MCP2221IO/Commands/BaseCommand.cs index 54915ab..64eba7c 100644 --- a/MCP2221IO/Commands/BaseCommand.cs +++ b/MCP2221IO/Commands/BaseCommand.cs @@ -55,5 +55,10 @@ public virtual void Serialize(Stream stream) stream.WriteByte(0); stream.WriteByte((byte)CommandCode); } + + internal static void WriteDnc(Stream stream) + { + stream.WriteByte(0xFF); + } } } diff --git a/MCP2221IO/Commands/CancelI2CBusTransferCommand.cs b/MCP2221IO/Commands/CancelI2CBusTransferCommand.cs index 8fe121e..c29a863 100644 --- a/MCP2221IO/Commands/CancelI2CBusTransferCommand.cs +++ b/MCP2221IO/Commands/CancelI2CBusTransferCommand.cs @@ -38,7 +38,8 @@ public override void Serialize(Stream stream) { base.Serialize(stream); - stream.WriteByte(0); // Don't care + WriteDnc(stream); + stream.WriteByte(0x10); // Cancel current I2C/SMBus transfer(sub - command) stream.WriteByte(0xFF); // Set I2C/SMBus communication speed (sub-command) no change } diff --git a/MCP2221IO/Commands/ReadStatusSetParametersCommand.cs b/MCP2221IO/Commands/ReadStatusSetParametersCommand.cs index 1e61cdd..429c0b9 100644 --- a/MCP2221IO/Commands/ReadStatusSetParametersCommand.cs +++ b/MCP2221IO/Commands/ReadStatusSetParametersCommand.cs @@ -36,7 +36,8 @@ public override void Serialize(Stream stream) { base.Serialize(stream); - stream.WriteByte(0); // Don't care + WriteDnc(stream); + stream.WriteByte(0xFF); // Cancel current I2C/SMBus transfer(sub - command) no change stream.WriteByte(0xFF); // Set I2C/SMBus communication speed (sub-command) no change } diff --git a/MCP2221IO/Commands/UnlockFlashCommand.cs b/MCP2221IO/Commands/UnlockFlashCommand.cs index f03f0c8..1396882 100644 --- a/MCP2221IO/Commands/UnlockFlashCommand.cs +++ b/MCP2221IO/Commands/UnlockFlashCommand.cs @@ -49,6 +49,8 @@ public override void Serialize(Stream stream) { base.Serialize(stream); + WriteDnc(stream); + stream.Write(Password.Bytes!.ToArray()); } } diff --git a/MCP2221IO/Commands/UpdateI2CBusSpeedCommand.cs b/MCP2221IO/Commands/UpdateI2CBusSpeedCommand.cs index 2430acd..5ce361f 100644 --- a/MCP2221IO/Commands/UpdateI2CBusSpeedCommand.cs +++ b/MCP2221IO/Commands/UpdateI2CBusSpeedCommand.cs @@ -47,7 +47,8 @@ public override void Serialize(Stream stream) { base.Serialize(stream); - stream.WriteByte(0); // Don't care + WriteDnc(stream); + stream.WriteByte(0x00); // Cancel current I2C/SMBus transfer(sub - command) no change stream.WriteByte(0x20); // Set I2C/SMBus communication speed (sub-command) diff --git a/MCP2221IO/Commands/WriteGpioPortsCommand.cs b/MCP2221IO/Commands/WriteGpioPortsCommand.cs index 0129b3f..e65dc6c 100644 --- a/MCP2221IO/Commands/WriteGpioPortsCommand.cs +++ b/MCP2221IO/Commands/WriteGpioPortsCommand.cs @@ -55,6 +55,8 @@ public override void Serialize(Stream stream) { base.Serialize(stream); + WriteDnc(stream); + GpioPort0.Serialize(stream); GpioPort1.Serialize(stream); GpioPort2.Serialize(stream); diff --git a/MCP2221IO/Commands/WriteSramSettingsCommand.cs b/MCP2221IO/Commands/WriteSramSettingsCommand.cs index c7a74d3..2c375b4 100644 --- a/MCP2221IO/Commands/WriteSramSettingsCommand.cs +++ b/MCP2221IO/Commands/WriteSramSettingsCommand.cs @@ -47,6 +47,8 @@ public override void Serialize(Stream stream) { base.Serialize(stream); + WriteDnc(stream); + int update = 0x80; update |= (int)SramSettings.ClockDutyCycle << 3; update |= (int)SramSettings.ClockDivider & 0b111; diff --git a/MCP2221IO/Commands/WriteStatusSetParametersCommand.cs b/MCP2221IO/Commands/WriteStatusSetParametersCommand.cs index 4bed961..01539f5 100644 --- a/MCP2221IO/Commands/WriteStatusSetParametersCommand.cs +++ b/MCP2221IO/Commands/WriteStatusSetParametersCommand.cs @@ -59,7 +59,9 @@ public WriteStatusSetParametersCommand(bool cancelTransfer = false, byte divisor public override void Serialize(Stream stream) { base.Serialize(stream); - stream.WriteByte(0); + + WriteDnc(stream); + stream.WriteByte(_cancelCommand); stream.WriteByte(_setSpeed); stream.WriteByte(_divisor); diff --git a/MCP2221IO/Device.cs b/MCP2221IO/Device.cs index 2765514..eaa0432 100644 --- a/MCP2221IO/Device.cs +++ b/MCP2221IO/Device.cs @@ -587,10 +587,7 @@ internal IList I2cScanBusInternal(bool useTenBitAddressing, uint upp private static void AssertAddress(I2cAddress address) { - if (address == null) - { - throw new ArgumentNullException(nameof(address)); - } + ArgumentNullException.ThrowIfNull(address); if (address.Size != I2cAddressSize.SevenBit) { @@ -758,15 +755,9 @@ private void HandleScanError(I2cAddress address, Exception ex) private void I2cWriteData(CommandCodes commandCode, I2cAddress address, IList data) where T : IResponse, new() { - if (address == null) - { - throw new ArgumentNullException(nameof(address)); - } + ArgumentNullException.ThrowIfNull(address); - if (data == null) - { - throw new ArgumentNullException(nameof(data)); - } + ArgumentNullException.ThrowIfNull(data); if (data.Count > IDevice.MaxI2cBlockSize) { diff --git a/MCP2221IO/Responses/BaseResponse.cs b/MCP2221IO/Responses/BaseResponse.cs index 459945f..81ccddf 100644 --- a/MCP2221IO/Responses/BaseResponse.cs +++ b/MCP2221IO/Responses/BaseResponse.cs @@ -47,10 +47,7 @@ protected BaseResponse(CommandCodes commandCode) // public virtual void Deserialize(Stream stream) { - if (stream == null) - { - throw new ArgumentNullException(nameof(stream)); - } + ArgumentNullException.ThrowIfNull(stream); if (stream.Length != 65) { diff --git a/PModAqs/Sensor/RawData.cs b/PModAqs/Sensor/RawData.cs index 1bc3680..d6b8a15 100644 --- a/PModAqs/Sensor/RawData.cs +++ b/PModAqs/Sensor/RawData.cs @@ -32,10 +32,7 @@ public class RawData { public RawData(IList data) { - if (data == null) - { - throw new ArgumentNullException(nameof(data)); - } + ArgumentNullException.ThrowIfNull(data); if (data.Count < 2) { diff --git a/PModAqs/Sensor/SensorData.cs b/PModAqs/Sensor/SensorData.cs index 764846f..838e61c 100644 --- a/PModAqs/Sensor/SensorData.cs +++ b/PModAqs/Sensor/SensorData.cs @@ -33,10 +33,7 @@ internal class SensorData { public SensorData(IList data) { - if (data == null) - { - throw new ArgumentNullException(nameof(data)); - } + ArgumentNullException.ThrowIfNull(data); if (data.Count < 8) { diff --git a/PModAqs/Sensor/VersionInfo.cs b/PModAqs/Sensor/VersionInfo.cs index 58237e9..97444f3 100644 --- a/PModAqs/Sensor/VersionInfo.cs +++ b/PModAqs/Sensor/VersionInfo.cs @@ -33,12 +33,9 @@ internal class VersionInfo { public VersionInfo(IList data) { - if (data == null) - { - throw new ArgumentNullException(nameof(data)); - } + ArgumentNullException.ThrowIfNull(data); - if(data.Count < 6) + if (data.Count < 6) { throw new ArgumentOutOfRangeException(nameof(data), "Must contain 6 bytes"); } diff --git a/PModAqs/Sensor/VersionNumber.cs b/PModAqs/Sensor/VersionNumber.cs index 976f512..ba37eb4 100644 --- a/PModAqs/Sensor/VersionNumber.cs +++ b/PModAqs/Sensor/VersionNumber.cs @@ -31,10 +31,7 @@ internal class VersionNumber { public VersionNumber(IList data) { - if (data == null) - { - throw new ArgumentNullException(nameof(data)); - } + ArgumentNullException.ThrowIfNull(data); if (data.Count < 2) { From 02068f296dff8758cb625e7040a1919631beddd1 Mon Sep 17 00:00:00 2001 From: DerekGn Date: Tue, 21 Jan 2025 20:39:19 +0000 Subject: [PATCH 7/9] Updated command label --- MCP2221IOConsole/Commands/Sram/SramReadSettingsCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MCP2221IOConsole/Commands/Sram/SramReadSettingsCommand.cs b/MCP2221IOConsole/Commands/Sram/SramReadSettingsCommand.cs index a800365..7cb0b8a 100644 --- a/MCP2221IOConsole/Commands/Sram/SramReadSettingsCommand.cs +++ b/MCP2221IOConsole/Commands/Sram/SramReadSettingsCommand.cs @@ -27,7 +27,7 @@ namespace MCP2221IOConsole.Commands.Sram { - [Command(Name="read-settings", Description = "Read MCP2221 SRAM Settings")] + [Command(Name="read-sram-settings", Description = "Read MCP2221 SRAM Settings")] internal class SramReadSettingsCommand : BaseCommand { public SramReadSettingsCommand(IServiceProvider serviceProvider) : base(serviceProvider) From 92cb97d73147b766cb7ff9812d6764d49c202837 Mon Sep 17 00:00:00 2001 From: DerekGn Date: Tue, 21 Jan 2025 21:17:52 +0000 Subject: [PATCH 8/9] Handle IO exception after reset Updated version numbers --- MCP2221IO/Device.cs | 4 ++++ MCP2221IO/MCP2221IO.csproj | 11 ++++++----- MCP2221IOConsole/Commands/BaseCommand.cs | 1 + MCP2221IOConsole/MCP2221IOConsole.csproj | 9 +++++---- MCP2221IOConsole/Properties/launchSettings.json | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/MCP2221IO/Device.cs b/MCP2221IO/Device.cs index eaa0432..0caf983 100644 --- a/MCP2221IO/Device.cs +++ b/MCP2221IO/Device.cs @@ -671,6 +671,10 @@ private void HandleOperationExecution(string className, Action operation, [Calle memberName, sw.Elapsed.TotalMilliseconds); } + catch(IOException ex) + { + _logger.LogInformation(ex, "An exception occurred executing [{ClassName}].[{MemberName}]", className, memberName); + } catch (Exception ex) { _logger.LogError(ex, "An exception occurred executing [{ClassName}].[{MemberName}]", className, memberName); diff --git a/MCP2221IO/MCP2221IO.csproj b/MCP2221IO/MCP2221IO.csproj index 0c72219..d688e30 100644 --- a/MCP2221IO/MCP2221IO.csproj +++ b/MCP2221IO/MCP2221IO.csproj @@ -3,12 +3,13 @@ net8.0 enable - 3.0.0 + 4.0.0 - 1.0.0 Initial Version - 2.0.0 Upgraded to .net 7 - 3.0.0 Upgraded to .net 8 - + 1.0.0 Initial Version + 2.0.0 Upgraded to .net 7 + 3.0.0 Upgraded to .net 8 + 4.0.0 Added dnc byte to some command streams + Derek Goslin Derek Goslin README.md diff --git a/MCP2221IOConsole/Commands/BaseCommand.cs b/MCP2221IOConsole/Commands/BaseCommand.cs index dcca3f3..88b3024 100644 --- a/MCP2221IOConsole/Commands/BaseCommand.cs +++ b/MCP2221IOConsole/Commands/BaseCommand.cs @@ -28,6 +28,7 @@ using MCP2221IO.Usb; using Microsoft.Extensions.Logging; using System; +using System.IO; namespace MCP2221IOConsole.Commands { diff --git a/MCP2221IOConsole/MCP2221IOConsole.csproj b/MCP2221IOConsole/MCP2221IOConsole.csproj index 22a372f..4455a81 100644 --- a/MCP2221IOConsole/MCP2221IOConsole.csproj +++ b/MCP2221IOConsole/MCP2221IOConsole.csproj @@ -16,10 +16,11 @@ https://github.com/DerekGn/MCP2221IO https://github.com/DerekGn/MCP2221IO - 1.0.0 Initial Version - 2.0.0 Upgraded to .net 7 - 3.0.0 Upgraded to .net 8 - + 1.0.0 Initial Version + 2.0.0 Upgraded to .net 7 + 3.0.0 Upgraded to .net 8 + 4.0.0 Updated to new version of MCP2221IO + 3.0.0 diff --git a/MCP2221IOConsole/Properties/launchSettings.json b/MCP2221IOConsole/Properties/launchSettings.json index 288b9bc..2bbf0f4 100644 --- a/MCP2221IOConsole/Properties/launchSettings.json +++ b/MCP2221IOConsole/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "MCP2221IOConsole": { "commandName": "Project", - "commandLineArgs": "status read-status" + "commandLineArgs": "reset" } } } \ No newline at end of file From 93495a51523e69d299a793a5341f68dd163365ba Mon Sep 17 00:00:00 2001 From: DerekGn Date: Wed, 22 Jan 2025 09:54:09 +0000 Subject: [PATCH 9/9] Updated readme Removed ado pipeline --- README.md | 2 +- azure-pipelines.yml | 57 --------------------------------------------- 2 files changed, 1 insertion(+), 58 deletions(-) delete mode 100644 azure-pipelines.yml diff --git a/README.md b/README.md index 6588af3..1dc31ac 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://derekgn.visualstudio.com/GitHub/_apis/build/status/DerekGn.MCP2221IO?branchName=main)](https://derekgn.visualstudio.com/GitHub/_build/latest?definitionId=12&branchName=main) +![GitHub Actions](https://github.com/DerekGn/MCP2221IO/actions/workflows/build.yml/badge.svg) [![NuGet Badge](https://buildstats.info/nuget/MCP2221IO)](https://www.nuget.org/packages/MCP2221IO/) diff --git a/azure-pipelines.yml b/azure-pipelines.yml deleted file mode 100644 index b1b6ac9..0000000 --- a/azure-pipelines.yml +++ /dev/null @@ -1,57 +0,0 @@ -variables: -- name: BuildParameters.solution - value: '**\*.sln' -- name: TestProject - value: '**/*UnitTests.csproj' - -name: $(Major).$(Minor).$(Patch).$(Build) - -trigger: -- main - -pool: - vmImage: windows-latest - -steps: - - task: DotNetCoreCLI@2 - displayName: "Build Solution" - inputs: - command: 'build' - projects: $(BuildParameters.solution) - configuration: $(BuildConfiguration) - - task: DotNetCoreCLI@2 - displayName: "Run Unit Tests" - inputs: - command: 'test' - projects: $(TestProject) - arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"' - - task: PublishCodeCoverageResults@1 - inputs: - codeCoverageTool: 'Cobertura' - summaryFileLocation: $(Agent.TempDirectory)/*/coverage.cobertura.xml # using ** instead of * finds duplicate coverage -# Workaraound for the pack command not accepting arguments -# https://github.com/microsoft/azure-pipelines-tasks/issues/11640 - - task: DotNetCoreCLI@2 - displayName: Pack - inputs: - command: custom - custom: pack - arguments: > - MCP2221IO.sln - --output $(Build.ArtifactStagingDirectory) - -p:PackageVersion=$(PackageVersion) - -p:Configuration=$(BuildConfiguration) - -p:Version=$(PackageVersion) - -p:AssemblyVersion=$(Build.BuildNumber) - - task: GitHubRelease@1 - displayName: "Publish Relase To GitHub" - inputs: - gitHubConnection: 'GitHub PAT' - repositoryName: '$(Build.Repository.Name)' - action: 'create' - target: '$(Build.SourceVersion)' - tagSource: 'userSpecifiedTag' - tag: 'v$(Build.BuildNumber)' - title: 'v$(Build.BuildNumber) - $(BuildConfiguration) - $(Build.SourceBranchName)' - changeLogCompareToRelease: 'lastFullRelease' - changeLogType: 'commitBased'