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
620
621
622
623
624
625
626
//! SubscribeCOVPropertyMultiple and COVNotificationMultiple services
//! per ASHRAE 135-2020 Clauses 13.14.3 / 13.15.

use bacnet_encoding::primitives;
use bacnet_encoding::tags;
use bacnet_types::enums::PropertyIdentifier;
use bacnet_types::error::Error;
use bacnet_types::primitives::{BACnetTimeStamp, ObjectIdentifier};
use bytes::BytesMut;

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

// ---------------------------------------------------------------------------
// SubscribeCOVPropertyMultipleRequest (Clause 13.14.3)
// ---------------------------------------------------------------------------

/// A single COV reference within a subscription specification.
#[derive(Debug, Clone, PartialEq)]
pub struct COVReference {
    pub monitored_property: PropertyReference,
    pub cov_increment: Option<f32>,
    pub timestamped: bool,
}

/// A single subscription specification (object + list of property references).
#[derive(Debug, Clone, PartialEq)]
pub struct COVSubscriptionSpecification {
    pub monitored_object_identifier: ObjectIdentifier,
    pub list_of_cov_references: Vec<COVReference>,
}

/// SubscribeCOVPropertyMultiple-Request service parameters.
#[derive(Debug, Clone, PartialEq)]
pub struct SubscribeCOVPropertyMultipleRequest {
    pub subscriber_process_identifier: u32,
    pub max_notification_delay: Option<u32>,
    pub issue_confirmed_notifications: Option<bool>,
    pub list_of_cov_subscription_specifications: Vec<COVSubscriptionSpecification>,
}

impl SubscribeCOVPropertyMultipleRequest {
    pub fn encode(&self, buf: &mut BytesMut) {
        // [0] subscriberProcessIdentifier
        primitives::encode_ctx_unsigned(buf, 0, self.subscriber_process_identifier as u64);
        // [1] maxNotificationDelay OPTIONAL
        if let Some(v) = self.max_notification_delay {
            primitives::encode_ctx_unsigned(buf, 1, v as u64);
        }
        // [2] issueConfirmedNotifications OPTIONAL
        if let Some(v) = self.issue_confirmed_notifications {
            primitives::encode_ctx_boolean(buf, 2, v);
        }
        // [3] listOfCovSubscriptionSpecifications
        tags::encode_opening_tag(buf, 3);
        for spec in &self.list_of_cov_subscription_specifications {
            // [0] monitoredObjectIdentifier
            primitives::encode_ctx_object_id(buf, 0, &spec.monitored_object_identifier);
            // [1] listOfCovReferences
            tags::encode_opening_tag(buf, 1);
            for cov_ref in &spec.list_of_cov_references {
                // [0] monitoredProperty (BACnetPropertyReference)
                tags::encode_opening_tag(buf, 0);
                cov_ref.monitored_property.encode(buf);
                tags::encode_closing_tag(buf, 0);
                // [1] covIncrement OPTIONAL
                if let Some(inc) = cov_ref.cov_increment {
                    primitives::encode_ctx_real(buf, 1, inc);
                }
                // [2] timestamped DEFAULT FALSE
                if cov_ref.timestamped {
                    primitives::encode_ctx_boolean(buf, 2, true);
                }
            }
            tags::encode_closing_tag(buf, 1);
        }
        tags::encode_closing_tag(buf, 3);
    }

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

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

        // [1] maxNotificationDelay OPTIONAL
        let mut max_notification_delay = None;
        if offset < data.len() {
            let (opt, new_off) = tags::decode_optional_context(data, offset, 1)?;
            if let Some(content) = opt {
                max_notification_delay = Some(primitives::decode_unsigned(content)? as u32);
                offset = new_off;
            }
        }

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

        // [3] listOfCovSubscriptionSpecifications — opening tag 3
        let (tag, tag_end) = tags::decode_tag(data, offset)?;
        if !tag.is_opening_tag(3) {
            return Err(Error::decoding(
                offset,
                "SubscribeCOVPropertyMultiple expected opening tag 3",
            ));
        }
        offset = tag_end;

