Skip to main content

opcua_kit/
lib.rs

1#![forbid(unsafe_code)]
2#![cfg_attr(
3    not(any(feature = "discovery", feature = "commissioning", feature = "server")),
4    no_std
5)]
6//! Embed OPC UA protocol services in a Rust application.
7//!
8//! The default build implements UA TCP endpoint discovery and UADP key frames in Rust.
9//! Both can be embedded without third-party dependencies. Discovery lists the policies an endpoint advertises.
10//! Before connecting, provision the approved server certificate and application URI.
11//!
12//! ```no_run
13//! # #[cfg(feature = "discovery")]
14//! # fn run() -> Result<(), opcua_kit::DiscoveryError> {
15//! use std::time::Duration;
16//! let endpoints = opcua_kit::discover_endpoints("opc.tcp://localhost:4840", Duration::from_secs(5))?;
17//! for endpoint in endpoints { println!("{}", endpoint.security_policy_uri); }
18//! # Ok(()) }
19//! ```
20//!
21//! The optional `client` and `server` features provide Rust implementations of sessions,
22//! reads, subscriptions and serving host-owned variables. `control` adds writes and calls.
23//! `commissioning` and `cems` support offline signal checks and meter processing.
24//! Enable only `pubsub` for the standalone `no_std` codec with an allocator.
25//! With all features disabled, the crate provides a dependency-free `no_std` status classifier.
26
27#[cfg(any(feature = "client", feature = "commissioning"))]
28mod identity;
29#[cfg(any(feature = "client", feature = "commissioning"))]
30pub use identity::{NodeId, NodeIdError};
31
32#[cfg(any(feature = "client", feature = "commissioning"))]
33mod unit;
34#[cfg(any(feature = "client", feature = "commissioning"))]
35pub use unit::EngineeringUnit;
36
37#[cfg(all(feature = "client", feature = "cems"))]
38pub use client::MeterReadError;
39#[cfg(all(feature = "client", feature = "commissioning"))]
40pub use client::SnapshotReadError;
41
42#[cfg(feature = "control")]
43pub use client::{CallOutcome, ConditionComment, ConditionError, Control, ControlError};
44
45#[cfg(feature = "client")]
46pub use certificate::certificate_application_uri;
47
48#[cfg(feature = "client")]
49pub use client::{
50    ArrayValue, BrowseEntry, BrowseError, BrowseName, CertificateRollover, Client, ConnectError,
51    ConnectionConfig, ConnectionOptions, DataChange, DataChanges, EngineeringUnitEvidence,
52    EventError, EventField, Events, FixedScalar, HistoricalRead, HistoryError, MAX_MONITORED_NODES,
53    MAX_READ_NODES, MonitoredDataChanges, MonitoredNode, PublishError, ReadAttribute, ReadError,
54    ReadOperand, RecoveredSession, RecoveringClient, RecoveryError, Sample, SampleTimestamp,
55    Scalar, SecurityPolicy, Sequenced, SequencedDataChanges, SessionState, SubscriptionError,
56    TimestampError, UnitStage, UserIdentity, VariableDefinition,
57};
58
59#[cfg(feature = "discovery")]
60pub mod discovery;
61#[cfg(feature = "discovery")]
62pub use discovery::{DiscoveryError, EndpointInfo, EndpointToken, discover_endpoints};
63
64#[cfg(feature = "client")]
65pub mod client;
66
67#[cfg(any(feature = "client", feature = "server"))]
68mod secure_channel;
69#[cfg(any(feature = "client", feature = "server"))]
70pub mod ua;
71
72#[cfg(feature = "commissioning")]
73pub mod commissioning;
74
75#[cfg(any(feature = "server", feature = "client"))]
76mod certificate;
77#[cfg(feature = "server")]
78pub mod server;
79#[cfg(feature = "pubsub")]
80extern crate alloc;
81#[cfg(feature = "pubsub")]
82pub mod pubsub;
83
84/// Client interpretation of the severity field of an OPC UA StatusCode.
85#[derive(Clone, Copy, Debug, Eq, PartialEq)]
86pub enum Severity {
87    /// The operation succeeded.
88    Good,
89    /// The result may be unsuitable for some uses.
90    Uncertain,
91    /// The operation failed or the severity uses the reserved encoding.
92    Bad,
93}
94
95/// Classifies the two severity bits, independently of the subcode and flags.
96///
97/// ```
98/// use opcua_kit::{status_severity, Severity};
99///
100/// assert_eq!(status_severity(0x0000_0000), Severity::Good);
101/// assert_eq!(status_severity(0x4000_0000), Severity::Uncertain);
102/// assert_eq!(status_severity(0x8000_0000), Severity::Bad);
103/// assert_eq!(status_severity(0xFFFF_FFFF), Severity::Bad);
104/// ```
105pub const fn status_severity(status_code: u32) -> Severity {
106    match status_code >> 30 {
107        0 => Severity::Good,
108        1 => Severity::Uncertain,
109        _ => Severity::Bad,
110    }
111}