fix(focas): #1036 a short macro/parameter payload publishes BadTypeMismatch, not BadCommunicationError

This commit is contained in:
Joseph Doherty
2026-12-03 14:56:25 -05:00
parent da467c9675
commit 52d2acad25
5 changed files with 56 additions and 5 deletions
+1 -1
View File
@@ -640,7 +640,7 @@ test fixtures (`tests/.../Driver.FOCAS.Tests/Fixtures/v3/`). Client-visible chan
| What | Before | Now |
|------|--------|-----|
| `cnc_rdmacro` decimal | read at payload offset 4 — a constant `10` length field — so `#500 = 1.0` published `0.01` | `dec_val` at offset 6; `mantissa / 10^dec`. Vacant variable → `BadNoData`; fractional value on an integer tag → `BadTypeMismatch`; integer tags receive the exact whole value |
| `cnc_rdmacro` decimal | read at payload offset 4 — a constant `10` length field — so `#500 = 1.0` published `0.01` | `dec_val` at offset 6; `mantissa / 10^dec`. Vacant variable → `BadNoData`; fractional value on an integer tag → `BadTypeMismatch`; integer tags receive the exact whole value. A reply too short to decode (macro, parameter) is `BadTypeMismatch` on that tag with no health impact: the CNC answered and the session is intact (#1036, `FocasWirePayloadException`) |
| Axis positions | scaled by `cnc_getfigure` (0x00D3), which does not exist — on hardware: raw counts | scaled by each block's own `base^exponent`; undescribable block → `BadNoData` |
| Axis / spindle names | first 2 of 4 record bytes — `AA6`/`AA7` collapsed to `AA` | whole record up to the first NUL (residue after it discarded) — ⚠️ `Axes/AA_1`→`Axes/AA6` on such a machine |
| `ServoLoad` | `value / 10^base` (offset 4 is the base, 10) — always ~0 | `value / base^exponent`, a direct percentage |
@@ -1362,7 +1362,7 @@ public sealed class FocasWireClient : IAsyncDisposable, IDisposable
private static void RequireLength(byte[] payload, int length, string call)
{
if (payload.Length < length)
throw new FocasWireException($"{call} returned {payload.Length} bytes; expected at least {length}.", rc: null);
throw new FocasWirePayloadException($"{call} returned {payload.Length} bytes; expected at least {length}.");
}
private static bool IsTransientException(Exception exception)
@@ -62,3 +62,19 @@ public class FocasWireException : Exception
IsTransient = isTransient;
}
}
/// <summary>
/// A well-formed reply whose payload is too short to decode (#1036). The CNC answered and the session is
/// intact, so this is a decode refusal for the one tag - not a comms fault - and maps to
/// <c>BadTypeMismatch</c> rather than <c>BadCommunicationError</c> (which would degrade driver health and feed
/// the #527 watchdog over a shape problem that no reconnect can fix).
/// </summary>
public sealed class FocasWirePayloadException : FocasWireException
{
/// <summary>Initializes a new short/malformed-payload exception.</summary>
/// <param name="message">The exception message.</param>
public FocasWirePayloadException(string message)
: base(message, rc: null)
{
}
}
@@ -403,7 +403,7 @@ public sealed class WireFocasClient : IFocasClient
}
catch (FocasWireException ex)
{
return (null, ex.Rc is short rc ? FocasStatusMapper.MapFocasReturn(rc) : FocasStatusMapper.BadCommunicationError);
return (null, MapWireFailure(ex));
}
}
@@ -419,7 +419,7 @@ public sealed class WireFocasClient : IFocasClient
}
catch (FocasWireException ex)
{
return (null, ex.Rc is short rc ? FocasStatusMapper.MapFocasReturn(rc) : FocasStatusMapper.BadCommunicationError);
return (null, MapWireFailure(ex));
}
}
@@ -460,6 +460,20 @@ public sealed class WireFocasClient : IFocasClient
};
}
/// <summary>
/// The status a wire failure publishes: the CNC's own rc when it sent one; <c>BadTypeMismatch</c> for a reply
/// whose payload is too short to decode (#1036 - the session is intact and the CNC answered, so a shape problem
/// is a per-tag refusal, never a comms fault); otherwise <c>BadCommunicationError</c>.
/// </summary>
/// <param name="ex">The wire failure.</param>
/// <returns>The OPC UA status to publish for the tag.</returns>
internal static uint MapWireFailure(FocasWireException ex)
=> ex.Rc is short rc
? FocasStatusMapper.MapFocasReturn(rc)
: ex is FocasWirePayloadException
? FocasStatusMapper.BadTypeMismatch
: FocasStatusMapper.BadCommunicationError;
private async Task<(object? value, uint status)> ReadMacroAsync(
FocasAddress address, FocasDataType type, CancellationToken cancellationToken)
{
@@ -472,7 +486,7 @@ public sealed class WireFocasClient : IFocasClient
}
catch (FocasWireException ex)
{
return (null, ex.Rc is short rc ? FocasStatusMapper.MapFocasReturn(rc) : FocasStatusMapper.BadCommunicationError);
return (null, MapWireFailure(ex));
}
}
@@ -95,6 +95,27 @@ public sealed class FocasMacroDecodeTests
Should.Throw<FocasWireException>(() => FocasWireClient.ParseMacro(1, new byte[length]));
}
// #1036: a short payload is a decode refusal on a live session, not a comms fault.
[Theory]
[InlineData(0)]
[InlineData(7)]
public void A_short_macro_payload_publishes_BadTypeMismatch_not_BadCommunicationError(int length)
{
var ex = Should.Throw<FocasWireException>(() => FocasWireClient.ParseMacro(1, new byte[length]));
WireFocasClient.MapWireFailure(ex).ShouldBe(FocasStatusMapper.BadTypeMismatch);
}
[Fact]
public void Wire_failures_without_a_payload_shape_problem_keep_their_comms_mapping()
{
WireFocasClient.MapWireFailure(new FocasWireException("link down", rc: null))
.ShouldBe(FocasStatusMapper.BadCommunicationError);
WireFocasClient.MapWireFailure(new FocasWireException("closed", new System.IO.IOException(), isTransient: true))
.ShouldBe(FocasStatusMapper.BadCommunicationError);
WireFocasClient.MapWireFailure(new FocasWireException("refused", rc: 7))
.ShouldBe(FocasStatusMapper.MapFocasReturn(7));
}
// ---- published value / quality ----
[Fact]