bacnet-server 0.15.0

BACnet server: APDU dispatch and service handlers
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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! Wire harness and independent list decoder for the Device
//! `Active_COV_Subscriptions` projection tests.
use super::super::*;
use crate::server::test_transport::{SendLog, TestTransport};
use bacnet_encoding::apdu::decode_apdu;
use bacnet_encoding::constructed::{decode_object_property_reference, decode_recipient};
use bacnet_encoding::npdu::{Npdu, decode_npdu, encode_npdu};
use bacnet_encoding::{primitives, tags};
use bacnet_objects::analog::AnalogValueObject;
pub(super) use bacnet_objects::device::{DeviceConfig, DeviceObject};
use bacnet_services::cov::{SubscribeCOVPropertyRequest, SubscribeCOVRequest};
use bacnet_services::cov_multiple::{
    COVReference, COVSubscriptionSpecification, SubscribeCOVPropertyMultipleRequest,
};
use bacnet_services::read_property::{ReadPropertyACK, ReadPropertyRequest};
use bacnet_services::rpm::{ReadPropertyMultipleACK, ReadPropertyMultipleRequest};
use bacnet_transport::port::{ReceivedNpdu, TransportProvenance};
pub(super) use bacnet_types::constructed::{
    BACnetAddress, BACnetCOVMultipleSubscription, BACnetCOVReference, BACnetCOVSubscription,
    BACnetCOVSubscriptionSpecification, BACnetObjectPropertyReference, BACnetRecipient,
    BACnetRecipientProcess,
};
use bacnet_types::constructed::{PropertyReference, ReadAccessSpecification};
use bacnet_types::enums::EngineeringUnits;

const DEVICE_INSTANCE: u32 = 813;
pub(super) const ACTIVE: PropertyIdentifier = PropertyIdentifier::ACTIVE_COV_SUBSCRIPTIONS;
pub(super) const MULTIPLE: PropertyIdentifier =
    PropertyIdentifier::ACTIVE_COV_MULTIPLE_SUBSCRIPTIONS;
pub(super) const PV: PropertyIdentifier = PropertyIdentifier::PRESENT_VALUE;

/// A subscriber as seen by the server: link source plus optional routed source.
#[derive(Clone)]
pub(super) struct Peer {
    pub(super) mac: Vec<u8>,
    pub(super) network: Option<NpduAddress>,
}

pub(super) fn direct() -> Peer {
    Peer {
        mac: vec![0x0A, 0, 0, 5, 0xBA, 0xC0],
        network: None,
    }
}

/// Behind a router whose link MAC differs from the remote subscriber MAC.
pub(super) fn routed() -> Peer {
    Peer {
        mac: vec![0x0A, 0, 0, 1, 0xBA, 0xC0],
        network: Some(NpduAddress {
            network: 7,
            mac_address: MacAddr::from_slice(&[0x33]),
        }),
    }
}

pub(super) fn device() -> ObjectIdentifier {
    ObjectIdentifier::new(ObjectType::DEVICE, DEVICE_INSTANCE).unwrap()
}

pub(super) fn wildcard() -> ObjectIdentifier {
    ObjectIdentifier::new(ObjectType::DEVICE, ObjectIdentifier::MAX_INSTANCE).unwrap()
}

pub(super) fn av(instance: u32) -> ObjectIdentifier {
    ObjectIdentifier::new(ObjectType::ANALOG_VALUE, instance).unwrap()
}

pub(super) fn subscribe_cov(
    process: u32,
    object: ObjectIdentifier,
    confirmed: Option<bool>,
    lifetime: Option<u32>,
) -> (ConfirmedServiceChoice, BytesMut) {
    let mut request = BytesMut::new();
    SubscribeCOVRequest {
        subscriber_process_identifier: process,
        monitored_object_identifier: object,
        issue_confirmed_notifications: confirmed,
        lifetime,
    }
    .encode(&mut request)
    .unwrap();
    (ConfirmedServiceChoice::SUBSCRIBE_COV, request)
}

