feat(ab-transport): Connection Manager, MSP, Logix read/write, symbol and template service codecs
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Wire;
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Cip;
|
||||
|
||||
/// <summary>Parameters for a (Large) Forward Open. <see cref="ConnectionSize"/> is the maximum CIP
|
||||
/// connection payload in bytes (e.g. 4002 for Large, 504 for regular).</summary>
|
||||
public sealed record ForwardOpenParams(
|
||||
bool Large, uint TOConnectionId, ushort ConnectionSerial, ushort VendorId, uint OriginatorSerial,
|
||||
byte TimeoutMultiplier, uint RpiMicros, int ConnectionSize, byte[] Route);
|
||||
|
||||
public sealed record ForwardOpenReply(uint OTConnectionId, uint TOConnectionId, uint OTApiMicros, uint TOApiMicros);
|
||||
|
||||
/// <summary>Connection Manager (class 6) request builders and reply parsers.</summary>
|
||||
public static class ConnectionManagerServices
|
||||
{
|
||||
/// <summary>Priority/time-tick byte.</summary>
|
||||
public const byte PriorityTick = 0x0A;
|
||||
|
||||
/// <summary>Timeout ticks. The brief proposed 0x0E; every libplctag capture (Forward Open, Forward
|
||||
/// Close, Unconnected Send) carries 0x05, and the capture wins.</summary>
|
||||
public const byte TimeoutTicks = 0x05;
|
||||
|
||||
public static byte[] UnconnectedSend(ReadOnlySpan<byte> embedded, ReadOnlySpan<byte> route)
|
||||
{
|
||||
var w = new CipWriter().U8(0x52).U8(2).Bytes(CipPath.ConnectionManager)
|
||||
.U8(PriorityTick).U8(TimeoutTicks).U16((ushort)embedded.Length).Bytes(embedded);
|
||||
if ((embedded.Length & 1) == 1) w.U8(0);
|
||||
return w.U8((byte)(route.Length / 2)).U8(0).Bytes(route).ToArray();
|
||||
}
|
||||
|
||||
public static byte[] ForwardOpen(ForwardOpenParams p)
|
||||
{
|
||||
var connPath = new CipWriter().Bytes(p.Route).Bytes(CipPath.MessageRouter).ToArray();
|
||||
var w = new CipWriter().U8(p.Large ? (byte)0x5B : (byte)0x54).U8(2).Bytes(CipPath.ConnectionManager)
|
||||
.U8(PriorityTick).U8(TimeoutTicks)
|
||||
.U32(0).U32(p.TOConnectionId).U16(p.ConnectionSerial).U16(p.VendorId).U32(p.OriginatorSerial)
|
||||
.U8(p.TimeoutMultiplier).U8(0).U8(0).U8(0);
|
||||
WriteSide(w, p);
|
||||
WriteSide(w, p);
|
||||
return w.U8(0xA3).U8((byte)(connPath.Length / 2)).Bytes(connPath).ToArray();
|
||||
}
|
||||
|
||||
private static void WriteSide(CipWriter w, ForwardOpenParams p)
|
||||
{
|
||||
w.U32(p.RpiMicros);
|
||||
if (p.Large) w.U32(0x42000000u | (uint)p.ConnectionSize);
|
||||
else w.U16((ushort)(0x4200 | (p.ConnectionSize & 0x1FF)));
|
||||
}
|
||||
|
||||
public static ForwardOpenReply ParseForwardOpenReply(CipReply reply)
|
||||
{
|
||||
if (reply.GeneralStatus != 0)
|
||||
throw new AbTransportException(reply.Status == AbStatus.BadReply ? AbStatus.BadConnection : reply.Status,
|
||||
$"Forward Open refused: general 0x{reply.GeneralStatus:X2}, extended {string.Join(",", reply.ExtendedStatus.Select(e => $"0x{e:X4}"))}.");
|
||||
var r = new CipReader(reply.Data.Span);
|
||||
var ot = r.U32(); var to = r.U32(); r.U16(); r.U16(); r.U32();
|
||||
return new ForwardOpenReply(ot, to, r.U32(), r.U32());
|
||||
}
|
||||
|
||||
/// <summary>True when a refused Large Forward Open should be retried as a regular one: general 0x08
|
||||
/// (Service Not Supported — pre-v20 Logix) or general 0x01 / extended 0x0109 (invalid connection size).</summary>
|
||||
public static bool ShouldFallBackToRegularOpen(CipReply r) =>
|
||||
r.GeneralStatus == 0x08
|
||||
|| (r.GeneralStatus == 0x01 && r.ExtendedStatus.Length > 0 && r.ExtendedStatus[0] == 0x0109);
|
||||
|
||||
public static byte[] ForwardClose(ushort connectionSerial, ushort vendorId, uint originatorSerial, ReadOnlySpan<byte> route)
|
||||
{
|
||||
var connPath = new CipWriter().Bytes(route).Bytes(CipPath.MessageRouter).ToArray();
|
||||
return new CipWriter().U8(0x4E).U8(2).Bytes(CipPath.ConnectionManager).U8(PriorityTick).U8(TimeoutTicks)
|
||||
.U16(connectionSerial).U16(vendorId).U32(originatorSerial)
|
||||
.U8((byte)(connPath.Length / 2)).U8(0).Bytes(connPath).ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user