using ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip;
///
/// The ONE classification of a transport status into "the controller answered" vs "the wire failed", shared by
/// the data path, the probe loop, the Test-Connect probe and the native runtime's initialisation.
///
///
/// A device refusal is deterministic: the session works and the controller said no to this one tag (a renamed
/// tag, Arr[999], a read-protected tag, a type the authored config does not match). It is per-tag Bad,
/// keeps the tag runtime (evicting re-creates it — and, for a sole lease holder, re-Forward-Opens — on every poll),
/// never writes Degraded, never opens the device-wide connect backoff, and warns once per tag. Everything
/// else (timeout, bad connection, a reply that failed to parse, partial, abort, …) is a comms fault.
///
/// The native transport currently folds CIP general statuses 0x1C, 0x20 and 0xFF/0x2104 into
/// (AbStatusCodes.FromCip, owned by Driver.Ab.Transport), which
/// this classification cannot tell from a garbled reply; until the transport maps them explicitly they stay
/// comms faults.
///
///
internal static class AbCipRefusals
{
/// True when proves the controller answered but refused this tag.
/// The transport status.
/// for a deterministic device refusal.
public static bool IsDeviceRefusal(AbStatus status) => status is AbStatus.NotFound or AbStatus.OutOfBounds
or AbStatus.NotAllowed or AbStatus.TooLarge or AbStatus.TooSmall or AbStatus.TypeMismatch
or AbStatus.Unsupported;
/// True when (an value) is a device refusal.
/// The runtime status word.
/// for a deterministic device refusal.
public static bool IsDeviceRefusal(int status) => IsDeviceRefusal((AbStatus)status);
/// True when is an carrying a device refusal.
/// The exception.
/// for a deterministic device refusal.
/// An is never a device refusal: nothing reached the device.
public static bool IsDeviceRefusal(Exception ex) =>
ex is AbTransportException ate and not AbCipLocalRefusalException && IsDeviceRefusal(ate.Status);
}