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
// OPCUA for Rust
// SPDX-License-Identifier: MPL-2.0
// Copyright (C) 2017-2022 Adam Lock

//! Contains the implementation of `DataValue`.

use std::io::{Read, Write};

use crate::{
    byte_string::ByteString, date_time::*, encoding::*, guid::Guid, localized_text::LocalizedText,
    node_id::NodeId, qualified_name::QualifiedName, service_types::TimestampsToReturn,
    status_codes::StatusCode, string::UAString, variant::Variant,
};

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;
    }
}

/// 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, Serialize, Deserialize)]
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<i16>,
    /// 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<i16>,
}

impl BinaryEncoder<DataValue> for DataValue {
    fn byte_len(&self) -> 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();
        }
        if encoding_mask.contains(DataValueFlags::HAS_STATUS) {
            size += self.status.as_ref().unwrap().byte_len();
        }
        if encoding_mask.contains(DataValueFlags::HAS_SOURCE_TIMESTAMP) {
            size += self.source_timestamp.as_ref().unwrap().byte_len();
            if encoding_mask.contains(DataValueFlags::HAS_SOURCE_PICOSECONDS) {
                size += self.source_picoseconds.as_ref().unwrap().byte_len();
            }
        }
        if encoding_mask.contains(DataValueFlags::HAS_SERVER_TIMESTAMP) {
            size += self.server_timestamp.as_ref().unwrap().byte_len();
            if encoding_mask.contains(DataValueFlags::HAS_SERVER_PICOSECONDS) {
                size += self.server_picoseconds.as_ref().unwrap().byte_len();
            }
        }
        size
    }

    fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
        let mut size = 0;

        let encoding_mask = self.encoding_mask();
        size += encoding_mask.bits.encode(stream)?;

        if encoding_mask.contains(DataValueFlags::HAS_VALUE) {
            size += self.value.as_ref().unwrap().encode(stream)?;
        }
        if encoding_mask.contains(DataValueFlags::HAS_STATUS) {
            size += self.status.as_ref().unwrap().bits().encode(stream)?;
        }
        if encoding_mask.contains(DataValueFlags::HAS_SOURCE_TIMESTAMP) {
            size += self.source_timestamp.as_ref().unwrap().encode(stream)?;
            if encoding_mask.contains(DataValueFlags::HAS_SOURCE_PICOSECONDS) {
                size += self.source_picoseconds.as_ref().unwrap().encode(stream)?;
            }
        }
        if encoding_mask.contains(DataValueFlags::HAS_SERVER_TIMESTAMP) {
            size += self.server_timestamp.as_ref().unwrap().encode(stream)?;
            if encoding_mask.contains(DataValueFlags::HAS_SERVER_PICOSECONDS) {
                size += self.server_picoseconds.as_ref().unwrap().encode(stream)?;
            }
        }
        Ok(size)
    }

    fn decode<S: Read>(stream: &mut S, decoding_options: &DecodingOptions) -> EncodingResult<Self> {
        let encoding_mask =
            DataValueFlags::from_bits_truncate(u8::decode(stream, decoding_options)?);

        // Value
        let value = if encoding_mask.contains(DataValueFlags::HAS_VALUE) {
            Some(Variant::decode(stream, decoding_options)?)
        } else {
            None
        };
        // Status
        let status = if encoding_mask.contains(DataValueFlags::HAS_STATUS) {
            let status = StatusCode::from_bits_truncate(u32::decode(stream, decoding_options)?);
            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 decoding_options = DecodingOptions {
                client_offset: chrono::Duration::zero(),
                ..*decoding_options
            };
            Some(DateTime::decode(stream, &decoding_options)?)
        } else {
            None
        };
        let source_picoseconds = if encoding_mask.contains(DataValueFlags::HAS_SOURCE_PICOSECONDS) {
            Some(i16::decode(stream, decoding_options)?)
        } else {
            None
        };
        // Server timestamp
        let server_timestamp = if encoding_mask.contains(DataValueFlags::HAS_SERVER_TIMESTAMP) {
            Some(DateTime::decode(stream, decoding_options)?)
        } else {
            None
        };
        let server_picoseconds = if encoding_mask.contains(DataValueFlags::HAS_SERVER_PICOSECONDS) {
            Some(i16::decode(stream, decoding_options)?)
        } 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 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().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
    }
}