Skip to content

Commit

Permalink
Apply refined code style and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Amarok79 committed Jun 30, 2024
1 parent 6d55079 commit 0eca689
Show file tree
Hide file tree
Showing 47 changed files with 4,052 additions and 1,804 deletions.
3,927 changes: 3,260 additions & 667 deletions .editorconfig

Large diffs are not rendered by default.

59 changes: 38 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@
This library is available as NuGet package:
[InlayTester.Drivers.FeigReader](https://www.nuget.org/packages/InlayTester.Drivers.FeigReader/)

The package provides strong-named binaries for .NET Standard 2.0, .NET 6.0, and .NET 7.0. Tests are performed with .NET Framework 4.8, .NET 6.0, .NET 7.0, and .NET 8.0.
The package provides strong-named binaries for .NET Standard 2.0, .NET 6.0, and .NET 7.0. Tests are performed with .NET
Framework 4.8, .NET 6.0, .NET 7.0, and .NET 8.0.

For development, you need *Visual Studio 2022*. For running the tests, you need to install [com0com](https://sourceforge.net/projects/com0com/) and set up a serial port pair with names "COMA" and "COMB". This virtual serial port pair is used throughout unit tests.

The library uses [Amarok79/InlayTester.Shared.Transports](https://github.com/Amarok79/InlayTester.Shared.Transports) under the hood, which again depends on the excellent [jcurl/SerialPortStream](https://github.com/jcurl/SerialPortStream) for serial communication.
For development, you need *Visual Studio 2022*. For running the tests, you need to
install [com0com](https://sourceforge.net/projects/com0com/) and set up a serial port pair with names "COMA" and "COMB".
This virtual serial port pair is used throughout unit tests.

The library uses [Amarok79/InlayTester.Shared.Transports](https://github.com/Amarok79/InlayTester.Shared.Transports)
under the hood, which again depends on the excellent [jcurl/SerialPortStream](https://github.com/jcurl/SerialPortStream)
for serial communication.

# Supported Feig RFID Reader/Modules

In general, all Feig RFID reader/modules are supported that run Feig `s standard or advanced protocol.

Specifically, following readers/modules have been extensively tested:

- CPR40.xx
- CPR74.xx
- ISC.M02


# Supported RFID Transponders

The provided `Inventory()` API supports following transponders:

- ISO14443A
- ISO14443B
- ISO15693
Expand All @@ -40,12 +45,12 @@ The provided `Inventory()` API supports following transponders:
- Innovatron
- FeliCa


# Documentation

### Create, Open, Close

To communicate with a Feig RFID reader/module connected via RS232, you first have to instantiate a **IFeigReader**. This is done via factory method **FeigReader.Create(..)**, which requires an instance of **FeigReaderSettings**.
To communicate with a Feig RFID reader/module connected via RS232, you first have to instantiate a **IFeigReader**. This
is done via factory method **FeigReader.Create(..)**, which requires an instance of **FeigReaderSettings**.

````cs
var settings = new FeigReaderSettings {
Expand All @@ -61,16 +66,19 @@ To communicate with a Feig RFID reader/module connected via RS232, you first hav
Protocol = FeigProtocol.Advanced,
Timeout = TimeSpan.FromMilliseconds(500),
};

using (IFeigReader reader = FeigReader.Create(settings))
{
// ...
}
````

The settings are used to provide configuration about serial communication (**SerialTransportSettings**) to the reader, but also to provide Feig-specific global settings like **Address**, **Protocol** and **Timeout**.
The settings are used to provide configuration about serial communication (**SerialTransportSettings**) to the reader,
but also to provide Feig-specific global settings like **Address**, **Protocol** and **Timeout**.

So far, we only configured and created an instance of our reader in code. The specified serial port hasn't been opened yet. To open communication you have to call **Open()** on the reader. If necessary, you can **Close()** and re-open the communication transport multiple times on the same **IFeigReader** instance.
So far, we only configured and created an instance of our reader in code. The specified serial port hasn't been opened
yet. To open communication you have to call **Open()** on the reader. If necessary, you can **Close()** and re-open the
communication transport multiple times on the same **IFeigReader** instance.

````cs
using (IFeigReader reader = FeigReader.Create(settings))
Expand All @@ -83,10 +91,10 @@ So far, we only configured and created an instance of our reader in code. The sp

Don't forget to dispose the **IFeigReader** at the end.


### Transfer

Now, to communicate with the reader/module, we perform a transfer operation, which sends a request to the reader and then waits for a response.
Now, to communicate with the reader/module, we perform a transfer operation, which sends a request to the reader and
then waits for a response.

````cs
var request = new FeigRequest {
Expand All @@ -99,15 +107,20 @@ Now, to communicate with the reader/module, we perform a transfer operation, whi
.ConfigureAwait(false);
````

Here we first instantiate a **FeigRequest** and then call the async **Transfer(..)**. The method overload uses *protocol* and *timeout* from global settings supplied earlier during reader construction. To override global settings for this single transfer operation only, you can provide them as additional arguments. You can even provide a cancellation token to cancel the transfer operation at any time.
Here we first instantiate a **FeigRequest** and then call the async **Transfer(..)**. The method overload uses
*protocol* and *timeout* from global settings supplied earlier during reader construction. To override global settings
for this single transfer operation only, you can provide them as additional arguments. You can even provide a
cancellation token to cancel the transfer operation at any time.

````cs
FeigTransferResult result = await reader.Transfer(
request, FeigProtocol.Standard, TimeSpan.FromMilliseconds(1000), cancellationToken)
.ConfigureAwait(false);
````

Now, **Transfer(..)** never throws exceptions for timeout, cancellation or errors. Instead, it returns a specific result object that contains detailed information about the transfer operation. You can use it to determine whether the transfer operation succeeded as shown in the following code snippet.
Now, **Transfer(..)** never throws exceptions for timeout, cancellation or errors. Instead, it returns a specific result
object that contains detailed information about the transfer operation. You can use it to determine whether the transfer
operation succeeded as shown in the following code snippet.

````cs
if (result.Status == FeigTransferStatus.Canceled)
Expand Down Expand Up @@ -139,12 +152,14 @@ Now, **Transfer(..)** never throws exceptions for timeout, cancellation or error
}
````

Error handling seems a bit tedious. But, you won't need to do it yourself. **Transfer(..)** represents a low-level method that gives you full control over transfer operations. In general, you will use other high-level methods better suited for most cases. It's just that you know you can do it that way.

Error handling seems a bit tedious. But, you won't need to do it yourself. **Transfer(..)** represents a low-level
method that gives you full control over transfer operations. In general, you will use other high-level methods better
suited for most cases. It's just that you know you can do it that way.

### Execute

**Execute(..)** is one of those higher-level methods that does this result-code checking for you, and that throws appropriate exceptions in certain error situations. On success, the method returns the received response.
**Execute(..)** is one of those higher-level methods that does this result-code checking for you, and that throws
appropriate exceptions in certain error situations. On success, the method returns the received response.

````cs
var request = new FeigRequest {
Expand All @@ -155,11 +170,13 @@ Error handling seems a bit tedious. But, you won't need to do it yourself. **Tra

var response = await reader.Execute(request)
.ConfigureAwait(false);

var data = response.Data;
````

In case of timeout, a **TimeoutException** is thrown. In case of cancellation an **OperationCanceledException**. Otherwise, if a communication error occurred or the reader/module returned an error code, an exception of type **FeigException** is thrown. In all other cases, the received **FeigResponse** is returned.
In case of timeout, a **TimeoutException** is thrown. In case of cancellation an **OperationCanceledException**.
Otherwise, if a communication error occurred or the reader/module returned an error code, an exception of type *
*FeigException** is thrown. In all other cases, the received **FeigResponse** is returned.

Regarding the previous code snippet. There exists another overload that allows you to write this in a much shorter way.

Expand All @@ -170,10 +187,10 @@ Regarding the previous code snippet. There exists another overload that allows y

This overload constructs the necessary **FeigRequest** internally.


### Common Commands

Most Feig RFID reader/modules support a set of common commands. These commands are already implemented and exposed as methods on the **IFeigReader** interface.
Most Feig RFID reader/modules support a set of common commands. These commands are already implemented and exposed as
methods on the **IFeigReader** interface.

Following commands are supported out-of-the-box:

Expand Down
2 changes: 1 addition & 1 deletion nuget.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json"/>
</packageSources>
</configuration>
2 changes: 1 addition & 1 deletion src/Debug.InlayTester.Drivers.FeigReader/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
</startup>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.11" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0"/>
<PackageReference Include="NLog" Version="5.3.2"/>
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.11"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\InlayTester.Drivers.FeigReader\InlayTester.Drivers.FeigReader.csproj" />
<ProjectReference Include="..\InlayTester.Drivers.FeigReader\InlayTester.Drivers.FeigReader.csproj"/>
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions src/Debug.InlayTester.Drivers.FeigReader/NLog.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false">
Expand Down Expand Up @@ -58,8 +58,8 @@
</targets>

<rules>
<logger name="*" enabled="true" minlevel="Trace" writeTo="FileAsync" />
<logger name="*" enabled="false" minlevel="Trace" writeTo="RealtimeAsync" />
<logger name="*" enabled="true" minlevel="Trace" writeTo="FileAsync"/>
<logger name="*" enabled="false" minlevel="Trace" writeTo="RealtimeAsync"/>
</rules>

</nlog>
16 changes: 8 additions & 8 deletions src/Debug.InlayTester.Drivers.FeigReader/Program.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.Diagnostics;
Expand Down Expand Up @@ -33,16 +33,16 @@ public static async Task Main()

var settings = new FeigReaderSettings {
TransportSettings = new SerialTransportSettings {
PortName = "COM3",
Baud = 38400,
DataBits = 8,
Parity = Parity.Even,
StopBits = StopBits.One,
PortName = "COM3",
Baud = 38400,
DataBits = 8,
Parity = Parity.Even,
StopBits = StopBits.One,
Handshake = Handshake.None,
},
Address = 255,
Address = 255,
Protocol = FeigProtocol.Advanced,
Timeout = TimeSpan.FromMilliseconds(1500),
Timeout = TimeSpan.FromMilliseconds(1500),
};

using (var reader = FeigReader.Create(settings, log))
Expand Down
2 changes: 1 addition & 1 deletion src/InlayTester.Drivers.FeigReader/AssemblyInfo.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.Runtime.CompilerServices;
Expand Down
30 changes: 15 additions & 15 deletions src/InlayTester.Drivers.FeigReader/Drivers.Feig/CrcCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ Boolean reflectOutput
{
Verify.IsInRange(order, 1, 32, nameof(order));

mOrder = order;
mPolynom = (UInt64)polynom;
mIsDirect = isDirect;
mCrcInit = (UInt64)crcInit;
mCrcXor = (UInt64)crcXor;
mReflectInput = reflectInput;
mOrder = order;
mPolynom = (UInt64)polynom;
mIsDirect = isDirect;
mCrcInit = (UInt64)crcInit;
mCrcXor = (UInt64)crcXor;
mReflectInput = reflectInput;
mReflectOutput = reflectOutput;

_Setup();
Expand All @@ -97,7 +97,7 @@ public Int32 Calculate(in BufferSpan data)

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

_BuildTable();
Expand All @@ -108,15 +108,15 @@ private void _Prepare()
{
UInt64 bit;
UInt64 crc;
Int32 i;
Int32 i;

if (!mIsDirect)
{
crc = mCrcInit;

for (i = 0; i < mOrder; i++)
{
bit = crc & mCrcHighBit;
bit = crc & mCrcHighBit;
crc <<= 1;

if (bit != 0)
Expand All @@ -125,13 +125,13 @@ private void _Prepare()
}
}

crc &= mCrcMask;
mCrcInitDirect = crc;
crc &= mCrcMask;
mCrcInitDirect = crc;
}
else
{
mCrcInitDirect = mCrcInit;
crc = mCrcInit;
crc = mCrcInit;

for (i = 0; i < mOrder; i++)
{
Expand Down Expand Up @@ -181,15 +181,15 @@ private void _BuildTable()
crc = _Reflect(crc, mOrder);
}

crc &= mCrcMask;
mCrcTable[i] = crc;
crc &= mCrcMask;
mCrcTable[i] = crc;
}
}

private static UInt64 _Reflect(UInt64 crc, Int32 bitnum)
{
// reflects the lower 'bitnum' bits of 'crc'
UInt64 j = 1;
UInt64 j = 1;
UInt64 crcout = 0;

for (var i = (UInt64)1 << (bitnum - 1); i != 0; i >>= 1)
Expand Down
Loading

0 comments on commit 0eca689

Please sign in to comment.