Skip to content

Commit

Permalink
- Added a first test for the decoder
Browse files Browse the repository at this point in the history
- Fixed issues in the float and double processing of the ReadBuffer
- Made the PlcValues comparable for equality
  • Loading branch information
chrisdutz committed Dec 15, 2021
1 parent 8ce8821 commit 7a4b8cb
Show file tree
Hide file tree
Showing 29 changed files with 449 additions and 48 deletions.
2 changes: 1 addition & 1 deletion sandbox/plc4net/api/plc4net-api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net452</TargetFramework>
<RootNamespace>org.apache.plc4net</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Apache PLC4X</Authors>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<RootNamespace>org.apache.plc4net</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Apache PLC4X</Authors>
<Copyright>The Apache Software Foundation</Copyright>
<PackageProjectUrl>https://plc4x.apache.org</PackageProjectUrl>
<Title>PLC4Net: Driver: KNXNet/IP: Test</Title>
<PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<PackageVersion>0.8.0-SNAPSHOT</PackageVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\knxnetip\plc4net-driver-knxproj.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using org.apache.plc4net.api.value;
using org.apache.plc4net.drivers.knxnetip.readwrite.model;
using org.apache.plc4net.spi.generation;
using org.apache.plc4net.spi.model.values;
using Xunit;

