namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus;

/// <summary>
///     Abstraction over the Modbus TCP socket. Takes a <c>PDU</c> (function code + data, excluding
///     the 7-byte MBAP header) and returns the response PDU — the transport owns transaction-id
///     pairing, framing, and socket I/O. Tests supply in-memory fakes.
/// </summary>
public interface IModbusTransport : IAsyncDisposable
{
    Task ConnectAsync(CancellationToken ct);

    /// <summary>
    ///     Send a Modbus PDU (function code + function-specific data) and read the response PDU.
    ///     Throws <see cref="ModbusException"/> when the server returns an exception PDU
    ///     (function code + 0x80 + exception code).
    /// </summary>
    Task<byte[]> SendAsync(byte unitId, byte[] pdu, CancellationToken ct);
}

public sealed class ModbusException(byte functionCode, byte exceptionCode, string message)
    : Exception(message)
{
    public byte FunctionCode { get; } = functionCode;
    public byte ExceptionCode { get; } = exceptionCode;
}