/// `(process, property, index, increment, confirmed, lifetime)` on `object`.
pub(super) type PropertySubscription = (
    u32,
    PropertyIdentifier,
    Option<u32>,
    Option<f32>,
    Option<bool>,
    Option<u32>,
);

pub(super) fn subscribe_cov_property(
    object: ObjectIdentifier,
    (process, property, index, increment, confirmed, lifetime): PropertySubscription,
) -> (ConfirmedServiceChoice, BytesMut) {
    let mut request = BytesMut::new();
    SubscribeCOVPropertyRequest {
        subscriber_process_identifier: process,
        monitored_object_identifier: object,
        issue_confirmed_notifications: confirmed,
        lifetime,
        monitored_property_identifier: property,
        monitored_property_array_index: index,
        cov_increment: increment,
    }
    .encode(&mut request)
    .unwrap();
    (ConfirmedServiceChoice::SUBSCRIBE_COV_PROPERTY, request)
}

/// `(property, index, increment, timestamped)` of one COV reference.
pub(super) type MultipleReference = (PropertyIdentifier, Option<u32>, Option<f32>, bool);

/// SubscribeCOVPropertyMultiple from `process` in the `confirmed` form, with
/// the request shape of the handler tests. `terms` is
/// `(lifetime, max_notification_delay)`; `None` omits both (cancellation).
pub(super) fn subscribe_cov_property_multiple(
    process: u32,
    confirmed: bool,
    terms: Option<(u32, u32)>,
    specs: Vec<(ObjectIdentifier, Vec<MultipleReference>)>,
) -> (ConfirmedServiceChoice, BytesMut) {
    let mut request = BytesMut::new();
    SubscribeCOVPropertyMultipleRequest {
        subscriber_process_identifier: process,
        issue_confirmed_notifications: confirmed,
        lifetime: terms.map(|(lifetime, _)| lifetime),
        max_notification_delay: terms.map(|(_, delay)| delay),
        list_of_cov_subscription_specifications: specs
            .into_iter()
            .map(|(object, references)| COVSubscriptionSpecification {
                monitored_object_identifier: object,
                list_of_cov_references: references
                    .into_iter()
                    .map(
                        |(property, index, cov_increment, timestamped)| COVReference {
                            monitored_property: PropertyReference {
                                property_identifier: property,
                                property_array_index: index,
                            },
                            cov_increment,
                            timestamped,
                        },
                    )
                    .collect(),
            })
            .collect(),
    }
    .encode(&mut request)
    .unwrap();
    (
        ConfirmedServiceChoice::SUBSCRIBE_COV_PROPERTY_MULTIPLE,
        request,
    )
}

/// ReadPropertyMultiple specifications: object plus `(property, index)` rows.
pub(super) type RpmSpecs = Vec<(ObjectIdentifier, Vec<(PropertyIdentifier, Option<u32>)>)>;

pub(super) fn rpm_request(specs: RpmSpecs) -> (ConfirmedServiceChoice, BytesMut) {
    let mut request = BytesMut::new();
    ReadPropertyMultipleRequest {
        list_of_read_access_specs: specs
            .into_iter()
            .map(|(object_identifier, references)| ReadAccessSpecification {
                object_identifier,
                list_of_property_references: references
                    .into_iter()
                    .map(
                        |(property_identifier, property_array_index)| PropertyReference {
                            property_identifier,
                            property_array_index,
                        },
                    )
                    .collect(),
            })
            .collect(),
    }
    .encode(&mut request)
    .unwrap();
    (ConfirmedServiceChoice::READ_PROPERTY_MULTIPLE, request)
}

