bacnet-services 0.5.4

BACnet service request/response encode/decode per ASHRAE 135-2020
Documentation
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
//! COV (Change of Value) services per ASHRAE 135-2020 Clause 13 & 16.

use bacnet_encoding::primitives;
use bacnet_encoding::tags;
use bacnet_types::enums::PropertyIdentifier;
use bacnet_types::error::Error;
use bacnet_types::primitives::ObjectIdentifier;
use bytes::BytesMut;

use crate::common::{BACnetPropertyValue, MAX_DECODED_ITEMS};

// ---------------------------------------------------------------------------
// SubscribeCOVRequest (Clause 13.14.1)
// ---------------------------------------------------------------------------

/// SubscribeCOV-Request service parameters.
///
/// Both `issue_confirmed_notifications` and `lifetime` absent = cancellation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubscribeCOVRequest {
    pub subscriber_process_identifier: u32,
    pub monitored_object_identifier: ObjectIdentifier,
    pub issue_confirmed_notifications: Option<bool>,
    pub lifetime: Option<u32>,
}

impl SubscribeCOVRequest {
    /// Whether this is a cancellation (both optional fields absent).
    pub fn is_cancellation(&self) -> bool {
        self.issue_confirmed_notifications.is_none() && self.lifetime.is_none()
    }

    pub fn encode(&self, buf: &mut BytesMut) {
        // [0] subscriber-process-identifier
        primitives::encode_ctx_unsigned(buf, 0, self.subscriber_process_identifier as u64);
        // [1] monitored-object-identifier
        primitives::encode_ctx_object_id(buf, 1, &self.monitored_object_identifier);
        // [2] issue-confirmed-notifications (optional)
        if let Some(confirmed) = self.issue_confirmed_notifications {
            primitives::encode_ctx_boolean(buf, 2, confirmed);
        }
        // [3] lifetime (optional)
        if let Some(lifetime) = self.lifetime {
            primitives::encode_ctx_unsigned(buf, 3, lifetime as u64);
        }
    }

    pub fn decode(data: &[u8]) -> Result<Self, Error> {
        let mut offset = 0;

        // [0] subscriber-process-identifier
        let (tag, pos) = tags::decode_tag(data, offset)?;
        let end = pos + tag.length as usize;
        if end > data.len() {
            return Err(Error::decoding(pos, "SubscribeCOV truncated at process-id"));
        }
        let subscriber_process_identifier = primitives::decode_unsigned(&data[pos..end])? as u32;
        offset = end;

        // [1] monitored-object-identifier
        let (tag, pos) = tags::decode_tag(data, offset)?;
        let end = pos + tag.length as usize;
        if end > data.len() {
            return Err(Error::decoding(pos, "SubscribeCOV truncated at object-id"));
        }
        let monitored_object_identifier = ObjectIdentifier::decode(&data[pos..end])?;
        offset = end;

        // [2] issue-confirmed-notifications (optional)
        let mut issue_confirmed_notifications = None;
        if offset < data.len() {
            let (opt_data, new_offset) = tags::decode_optional_context(data, offset, 2)?;
            if let Some(content) = opt_data {
                if content.is_empty() {
                    return Err(Error::decoding(
                        offset,
                        "SubscribeCOV: empty confirmed-notifications field",
                    ));
                }
                issue_confirmed_notifications = Some(content[0] != 0);
                offset = new_offset;
            }
        }

        // [3] lifetime (optional)
        let mut lifetime = None;
        if offset < data.len() {
            let (opt_data, _new_offset) = tags::decode_optional_context(data, offset, 3)?;
            if let Some(content) = opt_data {
                lifetime = Some(primitives::decode_unsigned(content)? as u32);
            }
        }

        Ok(Self {
            subscriber_process_identifier,
            monitored_object_identifier,
            issue_confirmed_notifications,
            lifetime,
        })
    }
}

// ---------------------------------------------------------------------------
// SubscribeCOVPropertyRequest (Clause 13.14.2)
// ---------------------------------------------------------------------------

