namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;
///
/// Serves a bit-indexed BOOL tag (MAIN.Flags.3, .ADSSTATE_RUN.0) from its PARENT word.
/// ADS has no symbol for WORD.N — live on a TwinCAT 2.11 target a notification registered on
/// .ADSSTATE_RUN.0 failed with DeviceSymbolNotFound — so a native notification registers
/// the parent as raw bytes at its resolved width and reads the bit here, exactly as a read already
/// reads the parent word and extracts the bit.
///
internal static class TwinCATBitAccess
{
/// The parent word's path: the bit path without its trailing .N.
/// The bit path (MAIN.Flags.3).
/// The parent path, or when it has no numeric last segment.
public static string ParentPath(string symbolPath)
{
var lastDot = symbolPath.LastIndexOf('.');
if (lastDot < 0) return symbolPath;
return int.TryParse(symbolPath.AsSpan(lastDot + 1), out _) ? symbolPath[..lastDot] : symbolPath;
}
/// Checks that addresses a bit of an integer parent word.
/// The bit index.
/// The parent symbol's shape.
/// null when addressable; otherwise the reason.
public static string? CheckParent(int bit, TwinCATPlcSymbolShape parent)
{
if (parent.Kind != TwinCATPlcKind.Scalar || parent.IsFloat)
return $"bit {bit} of {parent.TypeName} ({parent.ByteSize} B) — only an integer word has addressable bits";
return bit >= 0 && bit < parent.ByteSize * 8
? null
: $"bit {bit} is outside the {parent.ByteSize * 8}-bit parent {parent.TypeName}";
}
///
/// The modify step of a bit write's read-modify-write: checks the bit against the parent
/// () and returns the parent's bytes — exactly parent.ByteSize of them —
/// with one bit set or cleared. Works on raw bytes, so the parent's width and signedness play no part.
///
/// The parent symbol's resolved shape.
/// The parent's current bytes as read from the PLC.
/// The bit index.
/// The bit value.
/// The bytes to write back when the result is Good; otherwise empty.
/// Good; BadOutOfRange for a bit beyond the parent; BadTypeMismatch for a parent
/// that is not an integer word; BadCommunicationError for a read shorter than the parent
/// (writing it back would zero the missing bytes).
public static uint ApplyBitWrite(TwinCATPlcSymbolShape parent, ReadOnlySpan current, int bit, bool value,
out byte[] updated)
{
updated = [];
if (CheckParent(bit, parent) is not null)
return parent.Kind != TwinCATPlcKind.Scalar || parent.IsFloat
? TwinCATStatusMapper.BadTypeMismatch
: TwinCATStatusMapper.BadOutOfRange;
if (current.Length < parent.ByteSize) return TwinCATStatusMapper.BadCommunicationError;
var bytes = current[..parent.ByteSize].ToArray();
var mask = (byte)(1 << (bit % 8));
bytes[bit / 8] = value ? (byte)(bytes[bit / 8] | mask) : (byte)(bytes[bit / 8] & ~mask);
updated = bytes;
return TwinCATStatusMapper.Good;
}
/// Reads bit of a little-endian parent word.
/// The parent word's raw bytes.
/// The bit index (0 = least significant bit of the first byte).
/// The bit's value.
public static bool ReadBit(ReadOnlySpan parent, int bit) =>
(parent[bit / 8] & (1 << (bit % 8))) != 0;
}