Files
Marc Schier f66ceffd75 Immutable built in types and code generation (#3534)
Generate stack and application code using source generators
Change built in types to readonly structs
  Update localized text implementation to use JSON text serializer
  Status code with symbolic names
  Immutable node ids
  No boxing for all built in types <= 8 bytes in Variant.
Split user identity token and token handling via token handler
Add migration guide for breaking changes
2026-02-12 16:27:07 +01:00

283 lines
17 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
namespace Opc.Ua.SourceGeneration
{
/// <summary>
/// Template strings
/// </summary>
internal static class BinarySchemaTemplates
{
/// <summary>
/// Binary schema file template
/// </summary>
public static readonly TemplateString File = TemplateString.Parse(
$$"""
<opc:TypeDictionary
{{Tokens.XmlnsS0ListOfNamespaces}}
xmlns:opc="http://opcfoundation.org/BinarySchema/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ua="http://opcfoundation.org/UA/"
xmlns:tns="{{Tokens.DictionaryUri}}"
DefaultByteOrder="LittleEndian"
TargetNamespace="{{Tokens.DictionaryUri}}"
>
{{Tokens.Imports}}
{{Tokens.BuiltInTypes}}
{{Tokens.ListOfTypes}}
</opc:TypeDictionary>
""");
/// <summary>
/// Built-in types schema template
/// </summary>
public static readonly TemplateString BuiltInTypes =
"""
<opc:Import Namespace="http://opcfoundation.org/BinarySchema/" />
<opc:StructuredType Name="XmlElement">
<opc:Documentation>An XML element encoded as a UTF-8 string.</opc:Documentation>
<opc:Field Name="Length" TypeName="opc:Int32" />
<opc:Field Name="Value" TypeName="opc:Char" LengthField="Length" />
</opc:StructuredType>
<opc:EnumeratedType Name="NodeIdType" LengthInBits="6">
<opc:Documentation>The possible encodings for a NodeId value.</opc:Documentation>
<opc:EnumeratedValue Name="TwoByte" Value="0" />
<opc:EnumeratedValue Name="FourByte" Value="1" />
<opc:EnumeratedValue Name="Numeric" Value="2" />
<opc:EnumeratedValue Name="String" Value="3" />
<opc:EnumeratedValue Name="Guid" Value="4" />
<opc:EnumeratedValue Name="ByteString" Value="5" />
</opc:EnumeratedType>
<opc:StructuredType Name="TwoByteNodeId">
<opc:Field Name="Identifier" TypeName="opc:Byte" />
</opc:StructuredType>
<opc:StructuredType Name="FourByteNodeId">
<opc:Field Name="NamespaceIndex" TypeName="opc:Byte" />
<opc:Field Name="Identifier" TypeName="opc:UInt16" />
</opc:StructuredType>
<opc:StructuredType Name="NumericNodeId">
<opc:Field Name="NamespaceIndex" TypeName="opc:UInt16" />
<opc:Field Name="Identifier" TypeName="opc:UInt32" />
</opc:StructuredType>
<opc:StructuredType Name="StringNodeId">
<opc:Field Name="NamespaceIndex" TypeName="opc:UInt16" />
<opc:Field Name="Identifier" TypeName="opc:CharArray" />
</opc:StructuredType>
<opc:StructuredType Name="GuidNodeId">
<opc:Field Name="NamespaceIndex" TypeName="opc:UInt16" />
<opc:Field Name="Identifier" TypeName="opc:Guid" />
</opc:StructuredType>
<opc:StructuredType Name="ByteStringNodeId">
<opc:Field Name="NamespaceIndex" TypeName="opc:UInt16" />
<opc:Field Name="Identifier" TypeName="opc:ByteString" />
</opc:StructuredType>
<opc:StructuredType Name="NodeId">
<opc:Documentation>An identifier for a node in a UA server address space.</opc:Documentation>
<opc:Field Name="NodeIdType" TypeName="ua:NodeIdType" />
<opc:Field Name="Reserved1" TypeName="opc:Bit" Length="2" />
<opc:Field Name="TwoByte" TypeName="ua:TwoByteNodeId" SwitchField="NodeIdType" SwitchValue="0" />
<opc:Field Name="FourByte" TypeName="ua:FourByteNodeId" SwitchField="NodeIdType" SwitchValue="1" />
<opc:Field Name="Numeric" TypeName="ua:NumericNodeId" SwitchField="NodeIdType" SwitchValue="2" />
<opc:Field Name="String" TypeName="ua:StringNodeId" SwitchField="NodeIdType" SwitchValue="3" />
<opc:Field Name="Guid" TypeName="ua:GuidNodeId" SwitchField="NodeIdType" SwitchValue="4" />
<opc:Field Name="ByteString" TypeName="ua:ByteStringNodeId" SwitchField="NodeIdType" SwitchValue="5" />
</opc:StructuredType>
<opc:StructuredType Name="ExpandedNodeId">
<opc:Documentation>An identifier for a node in a UA server address space qualified with a complete namespace string.</opc:Documentation>
<opc:Field Name="NodeIdType" TypeName="ua:NodeIdType" />
<opc:Field Name="ServerIndexSpecified" TypeName="opc:Bit" />
<opc:Field Name="NamespaceURISpecified" TypeName="opc:Bit" />
<opc:Field Name="TwoByte" TypeName="ua:TwoByteNodeId" SwitchField="NodeIdType" SwitchValue="0" />
<opc:Field Name="FourByte" TypeName="ua:FourByteNodeId" SwitchField="NodeIdType" SwitchValue="1" />
<opc:Field Name="Numeric" TypeName="ua:NumericNodeId" SwitchField="NodeIdType" SwitchValue="2" />
<opc:Field Name="String" TypeName="ua:StringNodeId" SwitchField="NodeIdType" SwitchValue="3" />
<opc:Field Name="Guid" TypeName="ua:GuidNodeId" SwitchField="NodeIdType" SwitchValue="4" />
<opc:Field Name="ByteString" TypeName="ua:ByteStringNodeId" SwitchField="NodeIdType" SwitchValue="5" />
<opc:Field Name="NamespaceURI" TypeName="opc:CharArray" SwitchField="NamespaceURISpecified"/>
<opc:Field Name="ServerIndex" TypeName="opc:UInt32" SwitchField="ServerIndexSpecified"/>
</opc:StructuredType>
<opc:OpaqueType Name="StatusCode" LengthInBits="32" ByteOrderSignificant="true">
<opc:Documentation>A 32-bit status code value.</opc:Documentation>
</opc:OpaqueType>
<opc:StructuredType Name="DiagnosticInfo">
<opc:Documentation>A recursive structure containing diagnostic information associated with a status code.</opc:Documentation>
<opc:Field Name="SymbolicIdSpecified" TypeName="opc:Bit" />
<opc:Field Name="NamespaceURISpecified" TypeName="opc:Bit" />
<opc:Field Name="LocalizedTextSpecified" TypeName="opc:Bit" />
<opc:Field Name="LocaleSpecified" TypeName="opc:Bit" />
<opc:Field Name="AdditionalInfoSpecified" TypeName="opc:Bit" />
<opc:Field Name="InnerStatusCodeSpecified" TypeName="opc:Bit" />
<opc:Field Name="InnerDiagnosticInfoSpecified" TypeName="opc:Bit" />
<opc:Field Name="Reserved1" TypeName="opc:Bit" Length="1" />
<opc:Field Name="SymbolicId" TypeName="opc:Int32" SwitchField="SymbolicIdSpecified" />
<opc:Field Name="NamespaceURI" TypeName="opc:Int32" SwitchField="NamespaceURISpecified" />
<opc:Field Name="Locale" TypeName="opc:Int32" SwitchField="LocaleSpecified" />
<opc:Field Name="LocalizedText" TypeName="opc:Int32" SwitchField="LocalizedTextSpecified" />
<opc:Field Name="AdditionalInfo" TypeName="opc:CharArray" SwitchField="AdditionalInfoSpecified" />
<opc:Field Name="InnerStatusCode" TypeName="ua:StatusCode" SwitchField="InnerStatusCodeSpecified" />
<opc:Field Name="InnerDiagnosticInfo" TypeName="ua:DiagnosticInfo" SwitchField="InnerDiagnosticInfoSpecified" />
</opc:StructuredType>
<opc:StructuredType Name="QualifiedName">
<opc:Documentation>A string qualified with a namespace index.</opc:Documentation>
<opc:Field Name="NamespaceIndex" TypeName="opc:UInt16" />
<opc:Field Name="Name" TypeName="opc:CharArray" />
</opc:StructuredType>
<opc:StructuredType Name="LocalizedText">
<opc:Documentation>A string qualified with a namespace index.</opc:Documentation>
<opc:Field Name="LocaleSpecified" TypeName="opc:Bit" />
<opc:Field Name="TextSpecified" TypeName="opc:Bit" />
<opc:Field Name="Reserved1" TypeName="opc:Bit" Length="6" />
<opc:Field Name="Locale" TypeName="opc:CharArray" SwitchField="LocaleSpecified" />
<opc:Field Name="Text" TypeName="opc:CharArray" SwitchField="TextSpecified" />
</opc:StructuredType>
<opc:StructuredType Name="DataValue">
<opc:Documentation>A value with an associated timestamp, and quality.</opc:Documentation>
<opc:Field Name="ValueSpecified" TypeName="opc:Bit" />
<opc:Field Name="StatusCodeSpecified" TypeName="opc:Bit" />
<opc:Field Name="SourceTimestampSpecified" TypeName="opc:Bit" />
<opc:Field Name="ServerTimestampSpecified" TypeName="opc:Bit" />
<opc:Field Name="SourcePicosecondsSpecified" TypeName="opc:Bit" />
<opc:Field Name="ServerPicosecondsSpecified" TypeName="opc:Bit" />
<opc:Field Name="Reserved1" TypeName="opc:Bit" Length="2" />
<opc:Field Name="Value" TypeName="ua:Variant" SwitchField="ValueSpecified" />
<opc:Field Name="StatusCode" TypeName="ua:StatusCode" SwitchField="StatusCodeSpecified" />
<opc:Field Name="SourceTimestamp" TypeName="opc:DateTime" SwitchField="SourceTimestampSpecified" />
<opc:Field Name="SourcePicoseconds" TypeName="opc:UInt16" SwitchField="SourcePicosecondsSpecified" />
<opc:Field Name="ServerTimestamp" TypeName="opc:DateTime" SwitchField="ServerTimestampSpecified" />
<opc:Field Name="ServerPicoseconds" TypeName="opc:UInt16" SwitchField="ServerPicosecondsSpecified" />
</opc:StructuredType>
<opc:StructuredType Name="ExtensionObject">
<opc:Documentation>A serialized object prefixed with its data type identifier.</opc:Documentation>
<opc:Field Name="TypeIdSpecified" TypeName="opc:Bit" />
<opc:Field Name="BinaryBody" TypeName="opc:Bit" />
<opc:Field Name="XmlBody" TypeName="opc:Bit" />
<opc:Field Name="Reserved1" TypeName="opc:Bit" Length="5" />
<opc:Field Name="TypeId" TypeName="ua:ExpandedNodeId" SwitchField="TypeIdSpecified" />
<opc:Field Name="BodyLength" TypeName="opc:Int32" />
<opc:Field Name="Body" TypeName="opc:Byte" LengthField="BodyLength" />
</opc:StructuredType>
<opc:StructuredType Name="Variant">
<opc:Documentation>A union of several types.</opc:Documentation>
<opc:Field Name="VariantType" TypeName="opc:Bit" Length="6" />
<opc:Field Name="ArrayDimensionsSpecified" TypeName="opc:Bit" Length="1"/>
<opc:Field Name="ArrayLengthSpecified" TypeName="opc:Bit" Length="1"/>
<opc:Field Name="ArrayLength" TypeName="opc:Int32" SwitchField="ArrayLengthSpecified" />
<opc:Field Name="Boolean" TypeName="opc:Boolean" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="1" />
<opc:Field Name="SByte" TypeName="opc:SByte" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="2" />
<opc:Field Name="Byte" TypeName="opc:Byte" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="3" />
<opc:Field Name="Int16" TypeName="opc:Int16" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="4" />
<opc:Field Name="UInt16" TypeName="opc:UInt16" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="5" />
<opc:Field Name="Int32" TypeName="opc:Int32" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="6" />
<opc:Field Name="UInt32" TypeName="opc:UInt32" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="7" />
<opc:Field Name="Int64" TypeName="opc:Int64" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="8" />
<opc:Field Name="UInt64" TypeName="opc:UInt64" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="9" />
<opc:Field Name="Float" TypeName="opc:Float" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="10" />
<opc:Field Name="Double" TypeName="opc:Double" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="11" />
<opc:Field Name="String" TypeName="opc:CharArray" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="12" />
<opc:Field Name="DateTime" TypeName="opc:DateTime" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="13" />
<opc:Field Name="Guid" TypeName="opc:Guid" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="14" />
<opc:Field Name="ByteString" TypeName="opc:ByteString" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="15" />
<opc:Field Name="XmlElement" TypeName="ua:XmlElement" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="16" />
<opc:Field Name="NodeId" TypeName="ua:NodeId" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="17" />
<opc:Field Name="ExpandedNodeId" TypeName="ua:ExpandedNodeId" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="18" />
<opc:Field Name="StatusCode" TypeName="ua:StatusCode" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="19" />
<opc:Field Name="QualifiedName" TypeName="ua:QualifiedName" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="20" />
<opc:Field Name="LocalizedText" TypeName="ua:LocalizedText" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="21" />
<opc:Field Name="ExtensionObject" TypeName="ua:ExtensionObject" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="22" />
<opc:Field Name="DataValue" TypeName="ua:DataValue" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="23" />
<opc:Field Name="Variant" TypeName="ua:Variant" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="24" />
<opc:Field Name="DiagnosticInfo" TypeName="ua:DiagnosticInfo" LengthField="ArrayLength" SwitchField="VariantType" SwitchValue="25" />
<opc:Field Name="NoOfArrayDimensions" TypeName="opc:Int32" SwitchField="ArrayDimensionsSpecified" />
<opc:Field Name="ArrayDimensions" TypeName="opc:Int32" LengthField="NoOfArrayDimensions" SwitchField="ArrayDimensionsSpecified" />
</opc:StructuredType>
<opc:EnumeratedType Name="NamingRuleType" LengthInBits="32">
<opc:EnumeratedValue Name="Mandatory" Value="1" />
<opc:EnumeratedValue Name="Optional" Value="2" />
<opc:EnumeratedValue Name="Constraint" Value="3" />
</opc:EnumeratedType>
""";
/// <summary>
/// Enumerated type schema template
/// </summary>
public static readonly TemplateString EnumeratedType = TemplateString.Parse(
$$"""
<opc:EnumeratedType Name="{{Tokens.TypeName}}" LengthInBits="{{Tokens.LengthInBits}}"{{Tokens.IsOptionSet}}>
{{Tokens.Documentation}}
{{Tokens.ListOfFields}}
</opc:EnumeratedType>
""");
/// <summary>
/// Complex type schema template
/// </summary>
public static readonly TemplateString ComplexType = TemplateString.Parse(
$$"""
<opc:StructuredType Name="{{Tokens.TypeName}}" BaseType="{{Tokens.BaseType}}">
{{Tokens.Documentation}}
{{Tokens.ListOfFields}}
</opc:StructuredType>
""");
/// <summary>
/// Opaque type schema template
/// </summary>
public static readonly TemplateString OpaqueType = TemplateString.Parse(
$$"""
<opc:OpaqueType Name="{{Tokens.TypeName}}">
{{Tokens.Documentation}}
</opc:OpaqueType>
""");
}
}