d50ee809e3
Native LSV2 TCP transport (default port 19000; no Java/Python/vendor SDK), ported from the ignitionoee v0.20.3 reference driver: - Lsv2ReadOnlyPolicy: payload-aware outbound allowlist (A_LG INSPECT/DNC/DATA, R_VR, R_PR, R_RI 22-28/30-34/51, bounded R_MB, R_DP), applied in Lsv2Codec.Frame before any byte is written; no public raw-command API. - Lsv2Client: serialized transactions, one monotonic deadline per transaction, EOF/oversize/ unexpected/trickle close the client; Close never waits on the gate. - Lsv2Data / HeidenhainBindingCodec: reference decoders, R_PR byte-capacity + element-index rules, next-area cap, DATA Boolean 00/01/FF, strict non-truncating coercion. - HeidenhainSession: per-batch wire cache, refusal suppression, provisional DNC denial retry, alarm cursor (57 = end, overflow = BadOutOfRange), capability strings. - HeidenhainDeviceRuntime: one loop per control, generation-guarded publication, all-node BadNoCommunication on comms loss, bounded exponential reconnect. - HeidenhainDriver: cached ReadAsync, push subscriptions deduped on (value,status), static catalog discovery (no connect), host status, diagnostics, skipped-tag report. No IWritable. - Probe (INSPECT login + version) and factory registered in DriverFactoryBootstrap (probe on admin nodes too); DriverTypeNames.Heidenhain. Tests: 159 unit tests against an in-process LSV2 simulator incl. the Java fixtures' captured iTNC 530 replies as golden vectors; env-gated LiveIntegration category for real hardware.
184 lines
10 KiB
C#
184 lines
10 KiB
C#
using System.Buffers.Binary;
|
|
using System.Text;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Lsv2;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests.Simulator;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests;
|
|
|
|
/// <summary>Decoder regression ported from the reference <c>Lsv2DataTest</c> + <c>HeidenhainBindingsTest</c>.</summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class Lsv2DataTests
|
|
{
|
|
private static byte[] T(string s) => Encoding.Latin1.GetBytes(s);
|
|
|
|
[Fact]
|
|
public void Version_and_program_preserve_native_values()
|
|
{
|
|
Lsv2Data.Versions(T("iTNC 530\0NC\0PLC\0OPT\0")).Model.ShouldBe("iTNC 530");
|
|
Lsv2Data.Versions(T("TNC\0NC\0PLC\0SPLC\0OPT\0")).Options.ShouldBe("OPT");
|
|
var program = new byte[10];
|
|
BinaryPrimitives.WriteInt32BigEndian(program, -1);
|
|
T("A\0B\0\0\0").CopyTo(program, 4);
|
|
var p = Lsv2Data.Program(program);
|
|
p.Line.ShouldBe(4294967295L);
|
|
p.Current.ShouldBe("B");
|
|
Lsv2Data.U16([0xFF, 0xFE]).ShouldBe(65534);
|
|
}
|
|
|
|
[Fact]
|
|
public void Malformed_strings_and_lengths_are_rejected()
|
|
{
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Versions(T("TNC\0NC\0PLC\0OPT")));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Versions(T("TNC\0NC\0")));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Program(new byte[4]));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Overrides(new byte[11]));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Layout(new byte[119]));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.U16(new byte[3]));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Alarm(new byte[8]));
|
|
}
|
|
|
|
[Fact]
|
|
public void Empty_version_options_do_not_become_the_optional_splc_field()
|
|
{
|
|
Lsv2Data.Versions(T("iTNC 530\0NC\0PLC\0SPLC\0\0")).Options.ShouldBe("");
|
|
Lsv2Data.Versions(T("iTNC 530\0NC\0PLC\0\0")).Options.ShouldBe("");
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Versions(T("iTNC 530\0NC\0PLC\0SPLC\0OPT\0\0")));
|
|
}
|
|
|
|
[Fact]
|
|
public void Overrides_are_unsigned_hundredths_and_tool_is_mixed_endian()
|
|
{
|
|
var o = new byte[12];
|
|
BinaryPrimitives.WriteInt32BigEndian(o, 12345);
|
|
BinaryPrimitives.WriteInt32BigEndian(o.AsSpan(4), 8000);
|
|
Lsv2Data.Overrides(o).ShouldBe(new[] { 123.45, 80, 0 });
|
|
var t = new byte[24];
|
|
BinaryPrimitives.WriteInt32BigEndian(t, -1);
|
|
BinaryPrimitives.WriteInt16BigEndian(t.AsSpan(4), 4);
|
|
BinaryPrimitives.WriteInt16BigEndian(t.AsSpan(6), 2);
|
|
BinaryPrimitives.WriteDoubleLittleEndian(t.AsSpan(8), 123.5);
|
|
BinaryPrimitives.WriteDoubleLittleEndian(t.AsSpan(16), 4.25);
|
|
var tool = Lsv2Data.Tool(t);
|
|
tool.Number.ShouldBe(4294967295L);
|
|
tool.Length.ShouldBe(123.5);
|
|
tool.Radius.ShouldBe(4.25);
|
|
Lsv2Data.Tool(new byte[8]).Length.ShouldBeNull();
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Tool(new byte[9]));
|
|
BinaryPrimitives.WriteDoubleLittleEndian(t.AsSpan(8), double.NaN);
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Tool(t));
|
|
}
|
|
|
|
[Fact]
|
|
public void Axes_are_aligned_with_no_guessed_units()
|
|
{
|
|
var a = Lsv2Data.Axes(T("\0\u0002-1.25\u00002.5\0X\0C\0"));
|
|
a.Names.ShouldBe(new[] { "X", "C" });
|
|
a.Positions.ShouldBe(new[] { -1.25, 2.5 });
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Axes(T("\0\u0002NaN\u00002\0X\0Y\0")));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Axes(T("\0\u00021\u00002\0X\0X\0")));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Axes(T("\0\u00021\u00002\0X\0")));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.Axes(T("\0\u0002abc\u00002\0X\0Y\0")));
|
|
}
|
|
|
|
[Fact]
|
|
public void Error_number_is_signed_and_data_path_types_are_strict()
|
|
{
|
|
var e = new byte[12];
|
|
BinaryPrimitives.WriteInt16BigEndian(e, 3);
|
|
BinaryPrimitives.WriteInt16BigEndian(e.AsSpan(2), 4);
|
|
BinaryPrimitives.WriteInt32BigEndian(e.AsSpan(4), -10);
|
|
T("Err\0").CopyTo(e, 8);
|
|
Lsv2Data.Alarm(e).Number.ShouldBe(-10);
|
|
Lsv2Data.DataPath([0, 0, 0, 2, 0xFF, 0xFE]).ShouldBe(-2);
|
|
Lsv2Data.DataPath([0, 0, 0, 17, 0xFF]).ShouldBe(255);
|
|
Lsv2Data.DataPath([0, 0, 0, 16, 0xFF]).ShouldBe(-1);
|
|
Lsv2Data.DataPath([0, 0, 0, 11, 1]).ShouldBe(true);
|
|
Lsv2Data.DataPath([0, 0, 0, 8, 65, 66, 0]).ShouldBe("AB");
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.DataPath([0, 0, 0, 5]));
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.DataPath([0, 0, 0, 99, 1]));
|
|
var inf = new byte[12];
|
|
BinaryPrimitives.WriteInt32BigEndian(inf, 5);
|
|
BinaryPrimitives.WriteDoubleLittleEndian(inf.AsSpan(4), double.PositiveInfinity);
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.DataPath(inf));
|
|
}
|
|
|
|
[Fact]
|
|
public void Data_path_boolean_accepts_the_real_controls_ff_true_encoding()
|
|
{
|
|
Lsv2Data.DataPath([0, 0, 0, 11, 0xFF]).ShouldBe(true);
|
|
Lsv2Data.DataPath([0, 0, 0, 11, 0]).ShouldBe(false);
|
|
Should.Throw<Lsv2DecodeException>(() => Lsv2Data.DataPath([0, 0, 0, 11, 2]));
|
|
}
|
|
|
|
[Fact]
|
|
public void Numeric_counts_are_bytes_even_when_no_adjacent_area_caps_them()
|
|
{
|
|
foreach (var length in new[] { 120, 124 })
|
|
{
|
|
var bytes = new byte[length];
|
|
SimulatedControl.LayoutBytes(10).CopyTo(bytes, 0);
|
|
var layout = Lsv2Data.Layout(bytes);
|
|
foreach (var area in new[] { HeidenhainPlcArea.Byte, HeidenhainPlcArea.Word, HeidenhainPlcArea.Dword, HeidenhainPlcArea.InputWord, HeidenhainPlcArea.OutputWord })
|
|
layout.Region(area)!.Bytes.ShouldBe(10, area.ToString());
|
|
layout.Region(HeidenhainPlcArea.String)!.Bytes.ShouldBe(160);
|
|
layout.Region(HeidenhainPlcArea.Word)!.Base.ShouldBe(1000);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Memory_range_uses_byte_capacity_and_element_index()
|
|
{
|
|
var layout = Lsv2Data.Layout(SimulatedControl.LayoutBytes(10));
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.Dword, 1, layout).ShouldBe(new HeidenhainMemoryRange(1004, 4));
|
|
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.Dword, 2, layout));
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.Byte, 9, layout).Address.ShouldBe(1009);
|
|
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.Byte, 10, layout));
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.Word, 4, layout).Address.ShouldBe(1008);
|
|
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.Word, 5, layout));
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.InputWord, 4, layout).Address.ShouldBe(3008);
|
|
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.InputWord, 5, layout));
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.OutputWord, 4, layout).Address.ShouldBe(4008);
|
|
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.OutputWord, 5, layout));
|
|
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.Word, 0, null));
|
|
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.Word, 0xFFFF_FFFFL, layout));
|
|
}
|
|
|
|
[Fact]
|
|
public void Memory_and_runtime_coercion_cannot_truncate_or_wrap()
|
|
{
|
|
HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.Dword, [0xFE, 0xFF, 0xFF, 0xFF], DriverDataType.Int64).ShouldBe(-2L);
|
|
HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.InputWord, [0xFF, 0xFF], DriverDataType.Int32).ShouldBe(65535);
|
|
HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.Word, [0xFF, 0xFF], DriverDataType.Int16).ShouldBe((short)-1);
|
|
HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.Marker, [1], DriverDataType.Boolean).ShouldBe(true);
|
|
Should.Throw<Lsv2DecodeException>(() => HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.Marker, [0xFF], DriverDataType.Boolean));
|
|
Should.Throw<Lsv2DecodeException>(() => HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.Dword, new byte[3], DriverDataType.Int64));
|
|
Should.Throw<HeidenhainTypeMismatchException>(() => HeidenhainBindingCodec.Coerce(1.5, DriverDataType.Int32));
|
|
Should.Throw<HeidenhainTypeMismatchException>(() => HeidenhainBindingCodec.Coerce(32768, DriverDataType.Int16));
|
|
Should.Throw<HeidenhainTypeMismatchException>(() => HeidenhainBindingCodec.Coerce(1, DriverDataType.Boolean));
|
|
Should.Throw<HeidenhainTypeMismatchException>(() => HeidenhainBindingCodec.Coerce(1, DriverDataType.String));
|
|
Should.Throw<HeidenhainTypeMismatchException>(() => HeidenhainBindingCodec.Coerce(double.NaN, DriverDataType.Float64));
|
|
Should.Throw<HeidenhainTypeMismatchException>(() => HeidenhainBindingCodec.Coerce("x", DriverDataType.Int64));
|
|
HeidenhainBindingCodec.Coerce(2.0, DriverDataType.Int64).ShouldBe(2L);
|
|
HeidenhainBindingCodec.Coerce(7, DriverDataType.Float64).ShouldBe(7.0);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("hermle49")]
|
|
[InlineData("hermle59")]
|
|
public void Captured_layouts_reject_numeric_reads_at_the_next_advertised_area(string machine)
|
|
{
|
|
var f = Fixtures.Load("itnc530-inspect.properties");
|
|
var layout = Lsv2Data.Layout(Convert.FromHexString(f[machine + ".layout"]));
|
|
foreach (var (area, index) in new[] { (HeidenhainPlcArea.Byte, 10000L), (HeidenhainPlcArea.Word, 5000L), (HeidenhainPlcArea.Dword, 2500L), (HeidenhainPlcArea.InputWord, 500L), (HeidenhainPlcArea.OutputWord, 500L) })
|
|
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(area, index, layout), $"{machine} {area}:{index}");
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.Byte, 9999, layout).Address.ShouldBe(10063);
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.Word, 4999, layout).Address.ShouldBe(10062);
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.Dword, 2499, layout).Address.ShouldBe(10060);
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.InputWord, 499, layout).Address.ShouldBe(11062);
|
|
HeidenhainBindingCodec.Range(HeidenhainPlcArea.OutputWord, 499, layout).Address.ShouldBe(12062);
|
|
}
|
|
}
|