Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests/HeidenhainPlcDataPyLsv2ParityTests.cs
T
Joseph Doherty f8785cfa2c Merge branch 'worktree-agent-a71443b71c4c01c13' into heidenhain-pylsv2-parity
# Conflicts:
#	.claude/rules/drivers.md
#	docs/drivers/Heidenhain.md
#	tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests/HeidenhainPyLsv2ParityTests.cs
2026-11-12 23:57:06 -05:00

163 lines
9.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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>
/// PLC-memory (<c>R_PR</c>/<c>R_MB</c>) and DATA-path (<c>R_DP</c>) parity with pyLSV2 (MIT). Where pyLSV2
/// disagrees with a real iTNC 530 capture the capture wins and the test pins the capture (see
/// <c>docs/drivers/Heidenhain.md</c> §"pyLSV2 correspondence").
/// </summary>
[Trait("Category", "Unit")]
public sealed class HeidenhainPlcDataPyLsv2ParityTests
{
[Theory]
[InlineData(HeidenhainPlcArea.Marker)]
[InlineData(HeidenhainPlcArea.Input)]
[InlineData(HeidenhainPlcArea.Output)]
[InlineData(HeidenhainPlcArea.Counter)]
[InlineData(HeidenhainPlcArea.Timer)]
public void Plc_bit_areas_accept_the_ff_true_byte_real_controls_return_for_the_same_operands(HeidenhainPlcArea area)
{
// R_DP /PLC/memory/I/999 answered type 11 + FF on hermle59; pyLSV2 read_plc_memory unpacks "?" (non-zero = true).
HeidenhainBindingCodec.DecodePlc(area, [0xFF], DriverDataType.Boolean).ShouldBe(true);
HeidenhainBindingCodec.DecodePlc(area, [0x01], DriverDataType.Boolean).ShouldBe(true);
HeidenhainBindingCodec.DecodePlc(area, [0x00], DriverDataType.Boolean).ShouldBe(false);
// Still strict: pyLSV2 would call 0x02 true, but no control has been seen to send it.
Should.Throw<Lsv2DecodeException>(() => HeidenhainBindingCodec.DecodePlc(area, [0x02], DriverDataType.Boolean));
Should.Throw<Lsv2DecodeException>(() => HeidenhainBindingCodec.DecodePlc(area, [0x00, 0x00], DriverDataType.Boolean));
}
[Fact]
public void The_captured_ff_bit_is_the_same_byte_the_r_mb_decoder_now_accepts()
{
var f = Fixtures.Load("plc-count-units.properties");
var reply = f["hermle59.I.999"]; // S_DP:0000000bff
var value = Convert.FromHexString(reply["S_DP:".Length..]);
Lsv2Data.DataPath(value).ShouldBe(true);
HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.Input, value.AsSpan(4), DriverDataType.Boolean).ShouldBe(true);
}
[Theory]
[InlineData("PLC:INPUT_DWORD:3", HeidenhainPlcArea.InputDword)]
[InlineData("PLC:OUTPUT_DWORD:0", HeidenhainPlcArea.OutputDword)]
public void Input_and_output_dword_areas_parse_as_element_indexes(string address, HeidenhainPlcArea area)
{
HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", TestTags.Config(address, "Int32"), out var d, out var error)
.ShouldBeTrue(error);
d!.Address.Area.ShouldBe(area);
d.Address.Text.ShouldBe(address);
HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", TestTags.Config(address, "Int64"), out _, out _).ShouldBeTrue();
// int32 must not be narrowed into Int16; it is not a Boolean/String area.
HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", TestTags.Config(address, "Int16"), out _, out _).ShouldBeFalse();
HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", TestTags.Config(address, "Boolean"), out _, out _).ShouldBeFalse();
HeidenhainAddress.AreaTokens.ShouldContain(address.Split(':')[1]);
}
[Fact]
public void Dword_io_views_share_the_word_pools_with_element_index_times_four()
{
var layout = Lsv2Data.Layout(SimulatedControl.LayoutBytes(10));
var iw = layout.Region(HeidenhainPlcArea.InputWord)!;
var ow = layout.Region(HeidenhainPlcArea.OutputWord)!;
HeidenhainBindingCodec.Range(HeidenhainPlcArea.InputDword, 1, layout).ShouldBe(new HeidenhainMemoryRange(iw.Base + 4, 4));
HeidenhainBindingCodec.Range(HeidenhainPlcArea.OutputDword, 1, layout).ShouldBe(new HeidenhainMemoryRange(ow.Base + 4, 4));
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.InputDword, 2, layout));
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.OutputDword, 2, layout));
}
[Theory]
[InlineData("hermle59")]
[InlineData("hermle70")]
[InlineData("hermle29")]
public void Captured_layouts_give_dword_io_views_a_quarter_of_the_byte_pool(string machine)
{
// The input/output word pools are 1000 BYTES on these controls (IW998 is the last valid R_DP word); a
// DWORD view therefore holds 250 elements. pyLSV2 caps INPUT_DWORD at count/4 too.
var f = Fixtures.Load("plc-count-units.properties");
var layout = Lsv2Data.Layout(Convert.FromHexString(f[machine + ".layout"]));
var iw = layout.Region(HeidenhainPlcArea.InputWord)!;
HeidenhainBindingCodec.Range(HeidenhainPlcArea.InputDword, 249, layout).Address.ShouldBe(iw.Base + 996);
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.InputDword, 250, layout));
var ow = layout.Region(HeidenhainPlcArea.OutputWord)!;
HeidenhainBindingCodec.Range(HeidenhainPlcArea.OutputDword, 249, layout).Address.ShouldBe(ow.Base + 996);
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.OutputDword, 250, layout));
}
[Theory]
[InlineData("hermle59")]
[InlineData("hermle70")]
[InlineData("hermle29")]
public void Captured_layouts_contradict_pyLSV2_word_count_units(string machine)
{
// pyLSV2 read_plc_memory: BYTE max = count×2, WORD max = count, INPUT_WORD max = count (count read as words).
// The real controls refuse B10000 / W10000 / IW1000 over R_DP, and each advertised base + count is exactly
// the next advertised base — the counts are bytes. Pin the capture, not pyLSV2.
var f = Fixtures.Load("plc-count-units.properties");
var layout = Lsv2Data.Layout(Convert.FromHexString(f[machine + ".layout"]));
var words = layout.Region(HeidenhainPlcArea.Word)!;
var iw = layout.Region(HeidenhainPlcArea.InputWord)!;
var ow = layout.Region(HeidenhainPlcArea.OutputWord)!;
var markers = layout.Region(HeidenhainPlcArea.Marker)!;
(words.Base + words.Bytes).ShouldBe(iw.Base);
(iw.Base + iw.Bytes).ShouldBe(ow.Base);
(ow.Base + ow.Bytes).ShouldBe(markers.Base);
f[machine + ".B.10000"].ShouldStartWith("T_ER");
f[machine + ".W.9998"].ShouldStartWith("S_DP");
f[machine + ".IW.998"].ShouldStartWith("S_DP");
f[machine + ".IW.1000"].ShouldStartWith("T_ER");
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.Byte, 10000, layout));
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.Word, 5000, layout));
Should.Throw<HeidenhainRangeException>(() => HeidenhainBindingCodec.Range(HeidenhainPlcArea.InputWord, 500, layout));
}
[Fact]
public void Dword_io_decodes_signed_int32_little_endian()
{
HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.InputDword, [0xFE, 0xFF, 0xFF, 0xFF], DriverDataType.Int32).ShouldBe(-2);
HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.OutputDword, [0x01, 0x02, 0x03, 0x04], DriverDataType.Int64).ShouldBe(0x04030201L);
Should.Throw<Lsv2DecodeException>(() => HeidenhainBindingCodec.DecodePlc(HeidenhainPlcArea.InputDword, [0, 0], DriverDataType.Int32));
}
[Fact]
public void Plc_request_is_element_index_times_width_plus_base_like_pyLSV2()
{
// pyLSV2 read_plc_memory: address = start + first_element × mem_byte_count; count byte = width for one element.
var layout = Lsv2Data.Layout(SimulatedControl.LayoutBytes(10));
var w = layout.Region(HeidenhainPlcArea.Word)!;
var range = HeidenhainBindingCodec.Range(HeidenhainPlcArea.Word, 3, layout);
range.ShouldBe(new HeidenhainMemoryRange(w.Base + 6, 2));
var payload = Lsv2ReadOnlyPolicy.MemoryPayload(range.Address, range.Bytes);
BinaryPrimitives.ReadUInt32BigEndian(payload).ShouldBe((uint)(w.Base + 6));
payload[4].ShouldBe((byte)2);
}
[Fact]
public void Data_path_request_matches_pyLSV2_formatting()
{
// pyLSV2 read_data_path: four 0x00 bytes + path with / → \ and " → ' + NUL.
var payload = Lsv2ReadOnlyPolicy.DataPathPayload("/TABLE/TOOL/T/1/\"DOC\"");
payload[..4].ShouldBe(new byte[4]);
Encoding.Latin1.GetString(payload, 4, payload.Length - 4).ShouldBe("\\TABLE\\TOOL\\T\\1\\'DOC'\0");
}
[Fact]
public void Data_path_value_types_match_pyLSV2_table()
{
Lsv2Data.DataPath([0, 0, 0, 2, 0xFF, 0xFE]).ShouldBe(-2); // !h
Lsv2Data.DataPath([0, 0, 0, 3, 0xFF, 0xFF, 0xFF, 0xFE]).ShouldBe(-2); // !l
var dbl = new byte[12];
dbl[3] = 5;
BinaryPrimitives.WriteDoubleLittleEndian(dbl.AsSpan(4), 1.25); // <d
Lsv2Data.DataPath(dbl).ShouldBe(1.25);
Lsv2Data.DataPath([0, 0, 0, 8, (byte)'A', 0]).ShouldBe("A");
Lsv2Data.DataPath([0, 0, 0, 16, 0xFF]).ShouldBe(-1); // !b
Lsv2Data.DataPath([0, 0, 0, 17, 0xFF]).ShouldBe(255); // !B
}
}