opcua_types/
diagnostic_info.rs

1// OPCUA for Rust
2// SPDX-License-Identifier: MPL-2.0
3// Copyright (C) 2017-2022 Adam Lock
4
5//! Contains the implementation of `DiagnosticInfo`.
6
7use std::io::{Read, Write};
8
9use crate::{encoding::*, status_codes::StatusCode, string::UAString};
10
11bitflags! {
12    pub struct DiagnosticInfoMask: u8 {
13        const HAS_SYMBOLIC_ID = 0x01;
14        const HAS_NAMESPACE = 0x02;
15        const HAS_LOCALIZED_TEXT = 0x04;
16        const HAS_LOCALE = 0x08;
17        const HAS_ADDITIONAL_INFO = 0x10;
18        const HAS_INNER_STATUS_CODE = 0x20;
19        const HAS_INNER_DIAGNOSTIC_INFO = 0x40;
20    }
21}
22
23bitflags! {
24     pub struct DiagnosticBits: u32 {
25        /// ServiceLevel / SymbolicId
26        const SERVICE_LEVEL_SYMBOLIC_ID = 0x0000_0001;
27        /// ServiceLevel / LocalizedText
28        const SERVICE_LEVEL_LOCALIZED_TEXT = 0x0000_0002;
29        /// ServiceLevel / AdditionalInfo
30        const SERVICE_LEVEL_ADDITIONAL_INFO = 0x0000_0004;
31        /// ServiceLevel / Inner StatusCode
32        const SERVICE_LEVEL_LOCALIZED_INNER_STATUS_CODE = 0x0000_0008;
33        /// ServiceLevel / Inner Diagnostics
34        const SERVICE_LEVEL_LOCALIZED_INNER_DIAGNOSTICS = 0x0000_0010;
35        /// OperationLevel / SymbolicId
36        const OPERATIONAL_LEVEL_SYMBOLIC_ID = 0x0000_0020;
37        /// OperationLevel / LocalizedText
38        const OPERATIONAL_LEVEL_LOCALIZED_TEXT = 0x0000_0040;
39        /// OperationLevel / AdditionalInfo
40        const OPERATIONAL_LEVEL_ADDITIONAL_INFO = 0x0000_0080;
41        /// OperationLevel / Inner StatusCode
42        const OPERATIONAL_LEVEL_INNER_STATUS_CODE = 0x0000_0100;
43        /// OperationLevel / Inner Diagnostics
44        const OPERATIONAL_LEVEL_INNER_DIAGNOSTICS = 0x0000_0200;
45    }
46}
47
48/// Diagnostic information.
49#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
50pub struct DiagnosticInfo {
51    /// A symbolic name for the status code.
52    pub symbolic_id: Option<i32>,
53    /// A namespace that qualifies the symbolic id.
54    pub namespace_uri: Option<i32>,
55    /// The locale used for the localized text.
56    pub locale: Option<i32>,
57    /// A human readable summary of the status code.
58    pub localized_text: Option<i32>,
59    /// Detailed application specific diagnostic information.
60    pub additional_info: Option<UAString>,
61    /// A status code provided by an underlying system.
62    pub inner_status_code: Option<StatusCode>,
63    /// Diagnostic info associated with the inner status code.
64    pub inner_diagnostic_info: Option<Box<DiagnosticInfo>>,
65}
66
67impl BinaryEncoder<DiagnosticInfo> for DiagnosticInfo {
68    fn byte_len(&self) -> usize {
69        let mut size: usize = 0;
70        size += 1; // self.encoding_mask())
71        if let Some(ref symbolic_id) = self.symbolic_id {
72            // Write symbolic id
73            size += symbolic_id.byte_len();
74        }
75        if let Some(ref namespace_uri) = self.namespace_uri {
76            // Write namespace
77            size += namespace_uri.byte_len()
78        }
79        if let Some(ref locale) = self.locale {
80            // Write locale
81            size += locale.byte_len()
82        }
83        if let Some(ref localized_text) = self.localized_text {
84            // Write localized text
85            size += localized_text.byte_len()
86        }
87        if let Some(ref additional_info) = self.additional_info {
88            // Write Additional info
89            size += additional_info.byte_len()
90        }
91        if let Some(ref inner_status_code) = self.inner_status_code {
92            // Write inner status code
93            size += inner_status_code.byte_len()
94        }
95        if let Some(ref inner_diagnostic_info) = self.inner_diagnostic_info {
96            // Write inner diagnostic info
97            size += inner_diagnostic_info.byte_len()
98        }
99        size
100    }
101
102    fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
103        let mut size: usize = 0;
104        size += write_u8(stream, self.encoding_mask().bits)?;
105        if let Some(ref symbolic_id) = self.symbolic_id {
106            // Write symbolic id
107            size += write_i32(stream, *symbolic_id)?;
108        }
109        if let Some(ref namespace_uri) = self.namespace_uri {
110            // Write namespace
111            size += namespace_uri.encode(stream)?;
112        }
113        if let Some(ref locale) = self.locale {
114            // Write locale
115            size += locale.encode(stream)?;
116        }
117        if let Some(ref localized_text) = self.localized_text {
118            // Write localized text
119            size += localized_text.encode(stream)?;
120        }
121        if let Some(ref additional_info) = self.additional_info {
122            // Write Additional info
123            size += additional_info.encode(stream)?;
124        }
125        if let Some(ref inner_status_code) = self.inner_status_code {
126            // Write inner status code
127            size += inner_status_code.encode(stream)?;
128        }
129        if let Some(ref inner_diagnostic_info) = self.inner_diagnostic_info {
130            // Write inner diagnostic info
131            size += inner_diagnostic_info.clone().encode(stream)?;
132        }
133        Ok(size)
134    }
135
136    fn decode<S: Read>(stream: &mut S, decoding_options: &DecodingOptions) -> EncodingResult<Self> {
137        let encoding_mask =
138            DiagnosticInfoMask::from_bits_truncate(u8::decode(stream, decoding_options)?);
139        let mut diagnostic_info = DiagnosticInfo::default();
140
141        if encoding_mask.contains(DiagnosticInfoMask::HAS_SYMBOLIC_ID) {
142            // Read symbolic id
143            diagnostic_info.symbolic_id = Some(i32::decode(stream, decoding_options)?);
144        }
145        if encoding_mask.contains(DiagnosticInfoMask::HAS_NAMESPACE) {
146            // Read namespace
147            diagnostic_info.namespace_uri = Some(i32::decode(stream, decoding_options)?);
148        }
149        if encoding_mask.contains(DiagnosticInfoMask::HAS_LOCALE) {
150            // Read locale
151            diagnostic_info.locale = Some(i32::decode(stream, decoding_options)?);
152        }
153        if encoding_mask.contains(DiagnosticInfoMask::HAS_LOCALIZED_TEXT) {
154            // Read localized text
155            diagnostic_info.localized_text = Some(i32::decode(stream, decoding_options)?);
156        }
157        if encoding_mask.contains(DiagnosticInfoMask::HAS_ADDITIONAL_INFO) {
158            // Read Additional info
159            diagnostic_info.additional_info = Some(UAString::decode(stream, decoding_options)?);
160        }
161        if encoding_mask.contains(DiagnosticInfoMask::HAS_INNER_STATUS_CODE) {
162            // Read inner status code
163            diagnostic_info.inner_status_code = Some(StatusCode::decode(stream, decoding_options)?);
164        }
165        if encoding_mask.contains(DiagnosticInfoMask::HAS_INNER_DIAGNOSTIC_INFO) {
166            // Read inner diagnostic info
167            diagnostic_info.inner_diagnostic_info =
168                Some(Box::new(DiagnosticInfo::decode(stream, decoding_options)?));
169        }
170        Ok(diagnostic_info)
171    }
172}
173
174impl Default for DiagnosticInfo {
175    fn default() -> Self {
176        DiagnosticInfo::null()
177    }
178}
179
180impl DiagnosticInfo {
181    pub fn null() -> DiagnosticInfo {
182        DiagnosticInfo {
183            symbolic_id: None,
184            namespace_uri: None,
185            locale: None,
186            localized_text: None,
187            additional_info: None,
188            inner_status_code: None,
189            inner_diagnostic_info: None,
190        }
191    }
192
193    pub fn encoding_mask(&self) -> DiagnosticInfoMask {
194        let mut encoding_mask = DiagnosticInfoMask::empty();
195        if self.symbolic_id.is_some() {
196            encoding_mask |= DiagnosticInfoMask::HAS_SYMBOLIC_ID;
197        }
198        if self.namespace_uri.is_some() {
199            encoding_mask |= DiagnosticInfoMask::HAS_NAMESPACE;
200        }
201        if self.locale.is_some() {
202            encoding_mask |= DiagnosticInfoMask::HAS_LOCALE;
203        }
204        if self.localized_text.is_some() {
205            encoding_mask |= DiagnosticInfoMask::HAS_LOCALIZED_TEXT;
206        }
207        if self.additional_info.is_some() {
208            encoding_mask |= DiagnosticInfoMask::HAS_ADDITIONAL_INFO;
209        }
210        if self.inner_status_code.is_some() {
211            encoding_mask |= DiagnosticInfoMask::HAS_INNER_STATUS_CODE;
212        }
213        if self.inner_diagnostic_info.is_some() {
214            encoding_mask |= DiagnosticInfoMask::HAS_INNER_DIAGNOSTIC_INFO;
215        }
216        encoding_mask
217    }
218}