/// SubscribeCOVProperty-Request service parameters.
///
/// Like SubscribeCOV but targets a specific property and optionally
/// overrides the COV increment.
#[derive(Debug, Clone, PartialEq)]
pub struct SubscribeCOVPropertyRequest {
    pub subscriber_process_identifier: u32,
    pub monitored_object_identifier: ObjectIdentifier,
    pub issue_confirmed_notifications: Option<bool>,
    pub lifetime: Option<u32>,
    pub monitored_property_identifier: PropertyIdentifier,
    pub monitored_property_array_index: Option<u32>,
    pub cov_increment: Option<f32>,
}

impl SubscribeCOVPropertyRequest {
    /// Whether this is a cancellation (both optional notification/lifetime absent).
    pub fn is_cancellation(&self) -> bool {
        self.issue_confirmed_notifications.is_none() && self.lifetime.is_none()
    }

    pub fn encode(&self, buf: &mut BytesMut) {
        // [0] subscriberProcessIdentifier
        primitives::encode_ctx_unsigned(buf, 0, self.subscriber_process_identifier as u64);
        // [1] monitoredObjectIdentifier
        primitives::encode_ctx_object_id(buf, 1, &self.monitored_object_identifier);
        // [2] issueConfirmedNotifications (optional)
        if let Some(v) = self.issue_confirmed_notifications {
            primitives::encode_ctx_boolean(buf, 2, v);
        }
        // [3] lifetime (optional)
        if let Some(v) = self.lifetime {
            primitives::encode_ctx_unsigned(buf, 3, v as u64);
        }
        // [4] monitoredPropertyIdentifier (BACnetPropertyReference)
        tags::encode_opening_tag(buf, 4);
        primitives::encode_ctx_unsigned(buf, 0, self.monitored_property_identifier.to_raw() as u64);
        if let Some(idx) = self.monitored_property_array_index {
            primitives::encode_ctx_unsigned(buf, 1, idx as u64);
        }
        tags::encode_closing_tag(buf, 4);
        // [5] covIncrement (optional)
        if let Some(v) = self.cov_increment {
            primitives::encode_ctx_real(buf, 5, v);
        }
    }

    pub fn decode(data: &[u8]) -> Result<Self, Error> {
        let mut offset = 0;

        // [0] subscriberProcessIdentifier
        let (tag, pos) = tags::decode_tag(data, offset)?;
        if tag.number != 0 {
            return Err(Error::decoding(pos, "expected context 0 for process-id"));
        }
        let end = pos + tag.length as usize;
        if end > data.len() {
            return Err(Error::buffer_too_short(end, data.len()));
        }
        let subscriber_process_identifier = primitives::decode_unsigned(&data[pos..end])? as u32;
        offset = end;

        // [1] monitoredObjectIdentifier
        let (tag, pos) = tags::decode_tag(data, offset)?;
        if tag.number != 1 {
            return Err(Error::decoding(pos, "expected context 1 for object-id"));
        }
        let end = pos + tag.length as usize;
        if end > data.len() {
            return Err(Error::buffer_too_short(end, data.len()));
        }
        let monitored_object_identifier = ObjectIdentifier::decode(&data[pos..end])?;
        offset = end;

        // [2] issueConfirmedNotifications (optional)
        let mut issue_confirmed_notifications = None;
        if offset < data.len() {
            let (opt_data, new_offset) = tags::decode_optional_context(data, offset, 2)?;
            if let Some(content) = opt_data {
                issue_confirmed_notifications = Some(!content.is_empty() && content[0] != 0);
                offset = new_offset;
            }
        }

        // [3] lifetime (optional)
        let mut lifetime = None;
        if offset < data.len() {
            let (opt_data, new_offset) = tags::decode_optional_context(data, offset, 3)?;
            if let Some(content) = opt_data {
                lifetime = Some(primitives::decode_unsigned(content)? as u32);
                offset = new_offset;
            }
        }

        // [4] monitoredPropertyIdentifier (BACnetPropertyReference)
        let (tag, pos) = tags::decode_tag(data, offset)?;
        if tag.number != 4 {
            return Err(Error::decoding(
                pos,
                "expected context 4 for monitored-property",
            ));
        }
        // Opening tag — decode inner property reference
        offset = pos;
        let (tag, pos) = tags::decode_tag(data, offset)?;
        let end = pos + tag.length as usize;
        if end > data.len() {
            return Err(Error::buffer_too_short(end, data.len()));
        }
        let monitored_property_identifier =
            PropertyIdentifier::from_raw(primitives::decode_unsigned(&data[pos..end])? as u32);
        offset = end;

        // Optional array index [1] inside property reference
        let mut monitored_property_array_index = None;
        if offset < data.len() {
            let (opt_data, new_offset) = tags::decode_optional_context(data, offset, 1)?;
            if let Some(content) = opt_data {
                monitored_property_array_index = Some(primitives::decode_unsigned(content)? as u32);
                offset = new_offset;
            }
        }

        // Closing tag [4]
        let (_tag, pos) = tags::decode_tag(data, offset)?;
        offset = pos;

        // [5] covIncrement (optional)
        let mut cov_increment = None;
        if offset < data.len() {
            let (opt_data, new_offset) = tags::decode_optional_context(data, offset, 5)?;
            if let Some(content) = opt_data {
                cov_increment = Some(primitives::decode_real(content)?);
                offset = new_offset;
            }
        }
        let _ = offset; // suppress unused

        Ok(Self {
            subscriber_process_identifier,
            monitored_object_identifier,
            issue_confirmed_notifications,
            lifetime,
            monitored_property_identifier,
            monitored_property_array_index,
            cov_increment,
        })
    }
}

