using System.Diagnostics; using System.Net.Sockets; using System.Text.Json; using System.Text.Json.Serialization; using libplctag; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.PlcFamilies; namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy; /// /// Two-phase Test-Connect probe for the -shaped driver config. /// Phase 1: bare TCP connect to the first device's gateway host + EtherNet/IP port to /// quickly reject unreachable targets. Phase 2: initialises a libplctag /// against the same device to open a real PCCC-over-EIP session — confirming the remote /// endpoint actually speaks PCCC, not just accepts TCP. /// A device that accepts the TCP connection but is not a PCCC controller (wrong path, /// non-PCCC server) returns Ok = false with a "handshake failed" message instead of /// a false-positive green tick. /// /// Status-code discrimination: libplctag statuses that indicate the controller answered /// the PCCC session but could not resolve the probe tag /// (, , /// ) are treated as "reachable — Ok = true" because /// a non-existent tag name proves PCCC connectivity. Statuses that indicate a transport or /// session failure (, , /// , , /// , , /// , , /// , , /// , ) are treated as /// "handshake failed — Ok = false". All other error statuses default to /// "handshake failed" (conservative). /// /// public sealed class AbLegacyDriverProbe : IDriverProbe { private static readonly JsonSerializerOptions _opts = new() { PropertyNameCaseInsensitive = true, UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, }; /// public string DriverType => "AbLegacy"; /// public async Task ProbeAsync(string configJson, TimeSpan timeout, CancellationToken ct) { AbLegacyDriverOptions? opts; try { opts = JsonSerializer.Deserialize(configJson, _opts); } catch (Exception ex) { return new(false, $"Config JSON is invalid: {ex.Message}", null); } if (opts is null) return new(false, "Config JSON deserialized to null.", null); var (parsed, firstDevice) = ExtractTarget(opts); if (parsed is null || string.IsNullOrWhiteSpace(parsed.Gateway) || parsed.Port <= 0) return new(false, "Config has no host/port to probe.", null); var host = parsed.Gateway; var port = parsed.Port; // Bound both phases by the caller timeout, independent of `ct`, so a device that accepts // TCP but stalls the PCCC session cannot hang the probe. using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); cts.CancelAfter(timeout); // Phase 1: bare TCP preflight — fast rejection for unreachable hosts. var sw = Stopwatch.StartNew(); try { using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); await socket.ConnectAsync(host, port, cts.Token); } catch (SocketException ex) { return new(false, $"Connect failed: {ex.SocketErrorCode}", null); } catch (OperationCanceledException) { return new(false, $"Probe timed out after {timeout.TotalSeconds:F0}s.", null); } catch (Exception ex) { return new(false, ex.Message, null); } // Phase 2: PCCC session via libplctag LibplctagLegacyTagRuntime.InitializeAsync. // Open a real PCCC-over-EIP session to confirm the remote endpoint speaks PCCC, not just TCP. var profile = AbLegacyPlcFamilyProfile.ForFamily(firstDevice?.PlcFamily ?? AbLegacyPlcFamily.Slc500); // Use the configured probe address (defaults to "S:0" — status file, first word). // S:0 is present on all PCCC PLCs that support the status file, so a "not found" response // still proves PCCC connectivity; a successful read is the happy path. var tagName = opts.Probe.ProbeAddress ?? opts.Tags.FirstOrDefault( t => string.Equals(t.DeviceHostAddress, firstDevice?.HostAddress, StringComparison.OrdinalIgnoreCase)) ?.Address ?? opts.Tags.FirstOrDefault()?.Address ?? "S:0"; var p = new AbLegacyTagCreateParams( Gateway: parsed.Gateway, Port: parsed.Port, CipPath: parsed.CipPath, LibplctagPlcAttribute: profile.LibplctagPlcAttribute, TagName: tagName, Timeout: timeout); var rt = new LibplctagLegacyTagRuntime(p); try { await rt.InitializeAsync(cts.Token); sw.Stop(); // InitializeAsync completed without throwing — either the tag was found (Ok) or // libplctag returned a non-exception status. Check via GetStatus(). var statusCode = (Status)rt.GetStatus(); if (IsReachableStatus(statusCode)) { var msg = statusCode == Status.Ok ? "PCCC session OK" : "PCCC session OK (controller reachable; probe tag not found)"; return new(true, msg, sw.Elapsed); } // Non-Ok but non-exception: a session/transport error surfaced in the status word. return new(false, $"Reachable at {host}:{port} but PCCC handshake failed: {statusCode}", null); } catch (OperationCanceledException) { return new(false, $"Probe timed out after {timeout.TotalSeconds:F0}s.", null); } catch (LibPlcTagException ex) { // LibPlcTagException carries the Status; classify as reachable vs transport failure. if (IsReachableException(ex)) { sw.Stop(); return new(true, "PCCC session OK (controller reachable; probe tag not found)", sw.Elapsed); } return new(false, $"Reachable at {host}:{port} but PCCC handshake failed: {ex.Message}", null); } catch (Exception ex) { return new(false, $"Reachable at {host}:{port} but PCCC handshake failed: {ex.Message}", null); } finally { rt.Dispose(); } } /// /// Returns true for libplctag statuses that indicate the PCCC controller answered /// but the probe tag name was not found or did not match — sufficient to confirm PCCC /// reachability. Returns true for (tag was found). /// private static bool IsReachableStatus(Status s) => s is Status.Ok or Status.ErrorNotFound or Status.ErrorNoMatch or Status.ErrorBadDevice; /// /// Inspects a message to decide whether the error is a /// "controller answered PCCC but tag not found" condition (reachable) versus a session / /// transport failure (unreachable). Falls back to checking the exception message for /// known tag-not-found phrases used by the libplctag native library. /// private static bool IsReachableException(LibPlcTagException ex) { // libplctag wraps the Status in the message as "STATUS_NOT_FOUND", "STATUS_NO_MATCH", etc. var msg = ex.Message ?? string.Empty; return msg.Contains("NOT_FOUND", StringComparison.OrdinalIgnoreCase) || msg.Contains("NO_MATCH", StringComparison.OrdinalIgnoreCase) || msg.Contains("BAD_DEVICE", StringComparison.OrdinalIgnoreCase); } private static (AbLegacyHostAddress? parsed, AbLegacyDeviceOptions? device) ExtractTarget(AbLegacyDriverOptions opts) { // Parse the first device's ab:// host address to extract the gateway IP + EIP port. var firstDevice = opts.Devices.FirstOrDefault(); if (firstDevice is null) return (null, null); var parsed = AbLegacyHostAddress.TryParse(firstDevice.HostAddress); return (parsed, firstDevice); } }