ba9015fac8
- HeidenhainControlFamily + HeidenhainControlFamilies.Classify (Contracts): one authority, ported from pyLSV2 (MIT) dat_cls.py VersionInfo.control (strip spaces, case-insensitive prefix, ITNC530 first). Families: ITnc530, Tnc (640/620/320/128), Tnc7, Lathe (CNC PILOT 640 / MANUALplus 620), MillPlus, LegacyTnc (426/430, not a pyLSV2 class), Unknown. - New additive read-only leaf Controller/Family (String, Identity feature, derived from the same R_VR reply: no extra request). Lsv2Versions.Family is computed, so record equality and the #831 identity-change rediscovery signal are unchanged. Probe message + /hosts diagnostics (<host>.family). - DATA gate now goes through SupportsDataPath(family) (pyLSV2 client.py read_data_path is iTNC-only): non-iTNC and Unknown => Capabilities/DataLogin Unsupported, DATA login never sent. Replaces the "model contains itnc" substring check (same result on the real iTNC530 controls). - No per-family selector pre-gates: pyLSV2's TNC 640 observations (27/28/51/30-34 unknown telegram, 25/26 need DNC) are already handled by the reply-driven refusal/suppression path; tests pin it. - Lsv2ReadOnlyPolicy, HeidenhainTagWritable and the no-IWritable rule are untouched. - Simulator: per-family ControlProfiles (strings from the pyLSV2 README tables / protocol.rst), both R_VR field counts, 124-byte R_PR; HeidenhainControlFamilyTests (classification, gates, decoding, wire log inside the read-only set, refused selectors asked once). - docs/drivers/Heidenhain.md: Control families matrix (iTNC 530 hardware-proven; the rest per pyLSV2, hardware-unproven) + open questions (C_CC buffer size, encrypted passwords, R_RI 20/21). Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
264 lines
12 KiB
C#
264 lines
12 KiB
C#
using System.Text;
|
||
using Shouldly;
|
||
using Xunit;
|
||
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>
|
||
/// Control-family classification and the per-family gates, per pyLSV2 (MIT). Only the iTNC 530 rows are
|
||
/// hardware-backed; every other profile is built from what pyLSV2 records (see <see cref="ControlProfiles"/>).
|
||
/// </summary>
|
||
[Trait("Category", "Unit")]
|
||
public sealed class HeidenhainControlFamilyTests
|
||
{
|
||
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
||
|
||
public static TheoryData<string> Profiles()
|
||
{
|
||
var d = new TheoryData<string>();
|
||
foreach (var p in ControlProfiles.All) d.Add(p.Name);
|
||
return d;
|
||
}
|
||
|
||
private static ControlProfile Profile(string name) => ControlProfiles.All.Single(p => p.Name == name);
|
||
|
||
[Theory]
|
||
// per pyLSV2 (MIT) dat_cls.py:VersionInfo.control — spaces stripped, case-insensitive prefix.
|
||
[InlineData("iTNC530", HeidenhainControlFamily.ITnc530)]
|
||
[InlineData("iTNC 530", HeidenhainControlFamily.ITnc530)]
|
||
[InlineData("iTNC530 Programmierplatz", HeidenhainControlFamily.ITnc530)]
|
||
[InlineData("ITNC530", HeidenhainControlFamily.ITnc530)]
|
||
[InlineData("TNC640", HeidenhainControlFamily.Tnc)]
|
||
[InlineData("TNC 640", HeidenhainControlFamily.Tnc)]
|
||
[InlineData("TNC620", HeidenhainControlFamily.Tnc)]
|
||
[InlineData("TNC320", HeidenhainControlFamily.Tnc)]
|
||
[InlineData("tnc128", HeidenhainControlFamily.Tnc)]
|
||
[InlineData("TNC7", HeidenhainControlFamily.Tnc7)]
|
||
[InlineData("TNC 7", HeidenhainControlFamily.Tnc7)]
|
||
[InlineData("CNCpilot640", HeidenhainControlFamily.Lathe)]
|
||
[InlineData("CNC PILOT 640", HeidenhainControlFamily.Lathe)]
|
||
[InlineData("MANUALplus620", HeidenhainControlFamily.Lathe)]
|
||
[InlineData("MillPlusIT V600", HeidenhainControlFamily.MillPlus)]
|
||
[InlineData("TNC 426", HeidenhainControlFamily.LegacyTnc)]
|
||
[InlineData("TNC430", HeidenhainControlFamily.LegacyTnc)]
|
||
[InlineData("TNC", HeidenhainControlFamily.Unknown)]
|
||
[InlineData("TNC 530", HeidenhainControlFamily.Unknown)] // not an iTNC: no "i"
|
||
[InlineData("Anilam 6000i", HeidenhainControlFamily.Unknown)]
|
||
[InlineData("", HeidenhainControlFamily.Unknown)]
|
||
[InlineData(" ", HeidenhainControlFamily.Unknown)]
|
||
[InlineData(null, HeidenhainControlFamily.Unknown)]
|
||
public void Model_string_classifies_to_its_family(string? model, HeidenhainControlFamily expected) =>
|
||
HeidenhainControlFamilies.Classify(model).ShouldBe(expected);
|
||
|
||
[Fact]
|
||
public void Only_the_itnc_530_supports_data_paths()
|
||
{
|
||
foreach (var f in Enum.GetValues<HeidenhainControlFamily>())
|
||
HeidenhainControlFamilies.SupportsDataPath(f).ShouldBe(f == HeidenhainControlFamily.ITnc530, f.ToString());
|
||
}
|
||
|
||
[Fact]
|
||
public void Family_is_a_fixed_identity_string_leaf()
|
||
{
|
||
HeidenhainCatalog.TryGet("Controller/Family", out var e).ShouldBeTrue();
|
||
e.DataType.ShouldBe(ZB.MOM.WW.OtOpcUa.Core.Abstractions.DriverDataType.String);
|
||
e.IsArray.ShouldBeFalse();
|
||
e.Feature.ShouldBe(HeidenhainCatalog.IdentityFeature);
|
||
e.Group.ShouldBe(HeidenhainPollGroup.Slow);
|
||
}
|
||
|
||
[Fact]
|
||
public void Family_is_derived_so_identity_equality_is_unchanged()
|
||
{
|
||
var a = new Lsv2Versions("iTNC530", "340490 04 SP10", "PLC", "OPT");
|
||
a.ShouldBe(new Lsv2Versions("iTNC530", "340490 04 SP10", "PLC", "OPT"));
|
||
a.Family.ShouldBe(HeidenhainControlFamily.ITnc530);
|
||
}
|
||
|
||
[Theory]
|
||
[MemberData(nameof(Profiles))]
|
||
public void Version_reply_of_every_family_decodes_in_both_field_counts(string name)
|
||
{
|
||
var p = Profile(name);
|
||
foreach (var five in new[] { false, true })
|
||
{
|
||
var v = Lsv2Data.Versions(ControlProfiles.VersionReply(p with { FiveFieldVersion = five }));
|
||
v.Model.ShouldBe(p.Model);
|
||
v.Nc.ShouldBe(p.Nc);
|
||
v.Options.ShouldBe("%0000111100000111"); // SPLC is skipped, never relabelled as options
|
||
v.Family.ShouldBe(p.Family);
|
||
}
|
||
}
|
||
|
||
[Theory]
|
||
[MemberData(nameof(Profiles))]
|
||
public async Task Family_leaf_publishes_the_classified_family(string name)
|
||
{
|
||
var p = Profile(name);
|
||
await using var h = await Harness.OpenAsync(ControlProfiles.Build(p), Std("Controller/Family", "Controller/Model", "Capabilities/Identity"));
|
||
var s = await h.ReadAsync();
|
||
s[0].Status.ShouldBe(HeidenhainStatus.Good);
|
||
s[0].Value.ShouldBe(p.Family.ToString());
|
||
s[0].Value.ShouldBeOfType<string>();
|
||
s[1].Value.ShouldBe(p.Model); // the raw model string is never normalised
|
||
s[2].Value.ShouldBe(HeidenhainCatalog.States.Supported);
|
||
h.Session.Identity!.Family.ShouldBe(p.Family);
|
||
}
|
||
|
||
[Theory]
|
||
[MemberData(nameof(Profiles))]
|
||
public async Task Data_path_is_iTNC_only_and_a_non_iTNC_never_receives_the_DATA_login(string name)
|
||
{
|
||
var p = Profile(name);
|
||
var tags = new List<HeidenhainTagDefinition> { TestTags.Def("DATA:/x", "Int64"), TestTags.Def("Capabilities/DataLogin") };
|
||
await using var h = await Harness.OpenAsync(ControlProfiles.Build(p), tags, telemetry: false, data: true);
|
||
var first = await h.ReadAsync();
|
||
var second = await h.ReadAsync();
|
||
if (p.Family == HeidenhainControlFamily.ITnc530)
|
||
{
|
||
first[0].Value.ShouldBe(42L);
|
||
first[1].Value.ShouldBe(HeidenhainCatalog.States.Supported);
|
||
h.Control.Logins.Select(l => l.Role).ShouldBe(["INSPECT", "DATA"]);
|
||
h.Sim.Requests.Count(r => r.Command == "R_DP").ShouldBe(2);
|
||
}
|
||
else
|
||
{
|
||
foreach (var s in new[] { first, second })
|
||
{
|
||
s[0].Status.ShouldBe(HeidenhainStatus.BadNotSupported);
|
||
s[1].Value.ShouldBe(HeidenhainCatalog.States.Unsupported);
|
||
}
|
||
|
||
h.Control.Logins.Select(l => l.Role).ShouldBe(["INSPECT"]);
|
||
h.Sim.Requests.ShouldNotContain(r => r.Command == "R_DP");
|
||
}
|
||
}
|
||
|
||
[Theory]
|
||
[MemberData(nameof(Profiles))]
|
||
public async Task Full_catalog_on_every_family_stays_inside_the_read_only_set(string name)
|
||
{
|
||
var p = Profile(name);
|
||
var c = ControlProfiles.Build(p);
|
||
var tags = HeidenhainCatalog.Entries.Select(e => TestTags.Def(e.Address)).ToList();
|
||
tags.Add(TestTags.Def("DATA:/x", "Int64"));
|
||
tags.Add(TestTags.Def("PLC:WORD:1", "Int32"));
|
||
await using var h = await Harness.OpenAsync(c, tags, timers: true, data: true);
|
||
await h.ReadAsync();
|
||
await h.ReadAsync();
|
||
h.Sim.Requests.Select(r => r.Command).Distinct().ShouldBeSubsetOf(Lsv2ReadOnlyPolicy.Commands);
|
||
h.Control.Logins.Select(l => l.Role).Distinct().ShouldBeSubsetOf(Lsv2ReadOnlyPolicy.Roles);
|
||
if (p.Family != HeidenhainControlFamily.ITnc530)
|
||
{
|
||
h.Control.Logins.ShouldNotContain(l => l.Role == "DATA");
|
||
h.Sim.Requests.ShouldNotContain(r => r.Command == "R_DP");
|
||
}
|
||
|
||
// A selector the family refuses (T_ER_UNKNOWN_TELE) is asked once per session, then suppressed.
|
||
foreach (var s in new[] { 51, 27, 30, 31, 32, 33, 34 })
|
||
if (p.Selectors != SimSelectorBehaviour.All)
|
||
h.Sim.Requests.Count(r => r.Command == "R_RI" && r.Selector == s).ShouldBe(1, $"selector {s}");
|
||
}
|
||
|
||
[Theory]
|
||
[InlineData(true)]
|
||
[InlineData(false)]
|
||
public async Task Tnc_640_observed_selectors_degrade_per_feature(bool dncGranted)
|
||
{
|
||
// per pyLSV2 (MIT) docs/protocol.rst — TNC 640 NC 340590 10 SP2: 27/28/51 + 30–34 unknown telegram; 25/26 need DNC.
|
||
var c = ControlProfiles.Build(ControlProfiles.Tnc640);
|
||
if (!dncGranted) c.Login = (role, _) => role == "DNC" ? SimReply.Refuse(23) : SimReply.Ok("T_OK", []);
|
||
var tags = Std("Controller/Family", "Status/ExecutionState", "Status/ProgramState", "Overrides/Feed", "Program/Line", "Axes/Names",
|
||
"Tool/Number", "Alarms/Count", "Timers/NcUptime",
|
||
"Capabilities/Tool", "Capabilities/Alarms", "Capabilities/Timer_NcUptime", "Capabilities/Overrides", "Capabilities/ProgramState");
|
||
await using var h = await Harness.OpenAsync(c, tags, timers: true);
|
||
var s = await h.ReadAsync();
|
||
s[0].Value.ShouldBe("Tnc");
|
||
s[1].Value.ShouldBe(4);
|
||
s[5].Value.ShouldBe(new[] { "X", "Z" });
|
||
s[6].Status.ShouldBe(HeidenhainStatus.BadNotSupported);
|
||
s[7].Status.ShouldBe(HeidenhainStatus.BadNotSupported);
|
||
s[8].Status.ShouldBe(HeidenhainStatus.BadNotSupported);
|
||
s[9].Value.ShouldBe(HeidenhainCatalog.States.Unsupported);
|
||
s[10].Value.ShouldBe(HeidenhainCatalog.States.Unsupported);
|
||
s[11].Value.ShouldBe(HeidenhainCatalog.States.Unsupported);
|
||
if (dncGranted)
|
||
{
|
||
s[2].Value.ShouldBe(7);
|
||
s[3].Value.ShouldBe(100.0);
|
||
s[4].Value.ShouldBe(5L);
|
||
}
|
||
else
|
||
{
|
||
s[2].Status.ShouldBe(HeidenhainStatus.BadUserAccessDenied);
|
||
s[3].Status.ShouldBe(HeidenhainStatus.BadUserAccessDenied);
|
||
s[4].Value.ShouldBe(0L); // served under INSPECT, block number 0 — published raw, not repaired
|
||
s[12].Value.ShouldBe(HeidenhainCatalog.States.AccessDenied);
|
||
s[13].Value.ShouldBe(HeidenhainCatalog.States.AccessDenied);
|
||
}
|
||
}
|
||
|
||
[Theory]
|
||
[InlineData("TNC 640")]
|
||
[InlineData("CNC PILOT 640")]
|
||
[InlineData("iTNC 530")]
|
||
public async Task Plc_layout_is_family_independent(string name)
|
||
{
|
||
// per pyLSV2 (MIT) misc.py:decode_system_parameters — the 120/124-byte R_PR layouts carry no family branch,
|
||
// and client.py:read_plc_memory has no family gate.
|
||
var c = ControlProfiles.Build(Profile(name));
|
||
var layout = SimulatedControl.LayoutBytes(10);
|
||
c.Layout = [.. layout, 0, 0, 0, 0]; // the 124-byte variant
|
||
c.Memory = (_, n) => SimReply.Ok("S_MB", new byte[n]);
|
||
var tags = new List<HeidenhainTagDefinition> { TestTags.Def("PLC:WORD:1", "Int32"), TestTags.Def("Capabilities/PlcLayout") };
|
||
await using var h = await Harness.OpenAsync(c, tags, telemetry: false);
|
||
var s = await h.ReadAsync();
|
||
s[0].Status.ShouldBe(HeidenhainStatus.Good);
|
||
s[1].Value.ShouldBe(HeidenhainCatalog.States.Supported);
|
||
}
|
||
|
||
[Fact]
|
||
public async Task Probe_reports_the_family()
|
||
{
|
||
await using var sim = Lsv2Simulator.WithControl(ControlProfiles.Build(ControlProfiles.Tnc640));
|
||
var json = $$"""{"Devices":[{"Host":"127.0.0.1","Port":{{sim.Port}},"DeviceName":"d"}]}""";
|
||
var result = await new HeidenhainDriverProbe().ProbeAsync(json, TimeSpan.FromSeconds(3), Ct);
|
||
result.Ok.ShouldBeTrue(result.Message);
|
||
(result.Message ?? "").ShouldContain("TNC640 (Tnc) NC 340590 10 SP2");
|
||
}
|
||
|
||
private static List<HeidenhainTagDefinition> Std(params string[] a) => a.Select(x => TestTags.Def(x)).ToList();
|
||
|
||
private sealed class Harness : IAsyncDisposable
|
||
{
|
||
public required Lsv2Simulator Sim { get; init; }
|
||
public required SimulatedControl Control { get; init; }
|
||
public required HeidenhainSession Session { get; init; }
|
||
public required IReadOnlyList<HeidenhainTagDefinition> Tags { get; init; }
|
||
|
||
public Task<IReadOnlyList<HeidenhainSample>> ReadAsync() => Session.ReadBatchAsync(Tags, Ct);
|
||
|
||
public static async Task<Harness> OpenAsync(
|
||
SimulatedControl control, IReadOnlyList<HeidenhainTagDefinition> tags, bool telemetry = true, bool data = false, bool timers = false)
|
||
{
|
||
var sim = Lsv2Simulator.WithControl(control);
|
||
var client = new Lsv2Client("127.0.0.1", sim.Port, TimeSpan.FromSeconds(1));
|
||
var settings = new HeidenhainSessionSettings(telemetry, true, data, 64, timers,
|
||
tags.Any(t => t.Address.Kind == HeidenhainAddressKind.Plc), tags.Any(t => t.Address.Kind == HeidenhainAddressKind.DataPath));
|
||
var session = new HeidenhainSession(client, settings, (_, _) => ValueTask.FromResult(""));
|
||
await client.OpenAsync(Ct);
|
||
await session.InitializeAsync(Ct);
|
||
return new Harness { Sim = sim, Control = control, Session = session, Tags = tags };
|
||
}
|
||
|
||
public async ValueTask DisposeAsync()
|
||
{
|
||
await Session.DisposeAsync();
|
||
Control.Violations.ShouldBeEmpty();
|
||
await Sim.DisposeAsync();
|
||
}
|
||
}
|
||
}
|