// ---------------------------------------------------------------------------
// COVNotificationRequest (Clause 13.14.7 / 16.10.3)
// ---------------------------------------------------------------------------

/// COVNotification-Request service parameters.
///
/// Used for both ConfirmedCOVNotification and UnconfirmedCOVNotification.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct COVNotificationRequest {
    pub subscriber_process_identifier: u32,
    pub initiating_device_identifier: ObjectIdentifier,
    pub monitored_object_identifier: ObjectIdentifier,
    pub time_remaining: u32,
    pub list_of_values: Vec<BACnetPropertyValue>,
}

impl COVNotificationRequest {
    pub fn encode(&self, buf: &mut BytesMut) {
        // [0] subscriber-process-identifier
        primitives::encode_ctx_unsigned(buf, 0, self.subscriber_process_identifier as u64);
        // [1] initiating-device-identifier
        primitives::encode_ctx_object_id(buf, 1, &self.initiating_device_identifier);
        // [2] monitored-object-identifier
        primitives::encode_ctx_object_id(buf, 2, &self.monitored_object_identifier);
        // [3] time-remaining
        primitives::encode_ctx_unsigned(buf, 3, self.time_remaining as u64);
        // [4] list-of-values (opening/closing)
        tags::encode_opening_tag(buf, 4);
        for pv in &self.list_of_values {
            pv.encode(buf);
        }
        tags::encode_closing_tag(buf, 4);
    }

