diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/DataConditionProtoMapper.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/DataConditionProtoMapper.cs
new file mode 100644
index 000000000..1ead97ca7
--- /dev/null
+++ b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/DataConditionProtoMapper.cs
@@ -0,0 +1,108 @@
+using Google.Protobuf.WellKnownTypes;
+using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
+using W = ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1;
+
+namespace ZB.MOM.WW.OtOpcUa.Commons.DataConnections;
+
+///
+/// A2 condition codecs (plan §4.2, §5.5): CapabilitiesResponse.conditions, SubscriptionItem.condition_scope
+/// and the condition_states frame arm (9). Only ever exercised on a call that negotiated conditions: the
+/// host strips every one of these shapes from a header-less call, so a v1 client never sees them.
+///
+public static class DataConditionProtoMapper
+{
+ /// The most condition records one decoded frame may carry.
+ public const int MaxRecordsPerFrame = 5000;
+
+ /// Encodes the capabilities field.
+ public static W.ConditionCapabilities EncodeCapabilities(DataConditionCapabilities x) =>
+ new() { RootScopeItems = x.RootScopeItems, Methods = x.Methods, MaxConditions = x.MaxConditions };
+
+ /// Decodes the capabilities field (null when absent).
+ public static DataConditionCapabilities? DecodeCapabilities(W.ConditionCapabilities? x)
+ {
+ if (x is null) return null;
+ var decoded = new DataConditionCapabilities(x.RootScopeItems, x.Methods, x.MaxConditions);
+ decoded.Validate();
+ return decoded;
+ }
+
+ /// Encodes one condition record.
+ public static W.ConditionState EncodeState(DataConditionState x)
+ {
+ var v = new W.ConditionState
+ {
+ ConditionKey = x.ConditionKey, Version = x.Version, Removed = x.Removed, StateKnown = x.StateKnown,
+ Active = x.Active, Acknowledged = x.Acknowledged, Confirmed = x.Confirmed, Enabled = x.Enabled,
+ Shelving = x.Shelving, Severity = x.Severity, Message = x.Message ?? "", Quality = x.Quality,
+ ConditionClass = x.ConditionClass, Retain = x.Retain,
+ };
+ if (x.Info is { } info)
+ {
+ v.Info = new W.ConditionInfo
+ {
+ ConditionType = info.ConditionType, ConditionName = info.ConditionName, SourceKind = (int)info.SourceKind,
+ SupportedMethods = info.SupportedMethods,
+ };
+ v.Info.Placements.AddRange(info.Placements.Select(DataProtoMapper.EncodeView));
+ }
+ if (x.UnshelveAtUtc is { } unshelve) v.UnshelveAt = Timestamp.FromDateTimeOffset(unshelve);
+ if (x.TimeUtc is { } time) v.Time = Timestamp.FromDateTimeOffset(time);
+ if (x.Activation is { } a) v.Activation = new W.ActivationMarker { Incarnation = a.Incarnation, Sequence = a.Sequence };
+ if (x.Comment is not null) v.Comment = x.Comment;
+ if (x.ClientUserId is not null) v.ClientUserId = x.ClientUserId;
+ return v;
+ }
+
+ /// Decodes one condition record (a malformed key or negative version is an ).
+ public static DataConditionState DecodeState(W.ConditionState x)
+ {
+ if (!ImportedConditionIds.IsWellFormedConditionKey(x.ConditionKey) || x.Version < 0)
+ throw new ArgumentException("Invalid condition record.");
+ DataConditionInfo? info = null;
+ if (x.Info is { } i)
+ {
+ var kind = (DataConditionSourceKind)i.SourceKind;
+ if (!System.Enum.IsDefined(kind)) kind = DataConditionSourceKind.Unspecified;
+ info = new DataConditionInfo(i.ConditionType, i.ConditionName, kind,
+ i.Placements.Select(DataProtoMapper.DecodeView).ToArray(), i.SupportedMethods);
+ }
+ return new DataConditionState(x.ConditionKey, x.Version, x.Removed, x.StateKnown, info, x.Active, x.Acknowledged,
+ x.Confirmed, x.Enabled, x.Shelving, x.Severity, x.Message, x.Quality, x.UnshelveAt?.ToDateTimeOffset(),
+ x.ConditionClass, x.Time?.ToDateTimeOffset(), x.Retain,
+ x.Activation is { } m ? new DataActivationMarker(m.Incarnation, m.Sequence) : null,
+ x.HasComment ? x.Comment : null, x.HasClientUserId ? x.ClientUserId : null);
+ }
+
+ /// Encodes a condition frame (arm 9). The host calls this only on a call that negotiated conditions.
+ public static W.SubscriptionFrame EncodeFrame(DataSubscriptionConditions frame)
+ {
+ ArgumentNullException.ThrowIfNull(frame);
+ var states = new W.ConditionStates
+ {
+ Handle = DataProtoMapper.EncodeHandle(frame.Handle), IsSnapshot = frame.IsSnapshot, SnapshotComplete = frame.SnapshotComplete,
+ };
+ if (frame.Total is { } total) states.Total = total;
+ states.Items.AddRange(frame.Items.Select(EncodeState));
+ return new W.SubscriptionFrame
+ {
+ SubscriptionId = frame.SubscriptionId, StreamEpoch = frame.StreamEpoch, Sequence = frame.Sequence, ConditionStates = states,
+ };
+ }
+
+ /// Decodes a condition frame; the handle must belong to the frame's stream.
+ public static DataSubscriptionConditions DecodeFrame(W.SubscriptionFrame frame)
+ {
+ ArgumentNullException.ThrowIfNull(frame);
+ var c = frame.ConditionStates ?? throw new ArgumentException("Expected condition states.");
+ ArgumentException.ThrowIfNullOrWhiteSpace(frame.SubscriptionId);
+ if (frame.StreamEpoch <= 0 || frame.Sequence <= 0 || c.Handle is null || c.Items.Count > MaxRecordsPerFrame
+ || c.HasTotal && c.Total < 0)
+ throw new ArgumentException("Invalid condition frame.");
+ var handle = DataProtoMapper.DecodeHandle(c.Handle);
+ if (handle.SubscriptionId != frame.SubscriptionId || handle.StreamEpoch != frame.StreamEpoch)
+ throw new ArgumentException("Condition handle belongs to another stream.");
+ return new DataSubscriptionConditions(frame.SubscriptionId, frame.StreamEpoch, frame.Sequence, handle, c.IsSnapshot,
+ c.HasTotal ? c.Total : null, c.Items.Select(DecodeState).ToArray(), c.SnapshotComplete);
+ }
+}
diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/DataProtoMapper.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/DataProtoMapper.cs
index 6d15d85ef..f5eac65af 100644
--- a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/DataProtoMapper.cs
+++ b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/DataProtoMapper.cs
@@ -304,10 +304,13 @@ public static class DataProtoMapper
ClientItemId = x.ClientItemId,
View = EncodeView(x.View),
Settings = EncodeSettings(x.Settings),
+ // A2: only a caller that negotiated `conditions` ever sets this (the host strips it otherwise).
+ ConditionScope = x.ConditionScope is { } scope ? new W.ConditionScope { IncludeInfo = scope.IncludeInfo } : null,
};
public static DataSubscriptionItem DecodeSubscriptionItem(W.SubscriptionItem x) =>
- new(x.ClientItemId, DecodeView(x.View), DecodeSettings(x.Settings));
+ new(x.ClientItemId, DecodeView(x.View), DecodeSettings(x.Settings),
+ x.ConditionScope is { } scope ? new DataConditionScope(scope.IncludeInfo) : null);
public static W.SubscriptionCommand EncodeCommand(DataSubscriptionCommand x)
{
@@ -1037,6 +1040,9 @@ public static class DataProtoMapper
// H2: the service strips this from a call that did not negotiate history.
if (x.History is { } history)
v.History = DataHistoryProtoMapper.EncodeCapabilities(history);
+ // A2: the service strips this (and the AlarmSubscribe bit) from a call that did not negotiate conditions.
+ if (x.Conditions is { } conditions)
+ v.Conditions = DataConditionProtoMapper.EncodeCapabilities(conditions);
return v;
}
@@ -1062,7 +1068,9 @@ public static class DataProtoMapper
r,
// H2: HistoryRead without the history capabilities field is no grant (the two only ever travel together).
(DataOperations)(x.Operations & (uint)DataOperationSets.Understood
- & (DataHistoryProtoMapper.DecodeCapabilities(x.History) is null ? ~(uint)DataOperations.HistoryRead : uint.MaxValue)),
+ & (DataHistoryProtoMapper.DecodeCapabilities(x.History) is null ? ~(uint)DataOperations.HistoryRead : uint.MaxValue)
+ // A2: likewise AlarmSubscribe without the conditions field.
+ & (x.Conditions is null ? ~(uint)DataOperations.AlarmSubscribe : uint.MaxValue)),
kinds,
new DataLimits(
l.MaxPageItems,
@@ -1084,7 +1092,8 @@ public static class DataProtoMapper
x.WriteServingGenerationEnforced,
x.SubscriptionFreshnessChallenges,
x.MetadataItemLeases,
- DataHistoryProtoMapper.DecodeCapabilities(x.History)
+ DataHistoryProtoMapper.DecodeCapabilities(x.History),
+ DataConditionProtoMapper.DecodeCapabilities(x.Conditions)
);
result.Validate();
return result;
diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/Protos/data_connection.proto b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/Protos/data_connection.proto
index 28a230c64..9c58c4c00 100644
--- a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/Protos/data_connection.proto
+++ b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/Protos/data_connection.proto
@@ -123,8 +123,17 @@ message CapabilitiesResponse {
bool metadata_item_leases = 15;
// H2: only on a call that negotiated `history`, and only when this origin has a historian backend.
HistoryCapabilities history = 16;
- // X0 RESERVATION (not protobuf `reserved`: the number WILL be used): 17 = ConditionCapabilities conditions (A2).
- // Pinned by DataConnectionProtoContract + GrpcDataProtoReservationTests.
+ // A2: only on a call that negotiated `conditions`, and only when this origin can serve them (a condition-key secret
+ // is provisioned and the condition-state hub is wired). Pinned by DataConnectionProtoContract + GrpcDataProtoReservationTests.
+ ConditionCapabilities conditions = 17;
+}
+// A2 (plan §4.2/§5.5): what the origin's condition streams offer this caller.
+message ConditionCapabilities {
+ bool root_scope_items = 1;
+ // Bitmask of supported condition methods (A2b); 0 in A2a.
+ uint32 methods = 2;
+ // The most conditions one root-scoped item admits (the export's MaxConditions); 0 = undisclosed (no grant).
+ int32 max_conditions = 3;
}
message HistoryCapabilities {
bool raw = 1;
@@ -245,7 +254,46 @@ message SubscriptionItem {
string client_item_id = 1;
View view = 2;
Settings settings = 3;
- // X0 RESERVATION: 4 = ConditionScope condition_scope (A2, header-gated). Pinned by GrpcDataProtoReservationTests.
+ // A2 (header-gated): one root-scoped item = "all conditions under this export root"; view = the mapping's remote root.
+ ConditionScope condition_scope = 4;
+}
+message ConditionScope { bool include_info = 1; }
+message ConditionInfo {
+ string condition_type = 1; // namespace-URI qualified
+ string condition_name = 2;
+ int32 source_kind = 3; // 1 = NATIVE, 2 = SCRIPTED
+ repeated View placements = 4; // realm-local placement addresses inside THIS export (§5.2), never cross-realm
+ uint32 supported_methods = 5;
+}
+message ActivationMarker { string incarnation = 1; uint64 sequence = 2; }
+message ConditionState {
+ string condition_key = 1; // opaque keyed hash (§5.2)
+ int64 version = 2;
+ bool removed = 3;
+ bool state_known = 4;
+ ConditionInfo info = 5; // first appearance in an epoch and in every snapshot record
+ bool active = 6;
+ bool acknowledged = 7;
+ bool confirmed = 8;
+ bool enabled = 9;
+ int32 shelving = 10;
+ uint32 severity = 11;
+ string message = 12;
+ uint32 quality = 13;
+ google.protobuf.Timestamp unshelve_at = 14;
+ int32 condition_class = 15;
+ google.protobuf.Timestamp time = 16;
+ bool retain = 17;
+ ActivationMarker activation = 18;
+ optional string comment = 19;
+ optional string client_user_id = 20;
+}
+message ConditionStates {
+ Handle handle = 1; // the root-scoped item's handle
+ bool is_snapshot = 2;
+ optional int32 total = 3; // on the snapshot-begin chunk, before any record (admission before materialising)
+ repeated ConditionState items = 4;
+ bool snapshot_complete = 5;
}
message SubscriptionCommand {
string subscription_id = 1;
@@ -340,8 +388,9 @@ message SubscriptionFrame {
Control control = 5;
Values values = 6;
CompactValues compact_values = 7;
- // X0 RESERVATION: oneof arm 9 = ConditionStates condition_states (A2). Only ever sent on a call that
- // negotiated `conditions` through x-otopcua-data-features: v1 clients fault on an unknown frame arm.
+ // A2: only ever sent on a call that negotiated `conditions` through x-otopcua-data-features (v1 clients fault on
+ // an unknown frame arm), and only for a root-scoped condition item that call opened.
+ ConditionStates condition_states = 9;
}
}
// H2 remote history (plan §4.2). An unset start/end is ABSENT, never MinValue: the origin's end-only rules depend on it.
diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/DataConditionContracts.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/DataConditionContracts.cs
new file mode 100644
index 000000000..f75db0b39
--- /dev/null
+++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/DataConditionContracts.cs
@@ -0,0 +1,56 @@
+namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
+
+// ---------------------------------------------------------------------------------------------------------------------
+// A2 condition streaming (grpc-data history/alarms plan §4.2, §5.5). Root-scoped condition items on Subscribe: one item
+// per mapping = "all conditions under this export root". Negotiated per call through x-otopcua-data-features
+// `conditions`; a call without it never sees any of these shapes.
+// ---------------------------------------------------------------------------------------------------------------------
+
+/// What an origin's condition streams offer (CapabilitiesResponse.conditions); null when not negotiated
+/// or not servable (no condition-key secret, no condition-state hub).
+/// Root-scoped condition items are accepted.
+/// Supported condition methods (bitmask; A2b). 0 in A2a.
+/// The most conditions one root-scoped item admits for this caller (the largest
+/// MaxConditions of its AlarmSubscribe exports); 0 = undisclosed (no grant).
+public sealed record DataConditionCapabilities(bool RootScopeItems, uint Methods, int MaxConditions)
+{
+ public void Validate()
+ {
+ if (MaxConditions < 0) throw new ArgumentOutOfRangeException(nameof(MaxConditions));
+ }
+}
+
+/// A root-scoped condition item (SubscriptionItem.condition_scope); the item's view is the export root.
+/// Carry on first appearance and in every snapshot record.
+public sealed record DataConditionScope(bool IncludeInfo);
+
+/// Where an origin condition comes from (wire ConditionInfo.source_kind).
+public enum DataConditionSourceKind { Unspecified = 0, Native = 1, Scripted = 2 }
+
+/// Static facts about one condition, carried on first appearance in an epoch and in every snapshot record.
+/// The condition type, namespace-URI qualified.
+/// The Part 9 ConditionName.
+/// Native (driver-fed) or scripted.
+/// Realm-local placement addresses inside the listing export (§5.2), never cross-realm.
+/// The methods this condition supports (A2b).
+public sealed record DataConditionInfo(string ConditionType, string ConditionName, DataConditionSourceKind SourceKind,
+ IReadOnlyList Placements, uint SupportedMethods);
+
+/// The origin-owned activation marker (T3): (serving incarnation, per-condition occurrence sequence).
+public sealed record DataActivationMarker(string Incarnation, ulong Sequence);
+
+/// One condition record of a root-scoped item (ConditionState).
+/// A record with = false never changes a consumer's last state (§5.1 T2).
+public sealed record DataConditionState(string ConditionKey, long Version, bool Removed, bool StateKnown, DataConditionInfo? Info,
+ bool Active, bool Acknowledged, bool Confirmed, bool Enabled, int Shelving, uint Severity, string Message, uint Quality,
+ DateTimeOffset? UnshelveAtUtc, int ConditionClass, DateTimeOffset? TimeUtc, bool Retain, DataActivationMarker? Activation,
+ string? Comment = null, string? ClientUserId = null);
+
+///
+/// A condition frame (SubscriptionFrame.condition_states, oneof arm 9) for one root-scoped item. A snapshot begins
+/// with and set and no records (so capacity is decided before anything is
+/// materialised); records follow; ends it. Later frames are delta records.
+///
+public sealed record DataSubscriptionConditions(string SubscriptionId, long StreamEpoch, long Sequence, DataItemHandle Handle,
+ bool IsSnapshot, int? Total, IReadOnlyList Items, bool SnapshotComplete)
+ : DataSubscriptionFrame(SubscriptionId, StreamEpoch, Sequence);
diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/DataContracts.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/DataContracts.cs
index ab6acd335..095a5b5fe 100644
--- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/DataContracts.cs
+++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/DataContracts.cs
@@ -318,7 +318,7 @@ public sealed record DataCapabilities(int ProtocolMajor, int ProtocolMinor, Serv
DataOperations Operations, IReadOnlySet ValueKinds, DataLimits Limits, bool CompactBatches,
int CompactCodecVersion, string ClusterId, bool SubscriptionAgeReadiness = false, bool ResponseServingIdentity = false,
bool WriteServingGenerationEnforced = false, bool SubscriptionFreshnessChallenges = false,
- bool MetadataItemLeases = false, DataHistoryCapabilities? History = null)
+ bool MetadataItemLeases = false, DataHistoryCapabilities? History = null, DataConditionCapabilities? Conditions = null)
{
private readonly DataOperations _operations = Operations & DataOperationSets.Understood;
/// Masked to on construction and on every with.
@@ -344,6 +344,7 @@ public sealed record DataCapabilities(int ProtocolMajor, int ProtocolMinor, Serv
}
// Operations is masked by its accessor, so unknown bits from a newer peer never reach here (X0).
History?.Validate();
+ Conditions?.Validate();
}
}
public sealed record DataMetadata(DataView View, string BrowseName, string NodeKind, DataValueKind ValueKind, int ValueRank,
diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/IDataServices.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/IDataServices.cs
index cf9514bf7..a46dafb16 100644
--- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/IDataServices.cs
+++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/IDataServices.cs
@@ -85,7 +85,9 @@ public sealed record DataSubscriptionSettings(TimeSpan SamplingInterval, TimeSpa
}
}
}
-public sealed record DataSubscriptionItem(string ClientItemId, DataView View, DataSubscriptionSettings Settings);
+/// A subscription item. (A2, header-gated) makes it a root-scoped condition item.
+public sealed record DataSubscriptionItem(string ClientItemId, DataView View, DataSubscriptionSettings Settings,
+ DataConditionScope? ConditionScope = null);
public abstract record DataSubscriptionCommand(string SubscriptionId, long StreamEpoch, string CommandId, TimeSpan AcknowledgementTimeout);
public sealed record OpenSubscription(string SubscriptionId, long StreamEpoch, string CommandId, TimeSpan AcknowledgementTimeout,
IReadOnlyList Items, bool WatchCatalogOnly, TimeSpan SessionLifetime, DataBudget SnapshotBudget)