opcua_types/
diagnostic_info.rs1use 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 const SERVICE_LEVEL_SYMBOLIC_ID = 0x0000_0001;
27 const SERVICE_LEVEL_LOCALIZED_TEXT = 0x0000_0002;
29 const SERVICE_LEVEL_ADDITIONAL_INFO = 0x0000_0004;
31 const SERVICE_LEVEL_LOCALIZED_INNER_STATUS_CODE = 0x0000_0008;
33 const SERVICE_LEVEL_LOCALIZED_INNER_DIAGNOSTICS = 0x0000_0010;
35 const OPERATIONAL_LEVEL_SYMBOLIC_ID = 0x0000_0020;
37 const OPERATIONAL_LEVEL_LOCALIZED_TEXT = 0x0000_0040;
39 const OPERATIONAL_LEVEL_ADDITIONAL_INFO = 0x0000_0080;
41 const OPERATIONAL_LEVEL_INNER_STATUS_CODE = 0x0000_0100;
43 const OPERATIONAL_LEVEL_INNER_DIAGNOSTICS = 0x0000_0200;
45 }
46}
47
48#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
50pub struct DiagnosticInfo {
51 pub symbolic_id: Option<i32>,
53 pub namespace_uri: Option<i32>,
55 pub locale: Option<i32>,
57 pub localized_text: Option<i32>,
59 pub additional_info: Option<UAString>,
61 pub inner_status_code: Option<StatusCode>,
63 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; if let Some(ref symbolic_id) = self.symbolic_id {
72 size += symbolic_id.byte_len();
74 }
75 if let Some(ref namespace_uri) = self.namespace_uri {
76 size += namespace_uri.byte_len()
78 }
79 if let Some(ref locale) = self.locale {
80 size += locale.byte_len()
82 }
83 if let Some(ref localized_text) = self.localized_text {
84 size += localized_text.byte_len()
86 }
87 if let Some(ref additional_info) = self.additional_info {
88 size += additional_info.byte_len()
90 }
91 if let Some(ref inner_status_code) = self.inner_status_code {
92 size += inner_status_code.byte_len()
94 }
95 if let Some(ref inner_diagnostic_info) = self.inner_diagnostic_info {
96 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 size += write_i32(stream, *symbolic_id)?;
108 }
109 if let Some(ref namespace_uri) = self.namespace_uri {
110 size += namespace_uri.encode(stream)?;
112 }
113 if let Some(ref locale) = self.locale {
114 size += locale.encode(stream)?;
116 }
117 if let Some(ref localized_text) = self.localized_text {
118 size += localized_text.encode(stream)?;
120 }
121 if let Some(ref additional_info) = self.additional_info {
122 size += additional_info.encode(stream)?;
124 }
125 if let Some(ref inner_status_code) = self.inner_status_code {
126 size += inner_status_code.encode(stream)?;
128 }
129 if let Some(ref inner_diagnostic_info) = self.inner_diagnostic_info {
130 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 diagnostic_info.symbolic_id = Some(i32::decode(stream, decoding_options)?);
144 }
145 if encoding_mask.contains(DiagnosticInfoMask::HAS_NAMESPACE) {
146 diagnostic_info.namespace_uri = Some(i32::decode(stream, decoding_options)?);
148 }
149 if encoding_mask.contains(DiagnosticInfoMask::HAS_LOCALE) {
150 diagnostic_info.locale = Some(i32::decode(stream, decoding_options)?);
152 }
153 if encoding_mask.contains(DiagnosticInfoMask::HAS_LOCALIZED_TEXT) {
154 diagnostic_info.localized_text = Some(i32::decode(stream, decoding_options)?);
156 }
157 if encoding_mask.contains(DiagnosticInfoMask::HAS_ADDITIONAL_INFO) {
158 diagnostic_info.additional_info = Some(UAString::decode(stream, decoding_options)?);
160 }
161 if encoding_mask.contains(DiagnosticInfoMask::HAS_INNER_STATUS_CODE) {
162 diagnostic_info.inner_status_code = Some(StatusCode::decode(stream, decoding_options)?);
164 }
165 if encoding_mask.contains(DiagnosticInfoMask::HAS_INNER_DIAGNOSTIC_INFO) {
166 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}