    pub fn decode(data: &[u8]) -> Result<Self, Error> {
        let mut offset = 0;

        // [0] subscriber-process-identifier
        let (tag, pos) = tags::decode_tag(data, offset)?;
        let end = pos + tag.length as usize;
        if end > data.len() {
            return Err(Error::decoding(
                pos,
                "COVNotification truncated at process-id",
            ));
        }
        let subscriber_process_identifier = primitives::decode_unsigned(&data[pos..end])? as u32;
        offset = end;

        // [1] initiating-device-identifier
        let (tag, pos) = tags::decode_tag(data, offset)?;
        let end = pos + tag.length as usize;
        if end > data.len() {
            return Err(Error::decoding(
                pos,
                "COVNotification truncated at device-id",
            ));
        }
        let initiating_device_identifier = ObjectIdentifier::decode(&data[pos..end])?;
        offset = end;

        // [2] monitored-object-identifier
        let (tag, pos) = tags::decode_tag(data, offset)?;
        let end = pos + tag.length as usize;
        if end > data.len() {
            return Err(Error::decoding(
                pos,
                "COVNotification truncated at monitored-id",
            ));
        }
        let monitored_object_identifier = ObjectIdentifier::decode(&data[pos..end])?;
        offset = end;

        // [3] time-remaining
        let (tag, pos) = tags::decode_tag(data, offset)?;
        let end = pos + tag.length as usize;
        if end > data.len() {
            return Err(Error::decoding(
                pos,
                "COVNotification truncated at time-remaining",
            ));
        }
        let time_remaining = primitives::decode_unsigned(&data[pos..end])? as u32;
        offset = end;

        // [4] list-of-values (opening tag 4)
        let (tag, tag_end) = tags::decode_tag(data, offset)?;
        if !tag.is_opening_tag(4) {
            return Err(Error::decoding(
                offset,
                "COVNotification expected opening tag 4",
            ));
        }
        offset = tag_end;

        let mut values = Vec::new();
        loop {
            if offset >= data.len() {
                return Err(Error::decoding(
                    offset,
                    "COVNotification missing closing tag 4",
                ));
            }
            if values.len() >= MAX_DECODED_ITEMS {
                return Err(Error::decoding(
                    offset,
                    "COVNotification values exceeds max",
                ));
            }
            let (tag, tag_end) = tags::decode_tag(data, offset)?;
            if tag.is_closing_tag(4) {
                // Consume the closing tag for correctness; offset is not used
                // after the loop today but would be needed if trailing fields
                // are added in the future.
                offset = tag_end;
                break;
            }
            let (pv, new_offset) = BACnetPropertyValue::decode(data, offset)?;
            values.push(pv);
            offset = new_offset;
        }
        let _ = offset; // consumed after closing tag for future-proofing

        Ok(Self {
            subscriber_process_identifier,
            initiating_device_identifier,
            monitored_object_identifier,
            time_remaining,
            list_of_values: values,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bacnet_types::enums::{ObjectType, PropertyIdentifier};

    #[test]
    fn subscribe_cov_round_trip() {
        let req = SubscribeCOVRequest {
            subscriber_process_identifier: 1,
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                .unwrap(),
            issue_confirmed_notifications: Some(true),
            lifetime: Some(300),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = SubscribeCOVRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
        assert!(!decoded.is_cancellation());
    }

    #[test]
    fn subscribe_cov_cancellation() {
        let req = SubscribeCOVRequest {
            subscriber_process_identifier: 1,
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                .unwrap(),
            issue_confirmed_notifications: None,
            lifetime: None,
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = SubscribeCOVRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
        assert!(decoded.is_cancellation());
    }

    #[test]
    fn cov_notification_round_trip() {
        let req = COVNotificationRequest {
            subscriber_process_identifier: 1,
            initiating_device_identifier: ObjectIdentifier::new(ObjectType::DEVICE, 1234).unwrap(),
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                .unwrap(),
            time_remaining: 60,
            list_of_values: vec![
                BACnetPropertyValue {
                    property_identifier: PropertyIdentifier::PRESENT_VALUE,
                    property_array_index: None,
                    value: vec![0x44, 0x42, 0x90, 0x00, 0x00],
                    priority: None,
                },
                BACnetPropertyValue {
                    property_identifier: PropertyIdentifier::STATUS_FLAGS,
                    property_array_index: None,
                    value: vec![0x82, 0x04, 0x00], // bit-string: 4 unused, 0x00
                    priority: None,
                },
            ],
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = COVNotificationRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

    // -----------------------------------------------------------------------
    // Malformed-input decode error tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_decode_subscribe_cov_empty_input() {
        assert!(SubscribeCOVRequest::decode(&[]).is_err());
    }

    #[test]
    fn test_decode_subscribe_cov_truncated_1_byte() {
        let req = SubscribeCOVRequest {
            subscriber_process_identifier: 1,
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                .unwrap(),
            issue_confirmed_notifications: Some(true),
            lifetime: Some(300),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        assert!(SubscribeCOVRequest::decode(&buf[..1]).is_err());
    }

    #[test]
    fn test_decode_subscribe_cov_truncated_2_bytes() {
        let req = SubscribeCOVRequest {
            subscriber_process_identifier: 1,
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                .unwrap(),
            issue_confirmed_notifications: Some(true),
            lifetime: Some(300),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        assert!(SubscribeCOVRequest::decode(&buf[..2]).is_err());
    }

    #[test]
    fn test_decode_subscribe_cov_truncated_3_bytes() {
        let req = SubscribeCOVRequest {
            subscriber_process_identifier: 1,
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                .unwrap(),
            issue_confirmed_notifications: Some(true),
            lifetime: Some(300),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        assert!(SubscribeCOVRequest::decode(&buf[..3]).is_err());
    }

    #[test]
    fn test_decode_subscribe_cov_invalid_tag() {
        assert!(SubscribeCOVRequest::decode(&[0xFF, 0xFF, 0xFF]).is_err());
    }

    #[test]
    fn test_decode_cov_notification_empty_input() {
        assert!(COVNotificationRequest::decode(&[]).is_err());
    }

    #[test]
    fn test_decode_cov_notification_truncated_1_byte() {
        let req = COVNotificationRequest {
            subscriber_process_identifier: 1,
            initiating_device_identifier: ObjectIdentifier::new(ObjectType::DEVICE, 1234).unwrap(),
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                .unwrap(),
            time_remaining: 60,
            list_of_values: vec![BACnetPropertyValue {
                property_identifier: PropertyIdentifier::PRESENT_VALUE,
                property_array_index: None,
                value: vec![0x44, 0x42, 0x90, 0x00, 0x00],
                priority: None,
            }],
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        assert!(COVNotificationRequest::decode(&buf[..1]).is_err());
    }

    #[test]
    fn test_decode_cov_notification_truncated_3_bytes() {
        let req = COVNotificationRequest {
            subscriber_process_identifier: 1,
            initiating_device_identifier: ObjectIdentifier::new(ObjectType::DEVICE, 1234).unwrap(),
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                .unwrap(),
            time_remaining: 60,
            list_of_values: vec![BACnetPropertyValue {
                property_identifier: PropertyIdentifier::PRESENT_VALUE,
                property_array_index: None,
                value: vec![0x44, 0x42, 0x90, 0x00, 0x00],
                priority: None,
            }],
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        assert!(COVNotificationRequest::decode(&buf[..3]).is_err());
    }

    #[test]
    fn test_decode_cov_notification_truncated_half() {
        let req = COVNotificationRequest {
            subscriber_process_identifier: 1,
            initiating_device_identifier: ObjectIdentifier::new(ObjectType::DEVICE, 1234).unwrap(),
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                .unwrap(),
            time_remaining: 60,
            list_of_values: vec![BACnetPropertyValue {
                property_identifier: PropertyIdentifier::PRESENT_VALUE,
                property_array_index: None,
                value: vec![0x44, 0x42, 0x90, 0x00, 0x00],
                priority: None,
            }],
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let half = buf.len() / 2;
        assert!(COVNotificationRequest::decode(&buf[..half]).is_err());
    }

    #[test]
    fn test_decode_cov_notification_invalid_tag() {
        assert!(COVNotificationRequest::decode(&[0xFF, 0xFF, 0xFF]).is_err());
    }

    #[test]
    fn subscribe_cov_property_round_trip() {
        let req = SubscribeCOVPropertyRequest {
            subscriber_process_identifier: 7,
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 3)
                .unwrap(),
            issue_confirmed_notifications: Some(true),
            lifetime: Some(600),
            monitored_property_identifier: PropertyIdentifier::PRESENT_VALUE,
            monitored_property_array_index: None,
            cov_increment: Some(1.5),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = SubscribeCOVPropertyRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

    #[test]
    fn subscribe_cov_property_round_trip_with_array_index() {
        let req = SubscribeCOVPropertyRequest {
            subscriber_process_identifier: 2,
            monitored_object_identifier: ObjectIdentifier::new(ObjectType::BINARY_VALUE, 10)
                .unwrap(),
            issue_confirmed_notifications: None,
            lifetime: None,
            monitored_property_identifier: PropertyIdentifier::PRESENT_VALUE,
            monitored_property_array_index: Some(3),
            cov_increment: None,
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = SubscribeCOVPropertyRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
        assert!(decoded.is_cancellation());
    }
}