        let mut specs = Vec::new();
        loop {
            if offset >= data.len() {
                return Err(Error::decoding(
                    offset,
                    "SubscribeCOVPropertyMultiple missing closing tag 3",
                ));
            }
            if specs.len() >= MAX_DECODED_ITEMS {
                return Err(Error::decoding(offset, "too many subscription specs"));
            }
            let (tag, tag_end) = tags::decode_tag(data, offset)?;
            if tag.is_closing_tag(3) {
                offset = tag_end;
                break;
            }

            // [0] monitoredObjectIdentifier
            let end = tag_end + tag.length as usize;
            if end > data.len() {
                return Err(Error::decoding(
                    tag_end,
                    "SubscribeCOVPropertyMultiple truncated at object-id",
                ));
            }
            let oid = ObjectIdentifier::decode(&data[tag_end..end])?;
            offset = end;

            // [1] listOfCovReferences — opening tag 1
            let (tag, tag_end) = tags::decode_tag(data, offset)?;
            if !tag.is_opening_tag(1) {
                return Err(Error::decoding(
                    offset,
                    "SubscribeCOVPropertyMultiple expected opening tag 1",
                ));
            }
            offset = tag_end;

            let mut refs = Vec::new();
            loop {
                if offset >= data.len() {
                    return Err(Error::decoding(
                        offset,
                        "SubscribeCOVPropertyMultiple missing closing tag 1",
                    ));
                }
                if refs.len() >= MAX_DECODED_ITEMS {
                    return Err(Error::decoding(offset, "too many COV references"));
                }
                let (tag, tag_end) = tags::decode_tag(data, offset)?;
                if tag.is_closing_tag(1) {
                    offset = tag_end;
                    break;
                }

                // [0] monitoredProperty — opening tag 0
                if !tag.is_opening_tag(0) {
                    return Err(Error::decoding(
                        offset,
                        "SubscribeCOVPropertyMultiple expected opening tag 0 for property ref",
                    ));
                }
                let (prop_ref, new_off) = PropertyReference::decode(data, tag_end)?;
                offset = new_off;
                // closing tag 0
                let (_tag, tag_end) = tags::decode_tag(data, offset)?;
                offset = tag_end;

                // [1] covIncrement OPTIONAL
                let mut cov_increment = None;
                if offset < data.len() {
                    let (opt, new_off) = tags::decode_optional_context(data, offset, 1)?;
                    if let Some(content) = opt {
                        cov_increment = Some(primitives::decode_real(content)?);
                        offset = new_off;
                    }
                }

                // [2] timestamped DEFAULT FALSE
                let mut timestamped = false;
                if offset < data.len() {
                    let (opt, new_off) = tags::decode_optional_context(data, offset, 2)?;
                    if let Some(content) = opt {
                        timestamped = !content.is_empty() && content[0] != 0;
                        offset = new_off;
                    }
                }

                refs.push(COVReference {
                    monitored_property: prop_ref,
                    cov_increment,
                    timestamped,
                });
            }

            specs.push(COVSubscriptionSpecification {
                monitored_object_identifier: oid,
                list_of_cov_references: refs,
            });
        }
        let _ = offset;

        Ok(Self {
            subscriber_process_identifier,
            max_notification_delay,
            issue_confirmed_notifications,
            list_of_cov_subscription_specifications: specs,
        })
    }
}

// ---------------------------------------------------------------------------
// COVNotificationMultipleRequest (Confirmed service 31, Unconfirmed 11)
// Clause 13.15
// ---------------------------------------------------------------------------

/// A single value entry in a COV notification list.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct COVNotificationValue {
    pub property_identifier: PropertyIdentifier,
    pub property_array_index: Option<u32>,
    /// Raw application-tagged bytes for the value.
    pub value: Vec<u8>,
    pub time_of_change: Option<Vec<u8>>,
}

/// A single object notification within a COVNotificationMultiple.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct COVNotificationItem {
    pub monitored_object_identifier: ObjectIdentifier,
    pub list_of_values: Vec<COVNotificationValue>,
}

/// COVNotificationMultiple-Request service parameters.
#[derive(Debug, Clone, PartialEq)]
pub struct COVNotificationMultipleRequest {
    pub subscriber_process_identifier: u32,
    pub initiating_device_identifier: ObjectIdentifier,
    pub time_remaining: u32,
    pub timestamp: BACnetTimeStamp,
    pub list_of_cov_notifications: Vec<COVNotificationItem>,
}