namespace org.apache.plc4net.test.knxnetip.readwrite.model
{
public class KnxDatapointTests
{
[Fact]
public void DecodeKnxData1()
{
var formatName = "F32";
var input = StrToByteArray("0041b00000");
IPlcValue expected = new PlcREAL(22.0f);

var actual = new KnxDatapoint().Parse(new ReadBuffer(input), formatName);

Assert.Equal(expected, actual);
}

private static byte[] StrToByteArray(string str)
{
var hexIndex = new Dictionary<string, byte>();
for (var i = 0; i <= 255; i++)
{
hexIndex.Add(i.ToString("X2"), (byte) i);
}

var hexRes = new List<byte>();
for (var i = 0; i < str.Length; i += 2)
{
hexRes.Add(hexIndex[str.Substring(i, 2).ToUpper()]);
}
return hexRes.ToArray();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net452</TargetFramework>
<RootNamespace>org.apache.plc4net</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Apache PLC4X</Authors>
Expand Down
29 changes: 0 additions & 29 deletions sandbox/plc4net/plc4net.driver/plc4net.driver.csproj

This file was deleted.

7 changes: 7 additions & 0 deletions sandbox/plc4net/plc4net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "drivers", "drivers", "{35D8
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "plc4net-driver-knxproj", "drivers\knxnetip\plc4net-driver-knxproj.csproj", "{640E582A-68DF-4E51-B019-AE5774F31B54}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "plc4net-driver-knxproj-test", "drivers\knxnetip-test\plc4net-driver-knxproj-test.csproj", "{E9497B75-55B6-4C14-AAFB-DB93D0C88166}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -45,8 +47,13 @@ Global
{640E582A-68DF-4E51-B019-AE5774F31B54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{640E582A-68DF-4E51-B019-AE5774F31B54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{640E582A-68DF-4E51-B019-AE5774F31B54}.Release|Any CPU.Build.0 = Release|Any CPU
{E9497B75-55B6-4C14-AAFB-DB93D0C88166}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9497B75-55B6-4C14-AAFB-DB93D0C88166}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9497B75-55B6-4C14-AAFB-DB93D0C88166}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9497B75-55B6-4C14-AAFB-DB93D0C88166}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{640E582A-68DF-4E51-B019-AE5774F31B54} = {35D8C56A-A721-46B3-A32C-71FCF212E85D}
{E9497B75-55B6-4C14-AAFB-DB93D0C88166} = {35D8C56A-A721-46B3-A32C-71FCF212E85D}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions sandbox/plc4net/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<artifactId>plc4x-protocols-knxnetip</artifactId>
<classifier>tests</classifier>
<type>test-jar</type>
<outputDirectory>${project.basedir}/drivers/knxnetip/generated/testing/assets</outputDirectory>
<outputDirectory>${project.basedir}/drivers/knxnetip-test/resources</outputDirectory>
<includes>**/*.xml</includes>
<excludes>META-INF/**,org/**</excludes>
</artifactItem>
Expand All @@ -85,7 +85,7 @@
<protocolName>knxnetip</protocolName>
<languageName>C#</languageName>
<outputFlavor>read-write</outputFlavor>
<outputDir>${project.basedir}/drivers/knxnetip/generated/sources</outputDir>
<outputDir>${project.basedir}/drivers/knxnetip/src</outputDir>
</configuration>
</execution>
</executions>
Expand Down
2 changes: 1 addition & 1 deletion sandbox/plc4net/spi/plc4net-spi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net452</TargetFramework>
<RootNamespace>org.apache.plc4net</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Apache PLC4X</Authors>
Expand Down
26 changes: 13 additions & 13 deletions sandbox/plc4net/spi/spi/generation/ReadBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public byte ReadByte(int bitLength)
{
throw new ArgumentOutOfRangeException();
}
return (byte) _reader.ReadInt(0, bitLength);
return (byte) _reader.ReadInt(bitLength);
}

public ushort ReadUshort(int bitLength)
Expand All @@ -85,7 +85,7 @@ public ushort ReadUshort(int bitLength)
{
throw new ArgumentOutOfRangeException();
}
return (ushort) _reader.ReadInt(0, bitLength);
return (ushort) _reader.ReadInt(bitLength);
}

public uint ReadUint(int bitLength)
Expand All @@ -94,7 +94,7 @@ public uint ReadUint(int bitLength)
{
throw new ArgumentOutOfRangeException();
}
return (uint) _reader.ReadInt(0, bitLength);
return (uint) _reader.ReadInt(bitLength);
}

public ulong ReadUlong(int bitLength)
Expand All @@ -107,9 +107,9 @@ public ulong ReadUlong(int bitLength)
ulong firstInt = 0;
if (bitLength > 32)
{
firstInt = (ulong) _reader.ReadInt(0, bitLength - 32) << 32;
firstInt = (ulong) _reader.ReadInt(bitLength - 32) << 32;
}
return firstInt | (ulong) _reader.ReadInt(0, bitLength);
return firstInt | (ulong) _reader.ReadInt(bitLength);
}

public sbyte ReadSbyte(int bitLength)
Expand All @@ -118,7 +118,7 @@ public sbyte ReadSbyte(int bitLength)
{
throw new ArgumentOutOfRangeException();
}
return (sbyte) _reader.ReadInt(0, bitLength);
return (sbyte) _reader.ReadInt(bitLength);
}

public short ReadShort(int bitLength)
Expand All @@ -127,7 +127,7 @@ public short ReadShort(int bitLength)
{
throw new ArgumentOutOfRangeException();
}
return (short) _reader.ReadInt(0, bitLength);
return (short) _reader.ReadInt(bitLength);
}

public int ReadInt(int bitLength)
Expand All @@ -136,7 +136,7 @@ public int ReadInt(int bitLength)
{
throw new ArgumentOutOfRangeException();
}
return (int) _reader.ReadInt(0, bitLength);
return _reader.ReadInt(bitLength);
}

public long ReadLong(int bitLength)
Expand All @@ -149,16 +149,16 @@ public long ReadLong(int bitLength)
long firstInt = 0;
if (bitLength > 32)
{
firstInt = (long) _reader.ReadInt(0, bitLength - 32) << 32;
firstInt = (long) _reader.ReadInt(bitLength - 32) << 32;
}
return firstInt | (long) _reader.ReadInt(0, bitLength);
return firstInt | (long) _reader.ReadInt(bitLength);
}

public float ReadFloat(bool signed, int exponentBitLength, int mantissaBitLength)
{
if (signed && exponentBitLength == 8 && mantissaBitLength == 23)
{
return Convert.ToSingle(ReadInt(32));
return BitConverter.ToSingle(BitConverter.GetBytes(ReadInt(32)), 0);
}
// This is the format as described in the KNX spec ... it's not a real half precision floating point.
if (signed && exponentBitLength == 4 && mantissaBitLength == 11)
Expand All @@ -185,11 +185,11 @@ public double ReadDouble(bool signed, int exponentBitLength, int mantissaBitLeng
{
if (signed && exponentBitLength == 8 && mantissaBitLength == 23)
{
return Convert.ToDouble(ReadInt(32));
return BitConverter.ToDouble(BitConverter.GetBytes(ReadInt(32)), 0);
}
if (signed && exponentBitLength == 11 && mantissaBitLength == 52)
{
return Convert.ToDouble(ReadLong(64));
return BitConverter.ToDouble(BitConverter.GetBytes(ReadLong(64)), 0);
}
throw new NotImplementedException("This encoding is currently not supported");
}
Expand Down
19 changes: 19 additions & 0 deletions sandbox/plc4net/spi/spi/model/values/PlcBOOL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,24 @@ public PlcBOOL(bool value)
{
this.value = value;
}

protected bool Equals(PlcBOOL other)
{
return value == other.value;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PlcBOOL) obj);
}

public override int GetHashCode()
{
return value.GetHashCode();
}

}
}
19 changes: 19 additions & 0 deletions sandbox/plc4net/spi/spi/model/values/PlcBYTE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,24 @@ public PlcBYTE(byte value)
GetBoolAt(6), GetBoolAt(7)
};
}

protected bool Equals(PlcBYTE other)
{
return value == other.value;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PlcBYTE) obj);
}

public override int GetHashCode()
{
return value.GetHashCode();
}

}
}
19 changes: 19 additions & 0 deletions sandbox/plc4net/spi/spi/model/values/PlcCHAR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,24 @@ public PlcCHAR(char value)
{
this.value = value;
}

protected bool Equals(PlcCHAR other)
{
return value == other.value;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PlcCHAR) obj);
}

public override int GetHashCode()
{
return value.GetHashCode();
}

}
}
19 changes: 19 additions & 0 deletions sandbox/plc4net/spi/spi/model/values/PlcDATE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,24 @@ public PlcDATE(DateTime value)
{
this.value = value;
}

protected bool Equals(PlcDATE other)
{
return value.Equals(other.value);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PlcDATE) obj);
}

public override int GetHashCode()
{
return value.GetHashCode();
}

}
}
Loading

0 comments on commit 7a4b8cb

Please sign in to comment.