/// One confirmed exchange through the MS/TP-style reply path.
pub(super) async fn exchange(
    tx: &mpsc::Sender<ReceivedNpdu>,
    peer: &Peer,
    invoke_id: u8,
    (service_choice, service_request): (ConfirmedServiceChoice, BytesMut),
) -> Apdu {
    let mut payload = BytesMut::new();
    encode_apdu(
        &mut payload,
        &Apdu::ConfirmedRequest(ConfirmedRequestPdu {
            segmented: false,
            more_follows: false,
            segmented_response_accepted: false,
            max_segments: None,
            max_apdu_length: 1476,
            invoke_id,
            sequence_number: None,
            proposed_window_size: None,
            service_choice,
            service_request: service_request.freeze(),
        }),
    )
    .unwrap();
    let mut npdu = BytesMut::new();
    encode_npdu(
        &mut npdu,
        &Npdu {
            expecting_reply: true,
            source: peer.network.clone(),
            payload: payload.freeze(),
            ..Npdu::default()
        },
    )
    .unwrap();
    let (reply_tx, reply_rx) = oneshot::channel();
    tx.send(ReceivedNpdu {
        direct_response: None,
        npdu: npdu.freeze(),
        source_mac: MacAddr::from_slice(&peer.mac),
        link_layer_group: false,
        data_attributes: Vec::new(),
        provenance: TransportProvenance::unverified(),
        reply_tx: Some(reply_tx),
    })
    .await
    .unwrap();
    let reply = tokio::time::timeout(Duration::from_secs(5), reply_rx)
        .await
        .expect("server response without lock inversion")
        .unwrap();
    let reply = decode_npdu(reply).unwrap();
    assert_eq!(reply.destination, peer.network, "reply routed to requester");
    decode_apdu(reply.payload).unwrap()
}

pub(super) struct Wire {
    pub(super) server: BACnetServer<TestTransport>,
    pub(super) tx: mpsc::Sender<ReceivedNpdu>,
    pub(super) sent: SendLog,
    invoke_id: u8,
}

impl Wire {
    pub(super) async fn start(config: ServerConfig) -> Self {
        Self::start_with_devices(config, &[DEVICE_INSTANCE]).await
    }

    pub(super) async fn start_with_devices(config: ServerConfig, instances: &[u32]) -> Self {
        let mut db = ObjectDatabase::new();
        for &instance in instances {
            db.add(Box::new(
                DeviceObject::new(DeviceConfig {
                    identity: bacnet_objects::DeviceIdentity::new(instance, 0)
                        .expect("valid Device identity")
                        .with_name(format!("Active COV Device {instance}")),
                    ..DeviceConfig::default()
                })
                .unwrap(),
            ))
            .unwrap();
        }
        let mut first =
            AnalogValueObject::new(1, "AV-1", EngineeringUnits::DEGREES_CELSIUS).unwrap();
        first.set_relinquish_default(20.0).unwrap();
        db.add(Box::new(first)).unwrap();
        db.add(Box::new(
            AnalogValueObject::new(2, "AV-2", EngineeringUnits::DEGREES_CELSIUS).unwrap(),
        ))
        .unwrap();
        Self::start_with_database(config, db).await
    }

    pub(super) async fn start_with_database(mut config: ServerConfig, db: ObjectDatabase) -> Self {
        config.device = bacnet_objects::DeviceIdentity::new(
            db.selected_device()
                .map_or(DEVICE_INSTANCE, |oid| oid.instance_number()),
            0,
        )
        .unwrap();
        let (tx, rx) = mpsc::channel(16);
        let transport = TestTransport::builder()
            .local_mac(&[0x0A, 0, 0, 2, 0xBA, 0xC0])
            .inbound(rx)
            .build();
        let sent = transport.sent();
        let server = BACnetServer::start(config, db, transport).await.unwrap();
        Self {
            server,
            tx,
            sent,
            invoke_id: 0,
        }
    }

    /// Exact duplicates are discarded by the tracker; keep every request distinct.
    pub(super) async fn send(
        &mut self,
        peer: &Peer,
        request: (ConfirmedServiceChoice, BytesMut),
    ) -> Apdu {
        self.invoke_id = self.invoke_id.wrapping_add(1);
        exchange(&self.tx, peer, self.invoke_id, request).await
    }

