fix(abcip): #983 whole BOOL-array read counts DWORDs (P-13); probe leases with the driver timeout (P-22)

This commit is contained in:
Joseph Doherty
2026-12-03 09:50:16 -05:00
parent eff7775bca
commit c5c31a1671
4 changed files with 213 additions and 5 deletions
@@ -721,7 +721,11 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
private async Task ProbeLoopAsync(DeviceState state, CancellationToken ct)
{
var probeParams = state.BuildCreateParams(_options.Probe.ProbeTagPath!, _options.Probe.Timeout);
// P-22: the connection's timeout is part of its pooled identity and the first acquirer's options win, so the
// probe (which creates the connection first and again after every recycle) must lease with the DRIVER's
// timeout — a shorter probe timeout silently became every data read's timeout. The probe's own timeout
// bounds only its wait, below.
var probeParams = state.BuildCreateParams(_options.Probe.ProbeTagPath!, _options.Timeout);
IAbCipTagRuntime? probeRuntime = null;
while (!ct.IsCancellationRequested)
@@ -735,14 +739,18 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
try
{
probeRuntime ??= _tagFactory.Create(probeParams);
// The probe's own timeout bounds this tick's wait (cancelling a waiter never faults the shared
// session — scheduler B2); the pooled connection keeps the driver's timeout.
using var tick = CancellationTokenSource.CreateLinkedTokenSource(ct);
tick.CancelAfter(_options.Probe.Timeout);
// Lazy-init on first attempt; re-init after a transport failure has caused the
// native handle to be destroyed.
if (!state.ProbeInitialized)
{
await probeRuntime.InitializeAsync(ct).ConfigureAwait(false);
await probeRuntime.InitializeAsync(tick.Token).ConfigureAwait(false);
state.ProbeInitialized = true;
}
await probeRuntime.ReadAsync(ct).ConfigureAwait(false);
await probeRuntime.ReadAsync(tick.Token).ConfigureAwait(false);
var status = probeRuntime.GetStatus();
if (status == 0)
{
@@ -96,8 +96,22 @@ internal sealed class NativeAbCipTagRuntime : IAbCipTagRuntime
{
_p = p ?? throw new ArgumentNullException(nameof(p));
_elements = (ushort)Math.Clamp(p.ElementCount, 1, ushort.MaxValue);
_isBoolArray = p.IsArray && p.AuthoredType == AbCipDataType.Bool;
// P-13: a Logix BOOL array is stored as DWORDs (0xD3) and its element count is in DWORDs, so a whole read of
// BOOL[64] asks for 2 elements, not 64. Packed until a reply type says otherwise (see ReadCoreAsync).
_wireElements = _isBoolArray ? PackedBoolElements(_elements) : _elements;
}
// P-13: whole BOOL-array read. _elements is the number of BOOLs authored; _wireElements is the element count
// actually sent: ceil(n / 32) DWORDs while the array is packed (0xD3), n when the controller answers per-element 0xC1.
private readonly bool _isBoolArray;
private ushort _wireElements;
/// <summary>DWORDs holding <paramref name="bits"/> packed BOOLs.</summary>
/// <param name="bits">The number of BOOL elements.</param>
/// <returns>ceil(bits / 32), at least 1.</returns>
internal static ushort PackedBoolElements(int bits) => (ushort)Math.Clamp((bits + 31) / 32, 1, ushort.MaxValue);
/// <summary>
/// Test seam: a runtime over <paramref name="channel"/> with a private scheduler and no pool
/// lease. Production code always goes through <see cref="NativeAbCipTagFactory"/>.
@@ -193,7 +207,19 @@ internal sealed class NativeAbCipTagRuntime : IAbCipTagRuntime
var path = Path();
try
{
var (type, handle, data) = await client.ReadAsync(path, _elements, cancellationToken, ExpectedElementBytes(_p.AuthoredType)).ConfigureAwait(false);
var (type, handle, data) = await client.ReadAsync(path, _wireElements, cancellationToken, ExpectedElementBytes(_p.AuthoredType)).ConfigureAwait(false);
if (_isBoolArray && SettleWholeBoolArrayCount(type))
{
// The reply showed the OTHER layout than the one this read was sized for: discard it and ask again
// with the right element count (a packed array's count is DWORDs, a per-element one's is BOOLs).
if (mayRelayout)
{
await ReadCoreAsync(cancellationToken, mayRelayout: false).ConfigureAwait(false);
return;
}
throw new AbTransportException(AbStatus.TypeMismatch,
$"'{_p.TagName}': the controller's BOOL-array layout changed between two reads; retrying next read.");
}
if (_boolElementBit is not null)
{
BoolLayout? shown = type == LogixTypeCode.BoolArray ? BoolLayout.Packed
@@ -230,6 +256,25 @@ internal sealed class NativeAbCipTagRuntime : IAbCipTagRuntime
}
}
/// <summary>
/// Re-sizes a whole BOOL-array read to the layout the reply TYPE showed: 0xD3 means packed DWORDs (count =
/// ceil(n / 32)), 0xC1 means one byte per element (count = n, e.g. ab_server). Any other type is no evidence.
/// </summary>
/// <param name="replyType">The reply's CIP type code.</param>
/// <returns><see langword="true"/> when the count had to change, so the reply just read is for the wrong size.</returns>
private bool SettleWholeBoolArrayCount(ushort replyType)
{
var wanted = replyType switch
{
LogixTypeCode.BoolArray => PackedBoolElements(_elements),
LogixTypeCode.Bool => _elements,
_ => _wireElements,
};
if (wanted == _wireElements) return false;
_wireElements = wanted;
return true;
}
/// <summary>Settles a BOOL-array element tag's layout and selects the matching request path.</summary>
private void SetBoolLayout(BoolLayout layout)
{
@@ -688,7 +733,10 @@ internal sealed class NativeAbCipTagRuntime : IAbCipTagRuntime
private bool[] BuildBoolArray(int count)
{
var arr = new bool[count];
for (var i = 0; i < count; i++) arr[i] = GetBit(i);
// A per-element array (0xC1, one byte per BOOL) is not bit-packed: element i is byte i.
var perElement = _typeCode == LogixTypeCode.Bool;
for (var i = 0; i < count; i++)
arr[i] = perElement ? Span(i, 1)[0] != 0 : GetBit(i);
return arr;
}