impl COVNotificationMultipleRequest {
    pub fn encode(&self, buf: &mut BytesMut) {
        // [0] subscriberProcessIdentifier
        primitives::encode_ctx_unsigned(buf, 0, self.subscriber_process_identifier as u64);
        // [1] initiatingDeviceIdentifier
        primitives::encode_ctx_object_id(buf, 1, &self.initiating_device_identifier);
        // [2] timeRemaining
        primitives::encode_ctx_unsigned(buf, 2, self.time_remaining as u64);
        // [3] timestamp
        primitives::encode_timestamp(buf, 3, &self.timestamp);
        // [4] listOfCovNotifications
        tags::encode_opening_tag(buf, 4);
        for item in &self.list_of_cov_notifications {
            // [0] monitoredObjectIdentifier
            primitives::encode_ctx_object_id(buf, 0, &item.monitored_object_identifier);
            // [1] listOfValues
            tags::encode_opening_tag(buf, 1);
            for val in &item.list_of_values {
                // [0] propertyIdentifier
                primitives::encode_ctx_unsigned(buf, 0, val.property_identifier.to_raw() as u64);
                // [1] propertyArrayIndex OPTIONAL
                if let Some(idx) = val.property_array_index {
                    primitives::encode_ctx_unsigned(buf, 1, idx as u64);
                }
                // [2] value (opening/closing)
                tags::encode_opening_tag(buf, 2);
                buf.extend_from_slice(&val.value);
                tags::encode_closing_tag(buf, 2);
                // [3] timeOfChange OPTIONAL (opening/closing)
                if let Some(ref ts) = val.time_of_change {
                    tags::encode_opening_tag(buf, 3);
                    buf.extend_from_slice(ts);
                    tags::encode_closing_tag(buf, 3);
                }
            }
            tags::encode_closing_tag(buf, 1);
        }
        tags::encode_closing_tag(buf, 4);
    }

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

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

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

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

        // [3] timestamp
        let (timestamp, new_off) = primitives::decode_timestamp(data, offset, 3)?;
        offset = new_off;

        // [4] listOfCovNotifications — opening tag 4
        let (tag, tag_end) = tags::decode_tag(data, offset)?;
        if !tag.is_opening_tag(4) {
            return Err(Error::decoding(
                offset,
                "COVNotificationMultiple expected opening tag 4",
            ));
        }
        offset = tag_end;

        let mut items = Vec::new();
        loop {
            if offset >= data.len() {
                return Err(Error::decoding(
                    offset,
                    "COVNotificationMultiple missing closing tag 4",
                ));
            }
            if items.len() >= MAX_DECODED_ITEMS {
                return Err(Error::decoding(offset, "too many notification items"));
            }
            let (tag, tag_end) = tags::decode_tag(data, offset)?;
            if tag.is_closing_tag(4) {
                offset = tag_end;
                break;
            }

            // [0] monitoredObjectIdentifier
            let end = tag_end + tag.length as usize;
            if end > data.len() {
                return Err(Error::decoding(
                    tag_end,
                    "COVNotificationMultiple truncated at monitored-id",
                ));
            }
            let oid = ObjectIdentifier::decode(&data[tag_end..end])?;
            offset = end;

            // [1] listOfValues — opening tag 1
            let (tag, tag_end) = tags::decode_tag(data, offset)?;
            if !tag.is_opening_tag(1) {
                return Err(Error::decoding(
                    offset,
                    "COVNotificationMultiple expected opening tag 1",
                ));
            }
            offset = tag_end;

            let mut values = Vec::new();
            loop {
                if offset >= data.len() {
                    return Err(Error::decoding(
                        offset,
                        "COVNotificationMultiple missing closing tag 1",
                    ));
                }
                if values.len() >= MAX_DECODED_ITEMS {
                    return Err(Error::decoding(offset, "too many notification values"));
                }
                let (tag, tag_end) = tags::decode_tag(data, offset)?;
                if tag.is_closing_tag(1) {
                    offset = tag_end;
                    break;
                }

                // [0] propertyIdentifier
                let end = tag_end + tag.length as usize;
                if end > data.len() {
                    return Err(Error::decoding(
                        tag_end,
                        "COVNotificationMultiple truncated at property-id",
                    ));
                }
                let prop_id = primitives::decode_unsigned(&data[tag_end..end])? as u32;
                offset = end;

                // [1] propertyArrayIndex OPTIONAL
                let mut array_index = None;
                if offset < data.len() {
                    let (opt, new_off) = tags::decode_optional_context(data, offset, 1)?;
                    if let Some(content) = opt {
                        array_index = Some(primitives::decode_unsigned(content)? as u32);
                        offset = new_off;
                    }
                }

                // [2] value (opening/closing)
                let (tag, tag_end) = tags::decode_tag(data, offset)?;
                if !tag.is_opening_tag(2) {
                    return Err(Error::decoding(
                        offset,
                        "COVNotificationMultiple expected opening tag 2",
                    ));
                }
                let (value_bytes, new_off) = tags::extract_context_value(data, tag_end, 2)?;
                let value = value_bytes.to_vec();
                offset = new_off;

                // [3] timeOfChange OPTIONAL (opening/closing)
                let mut time_of_change = None;
                if offset < data.len() {
                    let (peek, _) = tags::decode_tag(data, offset)?;
                    if peek.is_opening_tag(3) {
                        let (_, inner_start) = tags::decode_tag(data, offset)?;
                        let (ts_bytes, new_off) =
                            tags::extract_context_value(data, inner_start, 3)?;
                        time_of_change = Some(ts_bytes.to_vec());
                        offset = new_off;
                    }
                }

                values.push(COVNotificationValue {
                    property_identifier: PropertyIdentifier::from_raw(prop_id),
                    property_array_index: array_index,
                    value,
                    time_of_change,
                });
            }

            items.push(COVNotificationItem {
                monitored_object_identifier: oid,
                list_of_values: values,
            });
        }
        let _ = offset;

