1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// OPCUA for Rust
// SPDX-License-Identifier: MPL-2.0
// Copyright (C) 2017-2024 Adam Lock
//! Contains the implementation of `DataValue`.
use std::io::{Read, Write};
use crate::{
byte_string::ByteString, date_time::*, guid::Guid, localized_text::LocalizedText,
node_id::NodeId, qualified_name::QualifiedName, status_code::StatusCode, string::UAString,
variant::Variant, BinaryDecodable, BinaryEncodable, Context, EncodingResult,
TimestampsToReturn,
};
use bitflags::bitflags;
bitflags! {
struct DataValueFlags: u8 {
/// False if the Value is Null.
const HAS_VALUE = 0x1;
/// False if the StatusCode is Good.
const HAS_STATUS = 0x2;
/// False if the Source Timestamp is DateTime.MinValue.
const HAS_SOURCE_TIMESTAMP = 0x4;
/// False if the Server Timestamp is DateTime.MinValue.
const HAS_SERVER_TIMESTAMP = 0x8;
/// False if the Source Picoseconds is 0.
const HAS_SOURCE_PICOSECONDS = 0x10;
/// False if the Server Picoseconds is 0.
const HAS_SERVER_PICOSECONDS = 0x20;
}
}
#[allow(unused)]
mod opcua {
pub(super) use crate as types;
}
/// A data value is a value of a variable in the OPC UA server and contains information about its
/// value, status and change timestamps.
#[derive(Debug, Clone, PartialEq, crate::UaNullable)]
#[cfg_attr(
feature = "json",
derive(opcua_macros::JsonEncodable, opcua_macros::JsonDecodable)
)]
pub struct DataValue {
/// The value. BaseDataType
/// Not present if the Value bit in the EncodingMask is False.
pub value: Option<Variant>,
/// The status associated with the value.
/// Not present if the StatusCode bit in the EncodingMask is False
pub status: Option<StatusCode>,
/// The source timestamp associated with the value.
/// Not present if the SourceTimestamp bit in the EncodingMask is False.
pub source_timestamp: Option<DateTime>,
/// The number of 10 picosecond intervals for the SourceTimestamp.
/// Not present if the SourcePicoSeconds bit in the EncodingMask is False.
/// If the source timestamp is missing the picoseconds are ignored.
pub source_picoseconds: Option<u16>,
/// The Server timestamp associated with the value.
/// Not present if the ServerTimestamp bit in the EncodingMask is False.
pub server_timestamp: Option<DateTime>,
/// The number of 10 picosecond intervals for the ServerTimestamp.
/// Not present if the ServerPicoSeconds bit in the EncodingMask is False.
/// If the Server timestamp is missing the picoseconds are ignored.
pub server_picoseconds: Option<u16>,
}
// For some spectacularly dumb reason Status is different in JSON and XML.
// It is named "StatusCode" in XML (5.3.1.18) and "Status" in JSON (5.4.2.18), the _only_ place
// where this is the case on a struct. So we have to implement XML manually.
#[cfg(feature = "xml")]
mod xml {
use super::DataValue;
impl crate::xml::XmlEncodable for DataValue {
fn encode(
&self,
stream: &mut crate::xml::XmlStreamWriter<&mut dyn std::io::Write>,
ctx: &crate::Context<'_>,
) -> crate::EncodingResult<()> {
use crate::xml::XmlWriteExt;
stream.encode_child("Value", &self.value, ctx)?;
stream.encode_child("StatusCode", &self.status, ctx)?;
stream.encode_child("SourceTimestamp", &self.source_timestamp, ctx)?;
stream.encode_child("SourcePicoseconds", &self.source_picoseconds, ctx)?;
stream.encode_child("ServerTimestamp", &self.server_timestamp, ctx)?;
stream.encode_child("ServerPicoseconds", &self.server_picoseconds, ctx)?;
Ok(())
}
}
impl crate::xml::XmlDecodable for DataValue {
fn decode(
stream: &mut crate::xml::XmlStreamReader<&mut dyn std::io::Read>,
ctx: &crate::Context<'_>,
) -> crate::EncodingResult<Self> {
use crate::xml::XmlReadExt;
let mut value = None;
let mut status = None;
let mut source_timestamp = None;
let mut source_picoseconds = None;
let mut server_timestamp = None;
let mut server_picoseconds = None;
stream.iter_children(
|__key, stream, ctx| {
match __key.as_str() {
"Value" => {
value = Some(crate::xml::XmlDecodable::decode(stream, ctx)?);
}
"StatusCode" => {
status = Some(crate::xml::XmlDecodable::decode(stream, ctx)?);
}
"SourceTimestamp" => {
source_timestamp = Some(crate::xml::XmlDecodable::decode(stream, ctx)?);
}
"SourcePicoseconds" => {
source_picoseconds =
Some(crate::xml::XmlDecodable::decode(stream, ctx)?);
}
"ServerTimestamp" => {
server_timestamp = Some(crate::xml::XmlDecodable::decode(stream, ctx)?);
}
"ServerPicoseconds" => {
server_picoseconds =
Some(crate::xml::XmlDecodable::decode(stream, ctx)?);
}
_ => {
stream.skip_value()?;
}
}
Ok(())
},
ctx,
)?;
Ok(Self {
value: value.unwrap_or_default(),
status: status.unwrap_or_default(),
source_timestamp: source_timestamp.unwrap_or_default(),
source_picoseconds: source_picoseconds.unwrap_or_default(),
server_timestamp: server_timestamp.unwrap_or_default(),
server_picoseconds: server_picoseconds.unwrap_or_default(),
})
}
}
impl crate::xml::XmlType for DataValue {
const TAG: &'static str = "DataValue";
}
}
impl BinaryEncodable for DataValue {
fn byte_len(&self, ctx: &opcua::types::Context<'_>) -> usize {
let mut size = 1;
let encoding_mask = self.encoding_mask();
if encoding_mask.contains(DataValueFlags::HAS_VALUE) {
size += self.value.as_ref().unwrap().byte_len(ctx);
}
if encoding_mask.contains(DataValueFlags::HAS_STATUS) {
size += self.status.as_ref().unwrap().byte_len(ctx);
}
if encoding_mask.contains(DataValueFlags::HAS_SOURCE_TIMESTAMP) {
size += self.source_timestamp.as_ref().unwrap().byte_len(ctx);
if encoding_mask.contains(DataValueFlags::HAS_SOURCE_PICOSECONDS) {
size += self.source_picoseconds.as_ref().unwrap().byte_len(ctx);
}
}
if encoding_mask.contains(DataValueFlags::HAS_SERVER_TIMESTAMP) {
size += self.server_timestamp.as_ref().unwrap().byte_len(ctx);
if encoding_mask.contains(DataValueFlags::HAS_SERVER_PICOSECONDS) {
size += self.server_picoseconds.as_ref().unwrap().byte_len(ctx);
}
}
size
}
fn encode<S: Write + ?Sized>(&self, stream: &mut S, ctx: &Context<'_>) -> EncodingResult<()> {
let encoding_mask = self.encoding_mask();
encoding_mask.bits().encode(stream, ctx)?;
if encoding_mask.contains(DataValueFlags::HAS_VALUE) {
self.value.as_ref().unwrap().encode(stream, ctx)?;
}
if encoding_mask.contains(DataValueFlags::HAS_STATUS) {
self.status.as_ref().unwrap().bits().encode(stream, ctx)?;
}
if encoding_mask.contains(DataValueFlags::HAS_SOURCE_TIMESTAMP) {
self.source_timestamp
.as_ref()
.unwrap()
.encode(stream, ctx)?;
if encoding_mask.contains(DataValueFlags::HAS_SOURCE_PICOSECONDS) {
self.source_picoseconds
.as_ref()
.unwrap()
.encode(stream, ctx)?;
}
}
if encoding_mask.contains(DataValueFlags::HAS_SERVER_TIMESTAMP) {
self.server_timestamp
.as_ref()
.unwrap()
.encode(stream, ctx)?;
if encoding_mask.contains(DataValueFlags::HAS_SERVER_PICOSECONDS) {
self.server_picoseconds
.as_ref()
.unwrap()
.encode(stream, ctx)?;
}
}
Ok(())
}
}
impl BinaryDecodable for DataValue {
fn decode<S: Read + ?Sized>(stream: &mut S, ctx: &Context<'_>) -> EncodingResult<Self> {
let encoding_mask = DataValueFlags::from_bits_truncate(u8::decode(stream, ctx)?);
// Value
let value = if encoding_mask.contains(DataValueFlags::HAS_VALUE) {
Some(Variant::decode(stream, ctx)?)
} else {
None
};
// Status
let status = if encoding_mask.contains(DataValueFlags::HAS_STATUS) {
let status = StatusCode::from(u32::decode(stream, ctx)?);
Some(status)
} else {
None
};
// Source timestamp
let source_timestamp = if encoding_mask.contains(DataValueFlags::HAS_SOURCE_TIMESTAMP) {
// The source timestamp should never be adjusted, not even when ignoring clock skew
let ctx = ctx.with_zero_offset();
Some(DateTime::decode(stream, &ctx)?)
} else {
None
};
let source_picoseconds = if encoding_mask.contains(DataValueFlags::HAS_SOURCE_PICOSECONDS) {
Some(u16::decode(stream, ctx)?)
} else {
None
};
// Server timestamp
let server_timestamp = if encoding_mask.contains(DataValueFlags::HAS_SERVER_TIMESTAMP) {
Some(DateTime::decode(stream, ctx)?)
} else {
None
};
let server_picoseconds = if encoding_mask.contains(DataValueFlags::HAS_SERVER_PICOSECONDS) {
Some(u16::decode(stream, ctx)?)
} else {
None
};
// Pico second values are discarded if associated timestamp is not supplied
Ok(DataValue {
value,
status,
source_picoseconds: if source_timestamp.is_some() {
source_picoseconds
} else {
None
},
source_timestamp,
server_picoseconds: if server_timestamp.is_some() {
server_picoseconds
} else {
None
},
server_timestamp,
})
}
}
// It would be nice if everything from here to the ... below could be condensed into a single
// trait impl somehow because it's more or less duplicating all the code in Variant.
impl From<bool> for DataValue {
fn from(v: bool) -> Self {
Self::from(Variant::from(v))
}
}
impl From<u8> for DataValue {
fn from(v: u8) -> Self {
Self::from(Variant::from(v))
}
}
impl From<i8> for DataValue {
fn from(v: i8) -> Self {
Self::from(Variant::from(v))
}
}
impl From<i16> for DataValue {
fn from(v: i16) -> Self {
Self::from(Variant::from(v))
}
}
impl From<u16> for DataValue {
fn from(v: u16) -> Self {
Self::from(Variant::from(v))
}
}
impl From<i32> for DataValue {
fn from(v: i32) -> Self {
Self::from(Variant::from(v))
}
}
impl From<u32> for DataValue {
fn from(v: u32) -> Self {
Self::from(Variant::from(v))
}
}
impl From<i64> for DataValue {
fn from(v: i64) -> Self {
Self::from(Variant::from(v))
}
}
impl From<u64> for DataValue {
fn from(v: u64) -> Self {
Self::from(Variant::from(v))
}
}
impl From<f32> for DataValue {
fn from(v: f32) -> Self {
Self::from(Variant::from(v))
}
}
impl From<f64> for DataValue {
fn from(v: f64) -> Self {
Self::from(Variant::from(v))
}
}
impl<'a> From<&'a str> for DataValue {
fn from(v: &'a str) -> Self {
Self::from(Variant::from(v))
}
}
impl From<String> for DataValue {
fn from(v: String) -> Self {
Self::from(Variant::from(v))
}
}
impl From<UAString> for DataValue {
fn from(v: UAString) -> Self {
Self::from(Variant::from(v))
}
}
impl From<DateTime> for DataValue {
fn from(v: DateTime) -> Self {
Self::from(Variant::from(v))
}
}
impl From<Guid> for DataValue {
fn from(v: Guid) -> Self {
Self::from(Variant::from(v))
}
}
impl From<StatusCode> for DataValue {
fn from(v: StatusCode) -> Self {
Self::from(Variant::from(v))
}
}
impl From<ByteString> for DataValue {
fn from(v: ByteString) -> Self {
Self::from(Variant::from(v))
}
}
impl From<QualifiedName> for DataValue {
fn from(v: QualifiedName) -> Self {
Self::from(Variant::from(v))
}
}
impl From<LocalizedText> for DataValue {
fn from(v: LocalizedText) -> Self {
Self::from(Variant::from(v))
}
}
impl From<NodeId> for DataValue {
fn from(v: NodeId) -> Self {
Self::from(Variant::from(v))
}
}
//... (see above)
impl From<Variant> for DataValue {
fn from(v: Variant) -> Self {
DataValue::value_only(v)
}
}
impl From<(Variant, StatusCode)> for DataValue {
fn from(v: (Variant, StatusCode)) -> Self {
DataValue {
value: Some(v.0),
status: Some(v.1),
source_timestamp: None,
source_picoseconds: None,
server_timestamp: None,
server_picoseconds: None,
}
}
}
/*
impl<'a> From<(Variant, &'a DateTime)> for DataValue {
fn from(v: (Variant, &'a DateTime)) -> Self {
DataValue {
value: Some(v.0),
status: Some(StatusCode::Good),
source_timestamp: Some(v.1.clone()),
source_picoseconds: Some(0),
server_timestamp: Some(v.1.clone()),
server_picoseconds: Some(0),
}
}
}
impl<'a> From<(Variant, &'a DateTime, &'a DateTime)> for DataValue {
fn from(v: (Variant, &'a DateTime, &'a DateTime)) -> Self {
// First date is source time, second is server time
DataValue {
value: Some(v.0),
status: Some(StatusCode::Good),
source_timestamp: Some(v.1.clone()),
source_picoseconds: Some(0),
server_timestamp: Some(v.2.clone()),
server_picoseconds: Some(0),
}
}
}
*/
impl Default for DataValue {
fn default() -> Self {
Self::null()
}
}
impl DataValue {
/// Creates a `DataValue` from the supplied value with nothing else.
pub fn value_only<V>(value: V) -> DataValue
where
V: Into<Variant>,
{
DataValue {
value: Some(value.into()),
status: None,
source_timestamp: None,
source_picoseconds: None,
server_timestamp: None,
server_picoseconds: None,
}
}
/// Creates a `DataValue` from the supplied value AND a timestamp for now. If you are passing a value to the Attribute::Write service
/// on a server from a server, you may consider this from the specification:
///
/// _If the SourceTimestamp or the ServerTimestamp is specified, the Server shall use these values.
/// The Server returns a Bad_WriteNotSupported error if it does not support writing of timestamps_
///
/// In which case, use the `value_only()` constructor, or make explicit which fields you pass.
pub fn new_now<V>(value: V) -> DataValue
where
V: Into<Variant>,
{
let now = DateTime::now();
DataValue {
value: Some(value.into()),
status: Some(StatusCode::Good),
source_timestamp: Some(now),
source_picoseconds: Some(0),
server_timestamp: Some(now),
server_picoseconds: Some(0),
}
}
/// Creates a `DataValue` from the supplied value and timestamp. If you are passing a value to the Attribute::Write service
/// on a server from a server, you may consider this from the specification:
///
/// _If the SourceTimestamp or the ServerTimestamp is specified, the Server shall use these values.
/// The Server returns a Bad_WriteNotSupported error if it does not support writing of timestamps_
///
/// In which case, use the `value_only()` constructor, or make explicit which fields you pass.
pub fn new_at<V>(value: V, time: DateTime) -> DataValue
where
V: Into<Variant>,
{
DataValue {
value: Some(value.into()),
status: Some(StatusCode::Good),
source_timestamp: Some(time),
source_picoseconds: Some(0),
server_timestamp: Some(time),
server_picoseconds: Some(0),
}
}
/// Creates a `DataValue` from the supplied value AND a timestamp for now. If you are passing a value to the Attribute::Write service
/// on a server from a server, you may consider this from the specification:
///
/// _If the SourceTimestamp or the ServerTimestamp is specified, the Server shall use these values.
/// The Server returns a Bad_WriteNotSupported error if it does not support writing of timestamps_
///
/// In which case, use the `value_only()` constructor, or make explicit which fields you pass.
pub fn new_now_status<V>(value: V, status: StatusCode) -> DataValue
where
V: Into<Variant>,
{
let now = DateTime::now();
DataValue {
value: Some(value.into()),
status: Some(status),
source_timestamp: Some(now),
source_picoseconds: Some(0),
server_timestamp: Some(now),
server_picoseconds: Some(0),
}
}
/// Creates a `DataValue` from the supplied value and timestamp. If you are passing a value to the Attribute::Write service
/// on a server from a server, you may consider this from the specification:
///
/// _If the SourceTimestamp or the ServerTimestamp is specified, the Server shall use these values.
/// The Server returns a Bad_WriteNotSupported error if it does not support writing of timestamps_
///
/// In which case, use the `value_only()` constructor, or make explicit which fields you pass.
pub fn new_at_status<V>(value: V, time: DateTime, status: StatusCode) -> DataValue
where
V: Into<Variant>,
{
DataValue {
value: Some(value.into()),
status: Some(status),
source_timestamp: Some(time),
source_picoseconds: Some(0),
server_timestamp: Some(time),
server_picoseconds: Some(0),
}
}
/// Creates an empty DataValue
pub fn null() -> DataValue {
DataValue {
value: None,
status: None,
source_timestamp: None,
source_picoseconds: None,
server_timestamp: None,
server_picoseconds: None,
}
}
/// Sets the value of the data value, updating the timestamps at the same point
pub fn set_value<V>(
&mut self,
value: V,
source_timestamp: &DateTime,
server_timestamp: &DateTime,
) where
V: Into<Variant>,
{
self.value = Some(value.into());
self.source_timestamp = Some(*source_timestamp);
self.source_picoseconds = Some(0);
self.server_timestamp = Some(*server_timestamp);
self.server_picoseconds = Some(0);
}
/// Sets the timestamps of the data value based on supplied timestamps to return
pub fn set_timestamps(
&mut self,
timestamps_to_return: TimestampsToReturn,
source_timestamp: DateTime,
server_timestamp: DateTime,
) {
match timestamps_to_return {
TimestampsToReturn::Source => {
self.source_timestamp = Some(source_timestamp);
self.source_picoseconds = Some(0);
self.server_timestamp = None;
self.server_picoseconds = None;
}
TimestampsToReturn::Server => {
self.source_timestamp = None;
self.source_picoseconds = None;
self.server_timestamp = Some(server_timestamp);
self.server_picoseconds = Some(0);
}
TimestampsToReturn::Both => {
self.source_timestamp = Some(source_timestamp);
self.source_picoseconds = Some(0);
self.server_timestamp = Some(server_timestamp);
self.server_picoseconds = Some(0);
}
TimestampsToReturn::Neither => {
self.source_timestamp = None;
self.source_picoseconds = None;
self.server_timestamp = None;
self.server_picoseconds = None;
}
_ => {}
}
}
/// Returns the status code or Good if there is no code on the value
pub fn status(&self) -> StatusCode {
self.status.map_or(StatusCode::Good, |s| s)
}
/// Test if the value held by this data value is known to be good
/// Anything other than Good is assumed to be invalid.
pub fn is_valid(&self) -> bool {
self.status().is_good()
}
fn encoding_mask(&self) -> DataValueFlags {
let mut encoding_mask = DataValueFlags::empty();
if self.value.is_some() {
encoding_mask |= DataValueFlags::HAS_VALUE;
}
if self.status.is_some() {
encoding_mask |= DataValueFlags::HAS_STATUS;
}
if self.source_timestamp.is_some() {
encoding_mask |= DataValueFlags::HAS_SOURCE_TIMESTAMP;
if self.source_picoseconds.is_some() {
encoding_mask |= DataValueFlags::HAS_SOURCE_PICOSECONDS;
}
}
if self.server_timestamp.is_some() {
encoding_mask |= DataValueFlags::HAS_SERVER_TIMESTAMP;
if self.server_picoseconds.is_some() {
encoding_mask |= DataValueFlags::HAS_SERVER_PICOSECONDS;
}
}
encoding_mask
}
}