Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Amarok79 committed Jun 28, 2024
1 parent 086ed98 commit b405d01
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 62 deletions.
16 changes: 8 additions & 8 deletions src/InlayTester.Drivers.FeigReader/Drivers.Feig/CrcCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

using System;
using Amarok.Contracts;
using Amarok.Shared;


namespace InlayTester.Shared;
namespace InlayTester.Drivers.Feig;


/// <summary>
Expand Down Expand Up @@ -97,8 +97,8 @@ public Int32 Calculate(in BufferSpan data)

private void _Setup()
{
mCrcMask = ( ( ( (UInt64)1 << ( mOrder - 1 ) ) - 1 ) << 1 ) | 1;
mCrcHighBit = (UInt64)1 << ( mOrder - 1 );
mCrcMask = ((((UInt64)1 << (mOrder - 1)) - 1) << 1) | 1;
mCrcHighBit = (UInt64)1 << (mOrder - 1);

_BuildTable();
_Prepare();
Expand Down Expand Up @@ -192,9 +192,9 @@ private static UInt64 _Reflect(UInt64 crc, Int32 bitnum)
UInt64 j = 1;
UInt64 crcout = 0;

for (var i = (UInt64)1 << ( bitnum - 1 ); i != 0; i >>= 1)
for (var i = (UInt64)1 << (bitnum - 1); i != 0; i >>= 1)
{
if (( crc & i ) != 0)
if ((crc & i) != 0)
{
crcout |= j;
}
Expand All @@ -218,7 +218,7 @@ private UInt64 _Calculate(Byte[] buffer, Int32 offset, Int32 count)
{
for (var i = offset; i < offset + count; i++)
{
crc = ( crc << 8 ) ^ mCrcTable[( ( crc >> ( mOrder - 8 ) ) & 0xff ) ^ buffer[i]];
crc = (crc << 8) ^ mCrcTable[((crc >> (mOrder - 8)) & 0xff) ^ buffer[i]];
}
}
else
Expand All @@ -227,7 +227,7 @@ private UInt64 _Calculate(Byte[] buffer, Int32 offset, Int32 count)

for (var i = offset; i < offset + count; i++)
{
crc = ( crc >> 8 ) ^ mCrcTable[( crc & 0xff ) ^ buffer[i]];
crc = (crc >> 8) ^ mCrcTable[(crc & 0xff) ^ buffer[i]];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

using System;
using System.IO;
Expand Down Expand Up @@ -203,7 +203,7 @@ public Task<FeigTransferResult> Transfer(

/// <summary>
/// Executes the supplied command by sending a request to the reader/module and then waits for a
/// corresponding response from the reader/module or for timeout, whatever comes first. This
/// corresponding response from the reader/module or for timeout, whatever comes first. These
/// methods throws appropriate exceptions for timeout, cancellation or failed operations.
/// </summary>
///
Expand Down Expand Up @@ -252,7 +252,7 @@ public async Task<FeigResponse> Execute(

if (result.Status == FeigTransferStatus.Success)
{
if (result!.Response!.Status is FeigStatus.OK or FeigStatus.NoTransponder or FeigStatus.MoreData)
if (result.Response!.Status is FeigStatus.OK or FeigStatus.NoTransponder or FeigStatus.MoreData)
{
return result.Response; // success
}
Expand Down Expand Up @@ -303,7 +303,7 @@ public async Task<FeigResponse> Execute(

/// <summary>
/// Executes the supplied command by sending a request to the reader/module and then waits for a
/// corresponding response from the reader/module or for timeout, whatever comes first. This
/// corresponding response from the reader/module or for timeout, whatever comes first. These
/// methods throws appropriate exceptions for timeout, cancellation or failed operations.
/// </summary>
///
Expand Down Expand Up @@ -486,7 +486,7 @@ public async Task ResetCPU(TimeSpan? timeout = null, CancellationToken cancellat

/// <summary>
/// The RF-field of the Reader antenna is switched off for approx. 6 ms. Thus, all transponders
/// which are within the antenna field of the reader will be reset to their base setting. After a
/// which are within the antenna field of the reader will be reset to their base setting. After an
/// RF Reset a transponder which is located within the field has to be re-selected.
/// </summary>
///
Expand Down Expand Up @@ -551,7 +551,7 @@ public async Task ResetRF(TimeSpan? timeout = null, CancellationToken cancellati
/// </summary>
///
/// <param name="flag">
/// A value indicating which RF field should switched on or off. Specify 0x00 to switch off all
/// A value indicating which RF field should switch on or off. Specify 0x00 to switch off all
/// antennas and 0x01 to switch on the first antenna. Look up more information in the reader's
/// manual.
/// </param>
Expand Down Expand Up @@ -674,7 +674,7 @@ public async Task<FeigSoftwareInfo> GetSoftwareInfo(
FirmwareVersion = new Version(response.Data[0], response.Data[1], response.Data[2]),
HardwareType = response.Data[3],
ReaderType = (FeigReaderType)response.Data[4],
SupportedTransponders = ( response.Data[5] << 8 ) | response.Data[6],
SupportedTransponders = (response.Data[5] << 8) | response.Data[6],
};

#region (logging)
Expand Down Expand Up @@ -764,7 +764,7 @@ public async Task<BufferSpan> ReadConfiguration(

var addr = (Byte)0x00;
addr |= (Byte)location;
addr |= (Byte)( block & 0x3F );
addr |= (Byte)(block & 0x3F);

mRequestBuffer[0] = addr;
var data = BufferSpan.From(mRequestBuffer, 0, 1);
Expand Down Expand Up @@ -873,15 +873,15 @@ async Task writeConfiguration()

var addr = (Byte)0x00;
addr |= (Byte)location;
addr |= (Byte)( block & 0x3F );
addr |= (Byte)(block & 0x3F);

mRequestBuffer[0] = addr;

Buffer.BlockCopy(data.Buffer, data.Offset, mRequestBuffer, 1, data.Count);

var cfgdata = BufferSpan.From(mRequestBuffer, 0, 1 + data.Count);
var cfgData = BufferSpan.From(mRequestBuffer, 0, 1 + data.Count);

var response = await Execute(FeigCommand.WriteConfiguration, cfgdata, timeout, cancellationToken)
var response = await Execute(FeigCommand.WriteConfiguration, cfgData, timeout, cancellationToken)
.ConfigureAwait(false);

#region (logging)
Expand Down Expand Up @@ -1022,7 +1022,7 @@ public async Task SaveConfiguration(

#endregion

mRequestBuffer[0] = (Byte)( block & 0x3F );
mRequestBuffer[0] = (Byte)(block & 0x3F);

var data = BufferSpan.From(mRequestBuffer, 0, 1);

Expand Down Expand Up @@ -1188,7 +1188,7 @@ public async Task ResetConfiguration(

var addr = (Byte)0x00;
addr |= (Byte)location;
addr |= (Byte)( block & 0x3F );
addr |= (Byte)(block & 0x3F);

mRequestBuffer[0] = addr;

Expand Down Expand Up @@ -1271,9 +1271,7 @@ public async Task ResetConfiguration(

var rspDat = response.Data;

var result = response.Status == FeigStatus.NoTransponder
? Array.Empty<FeigTransponder>()
: Inventory_Parse(ref rspDat);
var result = response.Status == FeigStatus.NoTransponder ? [] : Inventory_Parse(ref rspDat);

#region (logging)

Expand All @@ -1289,7 +1287,7 @@ public async Task ResetConfiguration(

#endregion

return ( result, response );
return (result, response);
}

internal static FeigTransponder[] Inventory_Parse(ref BufferSpan data)
Expand Down Expand Up @@ -1335,7 +1333,7 @@ private static FeigTransponder _Inventory_ParseSingle(ref BufferSpan data)
internal static FeigTransponder Inventory_Parse_ISO14443A(ref BufferSpan data)
{
var info = data[0];
var length = ( info & 0x04 ) != 0 ? 10 : 7;
var length = (info & 0x04) != 0 ? 10 : 7;
var identifier = data.Slice(2, length);

data = data.Discard(2 + length);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

using System;
using System.Threading;
Expand Down Expand Up @@ -111,7 +111,7 @@ CancellationToken cancellationToken

#endregion

( (TaskCompletionSource<FeigTransferResult>)completionSource! ).TrySetResult(
((TaskCompletionSource<FeigTransferResult>)completionSource!).TrySetResult(
FeigTransferResult.Canceled(mRequest)
);
},
Expand All @@ -137,7 +137,7 @@ CancellationToken cancellationToken

#endregion

( (TaskCompletionSource<FeigTransferResult>)completionSource! ).TrySetResult(
((TaskCompletionSource<FeigTransferResult>)completionSource!).TrySetResult(
FeigTransferResult.Timeout(mRequest)
);
},
Expand Down Expand Up @@ -193,7 +193,7 @@ private void _HandleReceived(BufferSpan data)
{
_CompleteWithError(result);
}
else if (result!.Response!.Command != mRequest!.Command)
else if (result.Response!.Command != mRequest!.Command)
{
_CompleteWithUnexpectedResponse(result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

// ReSharper disable InconsistentNaming

using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

using System;
using Amarok.Shared;
using InlayTester.Shared;


namespace InlayTester.Drivers.Feig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

// ReSharper disable InconsistentNaming

using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

// ReSharper disable InconsistentNaming

using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

// ReSharper disable InconsistentNaming

using System;

Expand Down
14 changes: 7 additions & 7 deletions src/InlayTester.Drivers.FeigReader/Drivers.Feig/FeigRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

using System;
using System.Text;
Expand Down Expand Up @@ -89,8 +89,8 @@ private BufferSpan _ToStandardProtocolFrame()

var crc = FeigChecksum.Calculate(BufferSpan.From(frame, 0, frameLength - 2));

frame[frameLength - 2] = (Byte)( crc & 0xff );
frame[frameLength - 1] = (Byte)( crc >> 8 );
frame[frameLength - 2] = (Byte)(crc & 0xff);
frame[frameLength - 1] = (Byte)(crc >> 8);

return BufferSpan.From(frame, frameLength);
}
Expand All @@ -101,17 +101,17 @@ private BufferSpan _ToAdvancedProtocolFrame()
var frame = new Byte[frameLength];

frame[0] = 0x02;
frame[1] = (Byte)( frameLength >> 8 );
frame[2] = (Byte)( frameLength & 0xff );
frame[1] = (Byte)(frameLength >> 8);
frame[2] = (Byte)(frameLength & 0xff);
frame[3] = Address;
frame[4] = (Byte)Command;

Buffer.BlockCopy(Data.Buffer, Data.Offset, frame, 5, Data.Count);

var crc = FeigChecksum.Calculate(BufferSpan.From(frame, 0, frameLength - 2));

frame[frameLength - 2] = (Byte)( crc & 0xff );
frame[frameLength - 1] = (Byte)( crc >> 8 );
frame[frameLength - 2] = (Byte)(crc & 0xff);
frame[frameLength - 1] = (Byte)(crc >> 8);

return BufferSpan.From(frame, frameLength);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

using System;
using System.Text;
Expand Down Expand Up @@ -105,7 +105,7 @@ private static FeigParseResult _TryParseAdvancedProtocolFrame(in BufferSpan span

var lenHigh = span[1];
var lenLow = span[2];
var frameLength = ( lenHigh << 8 ) | lenLow;
var frameLength = (lenHigh << 8) | lenLow;

if (span.Count < frameLength)
{
Expand All @@ -118,7 +118,7 @@ private static FeigParseResult _TryParseAdvancedProtocolFrame(in BufferSpan span

var crcLow = span[frameLength - 2];
var crcHigh = span[frameLength - 1];
var crc = ( crcHigh << 8 ) | crcLow;
var crc = (crcHigh << 8) | crcLow;

var calcCrc = FeigChecksum.Calculate(BufferSpan.From(span.Buffer, span.Offset, frameLength - 2));

Expand Down Expand Up @@ -166,7 +166,7 @@ private static FeigParseResult _TryParseStandardProtocolFrame(in BufferSpan span

var crcLow = span[frameLength - 2];
var crcHigh = span[frameLength - 1];
var crc = ( crcHigh << 8 ) | crcLow;
var crc = (crcHigh << 8) | crcLow;

var calcCrc = FeigChecksum.Calculate(BufferSpan.From(span.Buffer, span.Offset, frameLength - 2));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

// ReSharper disable InconsistentNaming

using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

using System;
using System.Text;
Expand Down Expand Up @@ -67,7 +67,7 @@ public override String ToString()


/// <summary>
/// Returns a result indicating a successfully transfer operation.
/// Returns a result indicating a successful transfer operation.
/// </summary>
///
/// <exception cref="ArgumentNullException">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

using System;
using System.Collections.Generic;
Expand All @@ -10,7 +10,7 @@ namespace InlayTester.Drivers.Feig;


/// <summary>
/// This type represents a RFID transponder consisting of type and identifier.
/// This type represents an RFID transponder consisting of type and identifier.
/// </summary>
public sealed class FeigTransponder
{
Expand Down Expand Up @@ -53,7 +53,7 @@ public override String ToString()
/// <summary>
/// Returns a string representation for the given transponders.
/// </summary>
public static String ToString(IEnumerable<FeigTransponder> transponders)
public static String ToString(IEnumerable<FeigTransponder>? transponders)
{
if (transponders == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright (c) 2022, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

// ReSharper disable InconsistentNaming

using System;

Expand Down
Loading

0 comments on commit b405d01

Please sign in to comment.