        Ok(Self {
            subscriber_process_identifier,
            initiating_device_identifier,
            time_remaining,
            timestamp,
            list_of_cov_notifications: items,
        })
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use bacnet_types::enums::ObjectType;
    use bacnet_types::primitives::Time;

    #[test]
    fn subscribe_cov_property_multiple_round_trip() {
        let req = SubscribeCOVPropertyMultipleRequest {
            subscriber_process_identifier: 42,
            max_notification_delay: Some(10),
            issue_confirmed_notifications: Some(true),
            list_of_cov_subscription_specifications: vec![COVSubscriptionSpecification {
                monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                    .unwrap(),
                list_of_cov_references: vec![
                    COVReference {
                        monitored_property: PropertyReference {
                            property_identifier: PropertyIdentifier::PRESENT_VALUE,
                            property_array_index: None,
                        },
                        cov_increment: Some(1.0),
                        timestamped: true,
                    },
                    COVReference {
                        monitored_property: PropertyReference {
                            property_identifier: PropertyIdentifier::STATUS_FLAGS,
                            property_array_index: None,
                        },
                        cov_increment: None,
                        timestamped: false,
                    },
                ],
            }],
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = SubscribeCOVPropertyMultipleRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

    #[test]
    fn subscribe_cov_property_multiple_minimal() {
        let req = SubscribeCOVPropertyMultipleRequest {
            subscriber_process_identifier: 1,
            max_notification_delay: None,
            issue_confirmed_notifications: None,
            list_of_cov_subscription_specifications: vec![COVSubscriptionSpecification {
                monitored_object_identifier: ObjectIdentifier::new(ObjectType::BINARY_INPUT, 5)
                    .unwrap(),
                list_of_cov_references: vec![COVReference {
                    monitored_property: PropertyReference {
                        property_identifier: PropertyIdentifier::PRESENT_VALUE,
                        property_array_index: None,
                    },
                    cov_increment: None,
                    timestamped: false,
                }],
            }],
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = SubscribeCOVPropertyMultipleRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

    #[test]
    fn cov_notification_multiple_round_trip() {
        let req = COVNotificationMultipleRequest {
            subscriber_process_identifier: 1,
            initiating_device_identifier: ObjectIdentifier::new(ObjectType::DEVICE, 100).unwrap(),
            time_remaining: 60,
            timestamp: BACnetTimeStamp::SequenceNumber(42),
            list_of_cov_notifications: vec![COVNotificationItem {
                monitored_object_identifier: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1)
                    .unwrap(),
                list_of_values: vec![
                    COVNotificationValue {
                        property_identifier: PropertyIdentifier::PRESENT_VALUE,
                        property_array_index: None,
                        value: vec![0x44, 0x42, 0x90, 0x00, 0x00],
                        time_of_change: None,
                    },
                    COVNotificationValue {
                        property_identifier: PropertyIdentifier::STATUS_FLAGS,
                        property_array_index: None,
                        value: vec![0x82, 0x04, 0x00],
                        time_of_change: Some(vec![0x19, 0x2A]), // raw timestamp bytes
                    },
                ],
            }],
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = COVNotificationMultipleRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

    #[test]
    fn cov_notification_multiple_with_time_timestamp() {
        let req = COVNotificationMultipleRequest {
            subscriber_process_identifier: 5,
            initiating_device_identifier: ObjectIdentifier::new(ObjectType::DEVICE, 200).unwrap(),
            time_remaining: 0,
            timestamp: BACnetTimeStamp::Time(Time {
                hour: 12,
                minute: 30,
                second: 0,
                hundredths: 0,
            }),
            list_of_cov_notifications: vec![COVNotificationItem {
                monitored_object_identifier: ObjectIdentifier::new(ObjectType::BINARY_VALUE, 3)
                    .unwrap(),
                list_of_values: vec![COVNotificationValue {
                    property_identifier: PropertyIdentifier::PRESENT_VALUE,
                    property_array_index: None,
                    value: vec![0x91, 0x01],
                    time_of_change: None,
                }],
            }],
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = COVNotificationMultipleRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

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

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