opcua_types/
lib.rs

1// OPCUA for Rust
2// SPDX-License-Identifier: MPL-2.0
3// Copyright (C) 2017-2024 Adam Lock
4#![warn(missing_docs)]
5
6//! The OPC UA Types module contains data types and enumerations for OPC UA.
7//!
8//! This includes:
9//!
10//! 1. All of the built-in data types described in OPC Part 6 Chapter 5 that are encodable.
11//! 2. All of the standard data types described in OPC Part 3 Chapter 8 (if not covered by 1.).
12//! 3. Autogenerated data types and request / responses as described in OPC Part 4.
13//!
14//! For the built-in data types, the module provides functions
15
16/// Contains constants recognized by OPC UA clients and servers to describe various protocols and
17/// profiles used during communication and encryption.
18pub mod profiles {
19    /// Transport profile for OPC UA Binary
20    pub const TRANSPORT_PROFILE_URI_BINARY: &str =
21        "http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary";
22    /// Security policy for anonymous tokens.
23    pub const SECURITY_USER_TOKEN_POLICY_ANONYMOUS: &str =
24        "http://opcfoundation.org/UA-Profile/Security/UserToken/Anonymous";
25    /// Security policy for username and password tokens.
26    pub const SECURITY_USER_TOKEN_POLICY_USERPASS: &str =
27        "http://opcfoundation.org/UA-Profile/ Security/UserToken-Server/UserNamePassword";
28}
29
30/// Contains a few constants and defaults used in decoding and encoding.
31pub mod constants {
32    /// Default maximum number of elements in an array
33    pub const MAX_ARRAY_LENGTH: usize = 1000;
34    /// Default maximum size of a string in chars
35    pub const MAX_STRING_LENGTH: usize = 65535;
36    /// Default maximum size of a byte string in bytes
37    pub const MAX_BYTE_STRING_LENGTH: usize = 65535;
38    /// Default maximum size of a certificate to send
39    pub const MAX_CERTIFICATE_LENGTH: usize = 32767;
40    /// Default maximum size of a message in bytes. 0 is any length, i.e. the other end can send a message of any size which is
41    /// not recommended in a server configuration. Override in the client / server config.
42    /// In clients, max message size is only preferred size since it can be adjusted by the server during the handshake.
43    pub const MAX_MESSAGE_SIZE: usize = 65535 * MAX_CHUNK_COUNT;
44    /// Default maximum number of chunks in a single message. 0 is any number but this is not recommended
45    /// as the default since server memory could be exhausted. Default number can be overridden
46    /// by client / server config which is where it should happen if you want a different figure. In clients
47    /// chunk size is a preferred value since the server can modify it during the handshake.
48    pub const MAX_CHUNK_COUNT: usize = 5;
49    /// Default maximum decoding depth for recursive data structures, i.e. if data is nested deeper than this it is
50    /// an error during decoding. This is a security measure to stop deeply nested junk being sent to
51    /// a server / client.
52    pub const MAX_DECODING_DEPTH: u64 = 10;
53    /// URI supplied for the None security policy
54    pub const SECURITY_POLICY_NONE_URI: &str = "http://opcfoundation.org/UA/SecurityPolicy#None";
55    /// String used as shorthand in config files, debug etc.for `None` security policy
56    pub const SECURITY_POLICY_NONE: &str = "None";
57}
58
59/// Contains constants for known issued token types as defined in the OPC-UA standard.
60pub mod issued_token_types {
61    /// JSON Web Tokens (JWT).
62    pub const JSON_WEB_TOKEN: &str = "http://opcfoundation.org/UA/UserToken#JWT";
63}
64
65use bitflags::bitflags;
66
67// Attributes mask bits
68bitflags! {
69    /// Bitmask for attributes
70    pub struct AttributesMask: u32 {
71        /// Indicates if the AccessLevel Attribute is set.
72        const ACCESS_LEVEL = 1;
73        /// Indicates if the ArrayDimensions Attribute is set.
74        const ARRAY_DIMENSIONS = 1 << 1;
75        /// Indicates if the ContainsNoLoops Attribute is set.
76        const CONTAINS_NO_LOOPS = 1 << 3;
77        /// Indicates if the DataType Attribute is set.
78        const DATA_TYPE = 1 << 4;
79        /// Indicates if the Description Attribute is set.
80        const DESCRIPTION = 1 << 5;
81        /// Indicates if the DisplayName Attribute is set.
82        const DISPLAY_NAME = 1 << 6;
83        /// Indicates if the EventNotifier Attribute is set.
84        const EVENT_NOTIFIER = 1 << 7;
85        /// Indicates if the Executable Attribute is set.
86        const EXECUTABLE = 1 << 8;
87        /// Indicates if the Historizing Attribute is set.
88        const HISTORIZING = 1 << 9;
89        /// Indicates if the InverseName Attribute is set.
90        const INVERSE_NAME = 1 << 10;
91        /// Indicates if the IsAbstract Attribute is set.
92        const IS_ABSTRACT = 1 << 11;
93        /// Indicates if the MinimumSamplingInterval Attribute is set.
94        const MINIMUM_SAMPLING_INTERVAL = 1 << 12;
95        /// Indicates if the Symmetric Attribute is set.
96        const SYMMETRIC = 1 << 15;
97        /// Indicates if the UserAccessLevel Attribute is set.
98        const USER_ACCESS_LEVEL = 1 << 16;
99        /// Indicates if the UserExecutable Attribute is set.
100        const USER_EXECUTABLE = 1 << 17;
101        /// Indicates if the UserWriteMask Attribute is set.
102        const USER_WRITE_MASK = 1 << 18;
103        /// Indicates if the ValueRank Attribute is set.
104        const VALUE_RANK = 1 << 19;
105        /// Indicates if the WriteMask Attribute is set.
106        const WRITE_MASK = 1 << 20;
107        /// Indicates if the Value Attribute is set
108        const VALUE = 1 << 21;
109    }
110}
111
112// Write mask bits (similar but different to AttributesMask)
113//
114// See Part 3, Table 43
115bitflags! {
116    /// Bitmask for attributes used during write.
117    pub struct WriteMask: u32 {
118        /// Indicates if the AccessLevel Attribute is writable.
119        const ACCESS_LEVEL = 1;
120        /// Indicates if the ArrayDimensions Attribute is writable.
121        const ARRAY_DIMENSIONS = 1 << 1;
122        ///Indicates if the BrowseName Attribute is writable.
123        const BROWSE_NAME = 1 << 2;
124        /// Indicates if the ContainsNoLoops Attribute is writable.
125        const CONTAINS_NO_LOOPS = 1 << 3;
126        /// Indicates if the DataType Attribute is writable.
127        const DATA_TYPE = 1 << 4;
128        /// Indicates if the Description Attribute is writable.
129        const DESCRIPTION = 1 << 5;
130        /// Indicates if the DisplayName Attribute is writable.
131        const DISPLAY_NAME = 1 << 6;
132        /// Indicates if the EventNotifier Attribute is writable.
133        const EVENT_NOTIFIER = 1 << 7;
134        /// Indicates if the Executable Attribute is writable.
135        const EXECUTABLE = 1 << 8;
136        /// Indicates if the Historizing Attribute is writable.
137        const HISTORIZING = 1 << 9;
138        /// Indicates if the InverseName Attribute is writable.
139        const INVERSE_NAME = 1 << 10;
140        /// Indicates if the IsAbstract Attribute is writable.
141        const IS_ABSTRACT = 1 << 11;
142        /// Indicates if the MinimumSamplingInterval Attribute is writable.
143        const MINIMUM_SAMPLING_INTERVAL = 1 << 12;
144        /// Indicates if the NodeClass Attribute is writable.
145        const NODE_CLASS = 1 << 13;
146        /// Indicates if the NodeId Attribute is writable.
147        const NODE_ID = 1 << 14;
148        /// Indicates if the Symmetric Attribute is writable.
149        const SYMMETRIC = 1 << 15;
150        /// Indicates if the UserAccessLevel Attribute is writable.
151        const USER_ACCESS_LEVEL = 1 << 16;
152        /// Indicates if the UserExecutable Attribute is writable.
153        const USER_EXECUTABLE = 1 << 17;
154        /// Indicates if the UserWriteMask Attribute is writable.
155        const USER_WRITE_MASK = 1 << 18;
156        /// Indicates if the ValueRank Attribute is writable.
157        const VALUE_RANK = 1 << 19;
158        /// Indicates if the WriteMask Attribute is writable.
159        const WRITE_MASK = 1 << 20;
160        /// Indicates if the Value Attribute is writable for a VariableType. It does not apply for Variables
161        /// since this is handled by the AccessLevel and UserAccessLevel Attributes for the Variable.
162        /// For Variables this bit shall be set to 0.
163        const VALUE_FOR_VARIABLE_TYPE = 1 << 21;
164        /// Indicates if the DataTypeDefinition Attribute is writable.
165        const DATA_TYPE_DEFINITION = 1 << 22;
166        /// Indicates if the RolePermissions Attribute is writable.
167        const ROLE_PERMISSIONS = 1 << 23;
168        /// Indicates if the AccessRestrictions Attribute is writable
169        const ACCESS_RESTRICTIONS = 1 << 24;
170        /// Indicates if the AccessLevelEx Attribute is writable
171        const ACCESS_LEVEL_EX = 1 << 25;
172
173        // Bits 26-31. Reserved for future use. Shall always be zero.
174    }
175}
176
177// Bits that control the reference description coming back from browse()
178bitflags! {
179    #[derive(Debug, Copy, Clone)]
180    /// Bitmask for fields returned in reference descriptions when browsing.
181    pub struct BrowseDescriptionResultMask: u32 {
182        /// Include the reference type of the reference.
183        const RESULT_MASK_REFERENCE_TYPE = 1;
184        /// Include the boolean flag indicating whether the reference
185        /// is forward or inverse.
186        const RESULT_MASK_IS_FORWARD = 1 << 1;
187        /// Include the node class of the referenced node.
188        const RESULT_MASK_NODE_CLASS = 1 << 2;
189        /// Include the browse name of the referenced node.
190        const RESULT_MASK_BROWSE_NAME = 1 << 3;
191        /// Include the display name of the referenced node.
192        const RESULT_MASK_DISPLAY_NAME = 1 << 4;
193        /// Include the type definition of the referenced node.
194        const RESULT_MASK_TYPE_DEFINITION = 1 << 5;
195    }
196}
197
198// Bits for a node class mask
199bitflags! {
200    #[derive(Debug, Copy, Clone)]
201    /// Bitmask for node classes.
202    pub struct NodeClassMask: u32 {
203        /// Object node class.
204        const OBJECT = 1;
205        /// Variable node class.
206        const VARIABLE = 1 << 1;
207        /// Method node class.
208        const METHOD = 1 << 2;
209        /// Object type node class.
210        const OBJECT_TYPE = 1 << 3;
211        /// Variable type node class.
212        const VARIABLE_TYPE = 1 << 4;
213        /// Reference type node class.
214        const REFERENCE_TYPE = 1 << 5;
215        /// Data type node class.
216        const DATA_TYPE = 1 << 6;
217        /// View node class.
218        const VIEW = 1 << 7;
219    }
220}
221
222bitflags! {
223    #[derive(Debug, Copy, Clone)]
224    /// Bitmask for browse results.
225    pub struct BrowseResultMaskFlags: u32 {
226        /// Include the reference type ID.
227        const ReferenceTypeId = 1;
228        /// Include whether the reference is forward or inverse.
229        const IsForward = 1 << 1;
230        /// Include the node class.
231        const NodeClass = 1 << 2;
232        /// Include the browse name.
233        const BrowseName = 1 << 3;
234        /// Include the display name.
235        const DisplayName = 1 << 4;
236        /// Include the type definition
237        const TypeDefinition = 1 << 5;
238    }
239}
240
241// These 2 modules are autogenerated
242#[doc(hidden)]
243pub mod generated;
244
245mod add_node_attributes;
246pub mod argument;
247pub mod array;
248pub mod attribute;
249pub mod basic_types;
250pub mod byte_string;
251pub mod custom;
252pub mod data_change;
253pub mod data_type_definition;
254pub mod data_types;
255pub mod data_value;
256pub mod date_time;
257pub mod diagnostic_info;
258pub mod encoding;
259pub mod errors;
260pub mod event_field;
261pub mod expanded_node_id;
262pub mod extension_object;
263pub mod guid;
264mod impls;
265#[cfg(feature = "json")]
266pub mod json;
267pub mod localized_text;
268pub mod namespaces;
269pub mod node_id;
270pub mod notification_message;
271pub mod numeric_range;
272pub mod operand;
273pub mod qualified_name;
274pub mod relative_path;
275pub mod request_header;
276pub mod response_header;
277pub mod status_code;
278pub mod string;
279pub mod type_loader;
280pub mod variant;
281#[cfg(feature = "xml")]
282pub mod xml;
283
284#[cfg(feature = "json")]
285pub use opcua_macros::{JsonDecodable, JsonEncodable};
286
287#[cfg(feature = "xml")]
288pub use opcua_macros::{XmlDecodable, XmlEncodable, XmlType};
289
290pub use opcua_macros::ua_encodable;
291pub use opcua_macros::BinaryDecodable;
292pub use opcua_macros::BinaryEncodable;
293pub use opcua_macros::UaEnum;
294pub use opcua_macros::UaNullable;
295mod ua_enum;
296
297pub use self::{
298    add_node_attributes::AddNodeAttributes,
299    argument::*,
300    array::*,
301    attribute::*,
302    byte_string::*,
303    data_change::*,
304    data_type_definition::*,
305    data_types::*,
306    data_value::*,
307    date_time::*,
308    diagnostic_info::*,
309    encoding::*,
310    event_field::*,
311    expanded_node_id::*,
312    extension_object::*,
313    generated::{node_ids::*, types::*},
314    guid::*,
315    impls::*,
316    localized_text::*,
317    namespaces::*,
318    node_id::*,
319    numeric_range::*,
320    operand::*,
321    qualified_name::*,
322    request_header::*,
323    response_header::*,
324    status_code::*,
325    string::*,
326    type_loader::*,
327    ua_enum::*,
328    variant::*,
329};
330
331#[derive(Debug, Clone)]
332/// Error type used for certain custom substring methods.
333pub struct OutOfRange;
334#[cfg(test)]
335mod tests;