    pub(super) async fn read(
        &mut self,
        object: ObjectIdentifier,
        property: PropertyIdentifier,
        index: Option<u32>,
    ) -> Result<Vec<u8>, (ErrorClass, ErrorCode)> {
        let mut request = BytesMut::new();
        ReadPropertyRequest {
            object_identifier: object,
            property_identifier: property,
            property_array_index: index,
        }
        .encode(&mut request);
        let request = (ConfirmedServiceChoice::READ_PROPERTY, request);
        match self.send(&direct(), request).await {
            Apdu::ComplexAck(ack) => {
                let ack = ReadPropertyACK::decode(&ack.service_ack).unwrap();
                assert_eq!(ack.property_identifier, property);
                Ok(ack.property_value)
            }
            Apdu::Error(error) => Err((error.error_class, error.error_code)),
            other => panic!("unexpected ReadProperty response {other:?}"),
        }
    }

    /// Encoded wire ReadProperty value of the selected Device's list.
    pub(super) async fn active_bytes(&mut self) -> Vec<u8> {
        self.read(device(), ACTIVE, None).await.unwrap()
    }

    pub(super) async fn active(&mut self) -> Vec<BACnetCOVSubscription> {
        decode_subscriptions(&self.active_bytes().await)
    }

    pub(super) async fn rpm(&mut self, specs: RpmSpecs) -> ReadPropertyMultipleACK {
        rpm_ack(self.send(&direct(), rpm_request(specs)).await)
    }

    /// Encoded wire ReadProperty value of the selected Device's Multiple list.
    pub(super) async fn multiple_bytes(&mut self) -> Vec<u8> {
        self.read(device(), MULTIPLE, None).await.unwrap()
    }

    pub(super) async fn multiple(&mut self) -> Vec<BACnetCOVMultipleSubscription> {
        decode_contexts(&self.multiple_bytes().await)
    }
}

pub(super) fn simple_ack(response: Apdu) {
    assert!(matches!(response, Apdu::SimpleAck(_)), "{response:?}");
}

pub(super) fn error(response: Apdu, class: ErrorClass, code: ErrorCode) {
    assert!(
        matches!(&response, Apdu::Error(e) if e.error_class == class && e.error_code == code),
        "expected {class:?}/{code:?}, got {response:?}"
    );
}

fn rpm_ack(response: Apdu) -> ReadPropertyMultipleACK {
    let Apdu::ComplexAck(ack) = response else {
        panic!("unexpected ReadPropertyMultiple response {response:?}");
    };
    ReadPropertyMultipleACK::decode(&ack.service_ack).unwrap()
}

/// Every successful Active_COV_Subscriptions row in one RPM response.
pub(super) fn active_rows(ack: &ReadPropertyMultipleACK) -> Vec<Vec<u8>> {
    ack.list_of_read_access_results
        .iter()
        .flat_map(|result| &result.list_of_results)
        .filter(|row| row.property_identifier == ACTIVE)
        .map(|row| row.property_value.clone().expect("Active_COV row value"))
        .collect()
}

/// Independent test decoder for a `BACnetLIST of BACnetCOVSubscription`
/// (Clause 21), entries concatenated bare. Each entry is read as the recipient
/// process in an opening/closing `[0]` frame, the monitored reference in a
/// `[1]` frame, then primitive `[2]` (confirmed flag) and `[3]` (time
/// remaining), and finally `[4]` (the REAL increment) only when it is present.
pub(super) fn decode_subscriptions(data: &[u8]) -> Vec<BACnetCOVSubscription> {
    fn primitive(data: &[u8], pos: usize, number: u8) -> (&[u8], usize) {
        let (tag, start) = tags::decode_tag(data, pos).unwrap();
        assert!(tag.is_context(number), "expected [{number}] at {pos}");
        let end = start + tag.length as usize;
        (&data[start..end], end)
    }
    fn constructed(data: &[u8], pos: usize, number: u8) -> (&[u8], usize) {
        let (tag, start) = tags::decode_tag(data, pos).unwrap();
        assert!(tag.is_opening_tag(number), "expected [{number}] at {pos}");
        tags::extract_context_value(data, start, number).unwrap()
    }

    let mut subscriptions = Vec::new();
    let mut pos = 0;
    while pos < data.len() {
        let (process, next) = constructed(data, pos, 0);
        let (recipient_bytes, after_recipient) = constructed(process, 0, 0);
        let (recipient, used) = decode_recipient(recipient_bytes, 0).unwrap();
        assert_eq!(used, recipient_bytes.len());
        let (process_id, process_end) = primitive(process, after_recipient, 1);
        assert_eq!(process_end, process.len());
        let (reference, next) = constructed(data, next, 1);
        let (confirmed, next) = primitive(data, next, 2);
        assert_eq!(confirmed.len(), 1);
        let (time_remaining, mut next) = primitive(data, next, 3);
        let mut cov_increment = None;
        if next < data.len() && tags::decode_tag(data, next).unwrap().0.is_context(4) {
            let (increment, after) = primitive(data, next, 4);
            cov_increment = Some(primitives::decode_real(increment).unwrap());
            next = after;
        }
        subscriptions.push(BACnetCOVSubscription {
            recipient: BACnetRecipientProcess {
                recipient,
                process_identifier: primitives::decode_unsigned(process_id).unwrap() as u32,
            },
            monitored_property_reference: decode_object_property_reference(reference).unwrap(),
            issue_confirmed_notifications: confirmed[0] != 0,
            time_remaining: primitives::decode_unsigned(time_remaining).unwrap() as u32,
            cov_increment,
        });
        pos = next;
    }
    subscriptions
}

pub(super) fn address(peer: &Peer) -> BACnetRecipient {
    BACnetRecipient::Address(match &peer.network {
        Some(source) => BACnetAddress {
            network_number: source.network,
            mac_address: source.mac_address.clone(),
        },
        None => BACnetAddress {
            network_number: 0,
            mac_address: MacAddr::from_slice(&peer.mac),
        },
    })
}

pub(super) fn processes(subscriptions: &[BACnetCOVSubscription]) -> Vec<u32> {
    let mut ids: Vec<_> = subscriptions
        .iter()
        .map(|entry| entry.recipient.process_identifier)
        .collect();
    ids.sort_unstable();
    ids
}

pub(super) fn find(
    subscriptions: &[BACnetCOVSubscription],
    process: u32,
) -> &BACnetCOVSubscription {
    let mut matching = subscriptions
        .iter()
        .filter(|entry| entry.recipient.process_identifier == process);
    let entry = matching
        .next()
        .unwrap_or_else(|| panic!("process {process} missing: {subscriptions:?}"));
    assert!(matching.next().is_none(), "process {process} duplicated");
    entry
}

// --- Active_COV_Multiple_Subscriptions (#814) ---

/// An untimestamped reference without an explicit increment.
pub(super) fn plain(property: PropertyIdentifier) -> MultipleReference {
    (property, None, None, false)
}

pub(super) fn reference(
    property: PropertyIdentifier,
    index: Option<u32>,
    cov_increment: Option<f32>,
    timestamped: bool,
) -> BACnetCOVReference {
    BACnetCOVReference {
        property_identifier: property,
        property_array_index: index,
        cov_increment,
        timestamped,
    }
}

pub(super) fn spec(
    object: ObjectIdentifier,
    list_of_cov_references: Vec<BACnetCOVReference>,
) -> BACnetCOVSubscriptionSpecification {
    BACnetCOVSubscriptionSpecification {
        monitored_object_identifier: object,
        list_of_cov_references,
    }
}

/// A context with `time_remaining` zeroed; [`untimed`] checks lifetimes.
pub(super) fn context(
    peer: &Peer,
    process: u32,
    confirmed: bool,
    max_notification_delay: u32,
    specs: Vec<BACnetCOVSubscriptionSpecification>,
) -> BACnetCOVMultipleSubscription {
    BACnetCOVMultipleSubscription {
        recipient: BACnetRecipientProcess {
            recipient: address(peer),
            process_identifier: process,
        },
        issue_confirmed_notifications: confirmed,
        time_remaining: 0,
        max_notification_delay,
        list_of_cov_subscription_specifications: specs,
    }
}

/// Zero every lifetime after checking it is exactly the one expected. The
/// tests that call this run on tokio's paused clock, which COV lifetimes are
/// measured on (#1556), so no time passes that they don't step.
pub(super) fn untimed(
    contexts: &[BACnetCOVMultipleSubscription],
    lifetimes: &[u32],
) -> Vec<BACnetCOVMultipleSubscription> {
    assert_eq!(contexts.len(), lifetimes.len(), "{contexts:?}");
    contexts
        .iter()
        .zip(lifetimes)
        .map(|(context, lifetime)| {
            assert_eq!(context.time_remaining, *lifetime, "{context:?}");
            BACnetCOVMultipleSubscription {
                time_remaining: 0,
                ..context.clone()
            }
        })
        .collect()
}

/// Enumerated identifiers of a wire `Property_List` value.
pub(super) fn property_list(data: &[u8]) -> Vec<u32> {
    let mut identifiers = Vec::new();
    let mut pos = 0;
    while pos < data.len() {
        let (tag, start) = tags::decode_tag(data, pos).unwrap();
        let end = start + tag.length as usize;
        identifiers.push(primitives::decode_unsigned(&data[start..end]).unwrap() as u32);
        pos = end;
    }
    identifiers
}

/// Every successful row for `property` in one RPM response.
pub(super) fn rows(ack: &ReadPropertyMultipleACK, property: PropertyIdentifier) -> Vec<Vec<u8>> {
    ack.list_of_read_access_results
        .iter()
        .flat_map(|result| &result.list_of_results)
        .filter(|row| row.property_identifier == property)
        .map(|row| row.property_value.clone().expect("row value"))
        .collect()
}

/// Independent test decoder for a `BACnetLIST of BACnetCOVMultipleSubscription`
/// (Clause 21), entries concatenated bare. Each entry is read as the recipient
/// process in a `[0]` frame, primitive `[1]` (confirmed flag), `[2]` (time
/// remaining) and `[3]` (maximum notification delay), then a `[4]` frame of
/// per-object items. An item is the object identifier at `[0]` plus a `[1]`
/// frame of references; a reference is a `[0]`-framed property reference, the
/// REAL increment at `[1]` only when present, and the timestamped flag at
/// `[2]`. Every level must be consumed.
pub(super) fn decode_contexts(data: &[u8]) -> Vec<BACnetCOVMultipleSubscription> {
    fn primitive(data: &[u8], pos: usize, number: u8) -> (&[u8], usize) {
        let (tag, start) = tags::decode_tag(data, pos).unwrap();
        assert!(
            tag.is_context(number) && !tag.is_opening && !tag.is_closing,
            "expected primitive [{number}] at {pos}"
        );
        let end = start + tag.length as usize;
        (&data[start..end], end)
    }
    fn constructed(data: &[u8], pos: usize, number: u8) -> (&[u8], usize) {
        let (tag, start) = tags::decode_tag(data, pos).unwrap();
        assert!(tag.is_opening_tag(number), "expected [{number}] at {pos}");
        tags::extract_context_value(data, start, number).unwrap()
    }
    fn unsigned(data: &[u8]) -> u32 {
        primitives::decode_unsigned(data).unwrap() as u32
    }
    fn boolean(data: &[u8]) -> bool {
        assert_eq!(data.len(), 1);
        data[0] != 0
    }
    fn references(data: &[u8]) -> Vec<BACnetCOVReference> {
        let mut references = Vec::new();
        let mut pos = 0;
        while pos < data.len() {
            let (property, mut next) = constructed(data, pos, 0);
            let (identifier, after) = primitive(property, 0, 0);
            let index = (after < property.len()).then(|| {
                let (index, end) = primitive(property, after, 1);
                assert_eq!(end, property.len());
                unsigned(index)
            });
            let mut cov_increment = None;
            if tags::decode_tag(data, next).unwrap().0.is_context(1) {
                let (increment, after) = primitive(data, next, 1);
                cov_increment = Some(primitives::decode_real(increment).unwrap());
                next = after;
            }
            let (timestamped, next) = primitive(data, next, 2);
            references.push(reference(
                PropertyIdentifier::from_raw(unsigned(identifier)),
                index,
                cov_increment,
                boolean(timestamped),
            ));
            pos = next;
        }
        references
    }

    let mut contexts = Vec::new();
    let mut pos = 0;
    while pos < data.len() {
        let (process, next) = constructed(data, pos, 0);
        let (recipient_bytes, after_recipient) = constructed(process, 0, 0);
        let (recipient, used) = decode_recipient(recipient_bytes, 0).unwrap();
        assert_eq!(used, recipient_bytes.len());
        let (process_id, process_end) = primitive(process, after_recipient, 1);
        assert_eq!(process_end, process.len());
        let (confirmed, next) = primitive(data, next, 1);
        let (time_remaining, next) = primitive(data, next, 2);
        let (delay, next) = primitive(data, next, 3);
        let (specs, next) = constructed(data, next, 4);
        let mut list = Vec::new();
        let mut spec_pos = 0;
        while spec_pos < specs.len() {
            let (object, after) = primitive(specs, spec_pos, 0);
            let (refs, after) = constructed(specs, after, 1);
            list.push(spec(
                ObjectIdentifier::decode(object).unwrap(),
                references(refs),
            ));
            spec_pos = after;
        }
        contexts.push(BACnetCOVMultipleSubscription {
            recipient: BACnetRecipientProcess {
                recipient,
                process_identifier: unsigned(process_id),
            },
            issue_confirmed_notifications: boolean(confirmed),
            time_remaining: unsigned(time_remaining),
            max_notification_delay: unsigned(delay),
            list_of_cov_subscription_specifications: list,
        });
        pos = next;
    }
    contexts
}

/// A SubscribeCOVPropertyMultiple with timing the checked request encoder
/// refuses to produce (one unconfirmed Present_Value reference on AV-1).
pub(super) fn out_of_range(
    process: u32,
    lifetime: u32,
    delay: u32,
) -> (ConfirmedServiceChoice, BytesMut) {
    let mut request = BytesMut::new();
    primitives::encode_ctx_unsigned(&mut request, 0, u64::from(process));
    primitives::encode_ctx_boolean(&mut request, 1, false);
    primitives::encode_ctx_unsigned(&mut request, 2, u64::from(lifetime));
    primitives::encode_ctx_unsigned(&mut request, 3, u64::from(delay));
    tags::encode_opening_tag(&mut request, 4);
    primitives::encode_ctx_object_id(&mut request, 0, &av(1));
    tags::encode_opening_tag(&mut request, 1);
    tags::encode_opening_tag(&mut request, 0);
    primitives::encode_ctx_unsigned(&mut request, 0, u64::from(PV.to_raw()));
    tags::encode_closing_tag(&mut request, 0);
    primitives::encode_ctx_boolean(&mut request, 2, false);
    tags::encode_closing_tag(&mut request, 1);
    tags::encode_closing_tag(&mut request, 4);
    (
        ConfirmedServiceChoice::SUBSCRIBE_COV_PROPERTY_MULTIPLE,
        request,
    )
}