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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! Truth-source cross-check tests for the PICS writability fix (issue #115).
//!
//! These tests live in a sibling module to `tests.rs` to keep `tests.rs` under
//! the workspace's 700-LOC file cap. They verify the central invariant of the
//! shared-truth-source design: for every core I/O/V object type, the
//! `BACnetObject::is_writable_property` override and the runtime
//! `write_property_from` match arms must agree — if the override reports a property
//! writable, `write_property_from` must accept it, and if it reports it read-only,
//! `write_property_from` must reject it. This is the regression guard that prevents
//! an override and its `write_property_from` arms from drifting apart (the exact
//! false-negative class issue #115 eliminates).

use bacnet_objects::analog::{AnalogInputObject, AnalogOutputObject, AnalogValueObject};
use bacnet_objects::binary::{BinaryInputObject, BinaryOutputObject, BinaryValueObject};
use bacnet_objects::multistate::{
    MultiStateInputObject, MultiStateOutputObject, MultiStateValueObject,
};
use bacnet_objects::traits::BACnetObject;
use bacnet_types::enums::{ObjectType, PropertyIdentifier};
use bacnet_types::primitives::PropertyValue;

use super::{PicsConfig, PicsGenerator};
use crate::server::ServerConfig;

/// Cross-check the central truth-source invariant: for every core I/O/V type,
/// `is_writable_property` and `write_property_from` must agree. If the override
/// reports a property writable, `write_property_from` must accept it; if it reports
/// it read-only, `write_property_from` must reject it. This guards against the
/// override and the `write_property_from` match arms drifting apart — the exact
/// false-negative class issue #115 is meant to eliminate.
///
/// Sample values are chosen to satisfy `write_property_from`'s validation so a true
/// agreement is tested (not a rejection on bad input). Input types (AI, BI,
/// MSI) require `out_of_service` before PRESENT_VALUE is accepted, so those
/// objects are pre-flipped where needed.
#[test]
fn is_writable_property_matches_write_property_on_all_core_types() {
    use bacnet_types::bitstring::LimitEnable;
    use bacnet_types::enums::EngineeringUnits;

    // A read-only property every type rejects — used as a universal negative.
    const READ_ONLY: PropertyIdentifier = PropertyIdentifier::STATUS_FLAGS;

    // Helper: assert the override says writable AND write_property accepts it.
    fn assert_accepts(
        obj: &mut dyn BACnetObject,
        pid: PropertyIdentifier,
        value: PropertyValue,
        label: &str,
    ) {
        assert!(
            obj.is_writable_property(pid),
            "{label}: is_writable_property must report {pid:?} writable"
        );
        let result = obj.write_property_from(
            pid,
            None,
            value,
            None,
            &crate::command_source::test_origin(),
        );
        assert!(
            result.is_ok(),
            "{label}: source-aware write must accept {pid:?}, got: {result:?}"
        );
    }

    // Helper: assert the override says read-only AND write_property rejects it.
    fn assert_rejects(obj: &mut dyn BACnetObject, pid: PropertyIdentifier, label: &str) {
        assert!(
            !obj.is_writable_property(pid),
            "{label}: is_writable_property must report {pid:?} NOT writable"
        );
        let result = obj.write_property_from(
            pid,
            None,
            PropertyValue::Null,
            None,
            &crate::command_source::test_origin(),
        );
        assert!(
            result.is_err(),
            "{label}: source-aware write must reject {pid:?}, got: {result:?}"
        );
    }

    // Helper: like assert_accepts but supplies an array_index for STATE_TEXT.
    fn assert_accepts_indexed(
        obj: &mut dyn BACnetObject,
        pid: PropertyIdentifier,
        index: u32,
        value: PropertyValue,
        label: &str,
    ) {
        assert!(
            obj.is_writable_property(pid),
            "{label}: is_writable_property must report {pid:?} writable"
        );
        let result = obj.write_property_from(
            pid,
            Some(index),
            value,
            None,
            &crate::command_source::test_origin(),
        );
        assert!(
            result.is_ok(),
            "{label}: source-aware write must accept {pid:?}[{index}], got: {result:?}"
        );
    }

    // AnalogInput — PRESENT_VALUE needs out-of-service.
    let mut ai = AnalogInputObject::new(1, "ai", EngineeringUnits::NO_UNITS).unwrap();
    ai.write_property(
        PropertyIdentifier::OUT_OF_SERVICE,
        None,
        PropertyValue::Boolean(true),
        None,
    )
    .unwrap();
    assert_accepts(
        &mut ai,
        PropertyIdentifier::PRESENT_VALUE,
        PropertyValue::Real(50.0),
        "AI",
    );
    assert_accepts(
        &mut ai,
        PropertyIdentifier::COV_INCREMENT,
        PropertyValue::Real(1.0),
        "AI",
    );
    assert_accepts(
        &mut ai,
        PropertyIdentifier::RELIABILITY,
        PropertyValue::Enumerated(0),
        "AI",
    );
    assert_accepts(
        &mut ai,
        PropertyIdentifier::LIMIT_ENABLE,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![LimitEnable::all().to_bacnet()],
        },
        "AI",
    );
    assert_rejects(&mut ai, READ_ONLY, "AI");

    // AnalogOutput — commandable Present_Value with a read-only Priority_Array.
    let mut ao = AnalogOutputObject::new(1, "ao", EngineeringUnits::NO_UNITS).unwrap();
    ao.write_property(
        PropertyIdentifier::OUT_OF_SERVICE,
        None,
        PropertyValue::Boolean(true),
        None,
    )
    .unwrap();
    assert_accepts(
        &mut ao,
        PropertyIdentifier::PRESENT_VALUE,
        PropertyValue::Real(50.0),
        "AO",
    );
    assert_rejects(&mut ao, PropertyIdentifier::PRIORITY_ARRAY, "AO");
    assert_accepts(
        &mut ao,
        PropertyIdentifier::COV_INCREMENT,
        PropertyValue::Real(1.0),
        "AO",
    );
    assert_accepts(
        &mut ao,
        PropertyIdentifier::RELIABILITY,
        PropertyValue::Enumerated(0),
        "AO",
    );
    assert_accepts(
        &mut ao,
        PropertyIdentifier::NOTIFY_TYPE,
        PropertyValue::Enumerated(0),
        "AO",
    );
    assert_rejects(&mut ao, READ_ONLY, "AO");

    // AnalogValue — same writable set as AnalogOutput.
    let mut av = AnalogValueObject::new(1, "av", EngineeringUnits::NO_UNITS).unwrap();
    av.write_property(
        PropertyIdentifier::OUT_OF_SERVICE,
        None,
        PropertyValue::Boolean(true),
        None,
    )
    .unwrap();
    assert_accepts(
        &mut av,
        PropertyIdentifier::PRESENT_VALUE,
        PropertyValue::Real(50.0),
        "AV",
    );
    assert_rejects(&mut av, PropertyIdentifier::PRIORITY_ARRAY, "AV");
    assert_accepts(
        &mut av,
        PropertyIdentifier::COV_INCREMENT,
        PropertyValue::Real(1.0),
        "AV",
    );
    assert_accepts(
        &mut av,
        PropertyIdentifier::RELIABILITY,
        PropertyValue::Enumerated(0),
        "AV",
    );
    assert_accepts(
        &mut av,
        PropertyIdentifier::EVENT_ENABLE,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0xE0], // all three transitions, MSB-first
        },
        "AV",
    );
    assert_rejects(&mut av, READ_ONLY, "AV");

    // BinaryInput — PRESENT_VALUE needs out-of-service; ACTIVE/INACTIVE_TEXT.
    let mut bi = BinaryInputObject::new(1, "bi").unwrap();
    bi.write_property(
        PropertyIdentifier::OUT_OF_SERVICE,
        None,
        PropertyValue::Boolean(true),
        None,
    )
    .unwrap();
    assert_accepts(
        &mut bi,
        PropertyIdentifier::PRESENT_VALUE,
        PropertyValue::Enumerated(1),
        "BI",
    );
    assert_accepts(
        &mut bi,
        PropertyIdentifier::ACTIVE_TEXT,
        PropertyValue::CharacterString("on".into()),
        "BI",
    );
    assert_accepts(
        &mut bi,
        PropertyIdentifier::INACTIVE_TEXT,
        PropertyValue::CharacterString("off".into()),
        "BI",
    );
    assert_accepts(
        &mut bi,
        PropertyIdentifier::EVENT_ENABLE,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0xE0], // all three transitions, MSB-first
        },
        "BI",
    );
    assert_rejects(&mut bi, READ_ONLY, "BI");

    // BinaryOutput — commandable + ACTIVE/INACTIVE_TEXT.
    let mut bo = BinaryOutputObject::new(1, "bo").unwrap();
    assert_accepts(
        &mut bo,
        PropertyIdentifier::PRESENT_VALUE,
        PropertyValue::Enumerated(1),
        "BO",
    );
    assert_rejects(&mut bo, PropertyIdentifier::PRIORITY_ARRAY, "BO");
    assert_accepts(
        &mut bo,
        PropertyIdentifier::ACTIVE_TEXT,
        PropertyValue::CharacterString("on".into()),
        "BO",
    );
    assert_accepts(
        &mut bo,
        PropertyIdentifier::INACTIVE_TEXT,
        PropertyValue::CharacterString("off".into()),
        "BO",
    );
    assert_rejects(&mut bo, READ_ONLY, "BO");

    // BinaryValue — same writable set as BinaryOutput (no dedicated mirror
    // test exists, so this is the BV override's regression guard).
    let mut bv = BinaryValueObject::new(1, "bv").unwrap();
    assert_accepts(
        &mut bv,
        PropertyIdentifier::PRESENT_VALUE,
        PropertyValue::Enumerated(1),
        "BV",
    );
    assert_rejects(&mut bv, PropertyIdentifier::PRIORITY_ARRAY, "BV");
    assert_accepts(
        &mut bv,
        PropertyIdentifier::ACTIVE_TEXT,
        PropertyValue::CharacterString("on".into()),
        "BV",
    );
    assert_accepts(
        &mut bv,
        PropertyIdentifier::INACTIVE_TEXT,
        PropertyValue::CharacterString("off".into()),
        "BV",
    );
    assert_accepts(
        &mut bv,
        PropertyIdentifier::NOTIFY_TYPE,
        PropertyValue::Enumerated(0),
        "BV",
    );
    assert_rejects(&mut bv, READ_ONLY, "BV");

    // MultiStateInput — PRESENT_VALUE needs out-of-service; STATE_TEXT (array).
    let mut msi = MultiStateInputObject::new(1, "msi", 2).unwrap();
    msi.write_property(
        PropertyIdentifier::OUT_OF_SERVICE,
        None,
        PropertyValue::Boolean(true),
        None,
    )
    .unwrap();
    assert_accepts(
        &mut msi,
        PropertyIdentifier::PRESENT_VALUE,
        PropertyValue::Unsigned(1),
        "MSI",
    );
    assert_accepts_indexed(
        &mut msi,
        PropertyIdentifier::STATE_TEXT,
        1,
        PropertyValue::CharacterString("s1".into()),
        "MSI",
    );
    assert_accepts(
        &mut msi,
        PropertyIdentifier::TIME_DELAY,
        PropertyValue::Unsigned(5),
        "MSI",
    );
    assert_rejects(&mut msi, READ_ONLY, "MSI");

    // MultiStateOutput — commandable + STATE_TEXT (array).
    let mut mso = MultiStateOutputObject::new(1, "mso", 2).unwrap();
    assert_accepts(
        &mut mso,
        PropertyIdentifier::PRESENT_VALUE,
        PropertyValue::Unsigned(1),
        "MSO",
    );
    assert_rejects(&mut mso, PropertyIdentifier::PRIORITY_ARRAY, "MSO");
    assert_accepts_indexed(
        &mut mso,
        PropertyIdentifier::STATE_TEXT,
        1,
        PropertyValue::CharacterString("s1".into()),
        "MSO",
    );
    assert_rejects(&mut mso, READ_ONLY, "MSO");

    // MultiStateValue — commandable + STATE_TEXT (array).
    let mut msv = MultiStateValueObject::new(1, "msv", 2).unwrap();
    assert_accepts(
        &mut msv,
        PropertyIdentifier::PRESENT_VALUE,
        PropertyValue::Unsigned(1),
        "MSV",
    );
    assert_rejects(&mut msv, PropertyIdentifier::PRIORITY_ARRAY, "MSV");
    assert_accepts_indexed(
        &mut msv,
        PropertyIdentifier::STATE_TEXT,
        1,
        PropertyValue::CharacterString("s1".into()),
        "MSV",
    );
    assert_accepts(
        &mut msv,
        PropertyIdentifier::NOTIFICATION_CLASS,
        PropertyValue::Unsigned(3),
        "MSV",
    );
    assert_rejects(&mut msv, READ_ONLY, "MSV");
}

/// The per-object `write_property_from` arms and the `is_writable_property`
/// override must agree on the two objects hardened in the #182 review round:
/// Pulse Converter (whose historical default advertised INPUT_REFERENCE
/// read-only while the arm accepted it — and OBJECT_NAME writable while no
/// arm routes it) and Averaging (same OBJECT_NAME misadvertising; its
/// OBJECT_PROPERTY_REFERENCE arm stayed unadvertised). Each advertised
/// property is written with a canonical good value; each unadvertised one
/// must reject even a plausible value.
#[test]
fn is_writable_property_matches_write_property_on_pulse_converter_and_averaging() {
    use bacnet_objects::accumulator::PulseConverterObject;
    use bacnet_objects::averaging::AveragingObject;
    use bacnet_objects::database::ObjectDatabase;
    use bacnet_types::enums::EngineeringUnits;
    use bacnet_types::enums::ObjectType;

    fn assert_exactly(
        obj: &mut dyn BACnetObject,
        label: &str,
        accepted: &[(PropertyIdentifier, PropertyValue)],
        rejected: &[(PropertyIdentifier, PropertyValue)],
    ) {
        for (pid, value) in accepted.iter().cloned() {
            assert!(
                obj.is_writable_property(pid),
                "{label}: {pid:?} must be advertised writable"
            );
            assert!(
                obj.write_property_from(
                    pid,
                    None,
                    value,
                    None,
                    &crate::command_source::test_origin()
                )
                .is_ok(),
                "{label}: {pid:?} advertised writable but the arm rejected a good value"
            );
        }
        for (pid, value) in rejected.iter().cloned() {
            assert!(
                !obj.is_writable_property(pid),
                "{label}: {pid:?} must NOT be advertised writable"
            );
            assert!(
                obj.write_property_from(
                    pid,
                    None,
                    value,
                    None,
                    &crate::command_source::test_origin()
                )
                .is_err(),
                "{label}: {pid:?} not advertised but the arm accepted a write"
            );
        }
    }

    // [0] analog-input 5, [1] present-value: the Clause 21 members (#1312).
    let reference_value =
        PropertyValue::ApplicationData(vec![0x0C, 0x00, 0x00, 0x00, 0x05, 0x19, 0x55]);

    let mut pc = PulseConverterObject::new(1, "PC-1", EngineeringUnits::DEGREES_CELSIUS).unwrap();
    // Count must cover the adjustment below: 2.0 / 1.5 takes one pulse off.
    pc.add_pulses(1).unwrap();
    assert_exactly(
        &mut pc,
        "PC",
        &[
            (
                PropertyIdentifier::OUT_OF_SERVICE,
                PropertyValue::Boolean(true),
            ),
            (PropertyIdentifier::PRESENT_VALUE, PropertyValue::Real(10.0)),
            // Out of service, as above (Clause 12.23.10, #1341).
            (
                PropertyIdentifier::RELIABILITY,
                PropertyValue::Enumerated(0),
            ),
            (PropertyIdentifier::SCALE_FACTOR, PropertyValue::Real(1.5)),
            (PropertyIdentifier::ADJUST_VALUE, PropertyValue::Real(2.0)),
            (PropertyIdentifier::COV_INCREMENT, PropertyValue::Real(0.5)),
            (PropertyIdentifier::INPUT_REFERENCE, reference_value),
            (
                PropertyIdentifier::DESCRIPTION,
                PropertyValue::CharacterString("d".into()),
            ),
        ],
        &[
            (
                PropertyIdentifier::OBJECT_NAME,
                PropertyValue::CharacterString("renamed".into()),
            ),
            (PropertyIdentifier::UNITS, PropertyValue::Enumerated(95)),
            (
                PropertyIdentifier::EVENT_STATE,
                PropertyValue::Enumerated(0),
            ),
            (
                PropertyIdentifier::STATUS_FLAGS,
                PropertyValue::BitString {
                    unused_bits: 4,
                    data: vec![0],
                },
            ),
        ],
    );

    let mut db = ObjectDatabase::new();
    db.add(Box::new(pc)).unwrap();
    let pics = PicsGenerator::new(&db, &ServerConfig::default(), &PicsConfig::default()).generate();
    let pulse_support = pics
        .supported_object_types
        .iter()
        .find(|support| support.object_type == ObjectType::PULSE_CONVERTER)
        .unwrap();
    assert!(pulse_support.supported_properties.iter().any(|property| {
        property.property_id == PropertyIdentifier::PRESENT_VALUE && property.access.writable
    }));

    let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
    assert_exactly(
        &mut avg,
        "AVG",
        &[
            (
                PropertyIdentifier::OBJECT_PROPERTY_REFERENCE,
                // The Clause 21 form Averaging serves (#1182).
                PropertyValue::ApplicationData(vec![0x0C, 0x00, 0x00, 0x00, 0x05, 0x19, 0x55]),
            ),
            (
                PropertyIdentifier::DESCRIPTION,
                PropertyValue::CharacterString("d".into()),
            ),
            // The window rows and the Attempted_Samples reset (#1092).
            (
                PropertyIdentifier::WINDOW_INTERVAL,
                PropertyValue::Unsigned(60),
            ),
            (
                PropertyIdentifier::WINDOW_SAMPLES,
                PropertyValue::Unsigned(30),
            ),
            (
                PropertyIdentifier::ATTEMPTED_SAMPLES,
                PropertyValue::Unsigned(0),
            ),
        ],
        &[
            (
                PropertyIdentifier::OBJECT_NAME,
                PropertyValue::CharacterString("renamed".into()),
            ),
            // Table 12-5 has no Out_Of_Service or Present_Value (#1064).
            (
                PropertyIdentifier::OUT_OF_SERVICE,
                PropertyValue::Boolean(true),
            ),
            (PropertyIdentifier::PRESENT_VALUE, PropertyValue::Real(1.0)),
            (PropertyIdentifier::MINIMUM_VALUE, PropertyValue::Real(1.0)),
            (
                PropertyIdentifier::VALID_SAMPLES,
                PropertyValue::Unsigned(0),
            ),
            (
                PropertyIdentifier::RELIABILITY,
                PropertyValue::Enumerated(0),
            ),
        ],
    );
}

#[test]
fn pics_log_family_writability_comes_from_runtime_routes() {
    use bacnet_objects::database::ObjectDatabase;
    use bacnet_objects::event_log::EventLogObject;
    use bacnet_objects::trend::{TrendLogMultipleObject, TrendLogObject};

    let mut db = ObjectDatabase::new();
    db.add(Box::new(EventLogObject::new(1, "EL-1", 3).unwrap()))
        .unwrap();
    db.add(Box::new(TrendLogObject::new(1, "TL-1", 3).unwrap()))
        .unwrap();
    db.add(Box::new(
        TrendLogMultipleObject::new(1, "TLM-1", 3).unwrap(),
    ))
    .unwrap();
    let server_config = ServerConfig::default();
    let pics_config = PicsConfig::default();
    let pics = PicsGenerator::new(&db, &server_config, &pics_config).generate();

    let cases = [
        (
            // Table 12-31 has no Log_Interval or Out_Of_Service (#1064).
            ObjectType::EVENT_LOG,
            &[
                PropertyIdentifier::LOG_ENABLE,
                PropertyIdentifier::STOP_WHEN_FULL,
                PropertyIdentifier::RECORD_COUNT,
                PropertyIdentifier::DESCRIPTION,
                // The window (#1353).
                PropertyIdentifier::START_TIME,
                PropertyIdentifier::STOP_TIME,
            ][..],
        ),
        (
            ObjectType::TREND_LOG,
            &[
                PropertyIdentifier::LOG_ENABLE,
                PropertyIdentifier::LOG_INTERVAL,
                PropertyIdentifier::STOP_WHEN_FULL,
                PropertyIdentifier::RECORD_COUNT,
                PropertyIdentifier::DESCRIPTION,
                PropertyIdentifier::LOG_DEVICE_OBJECT_PROPERTY,
                // Logging_Type, the window, alignment and Trigger (#1353,
                // #1354).
                PropertyIdentifier::LOGGING_TYPE,
                PropertyIdentifier::START_TIME,
                PropertyIdentifier::STOP_TIME,
                PropertyIdentifier::ALIGN_INTERVALS,
                PropertyIdentifier::INTERVAL_OFFSET,
                PropertyIdentifier::TRIGGER,
                // The COV rows (#1480).
                PropertyIdentifier::COV_RESUBSCRIPTION_INTERVAL,
                PropertyIdentifier::CLIENT_COV_INCREMENT,
            ][..],
        ),
        (
            ObjectType::TREND_LOG_MULTIPLE,
            &[
                PropertyIdentifier::LOG_ENABLE,
                PropertyIdentifier::LOG_INTERVAL,
                PropertyIdentifier::STOP_WHEN_FULL,
                PropertyIdentifier::RECORD_COUNT,
                PropertyIdentifier::DESCRIPTION,
                PropertyIdentifier::LOG_DEVICE_OBJECT_PROPERTY,
                // Logging_Type, the window, alignment and Trigger (#1235).
                PropertyIdentifier::LOGGING_TYPE,
                PropertyIdentifier::START_TIME,
                PropertyIdentifier::STOP_TIME,
                PropertyIdentifier::ALIGN_INTERVALS,
                PropertyIdentifier::INTERVAL_OFFSET,
                PropertyIdentifier::TRIGGER,
            ][..],
        ),
    ];
    for (object_type, expected) in cases {
        let support = pics
            .supported_object_types
            .iter()
            .find(|support| support.object_type == object_type)
            .unwrap();
        let writable = support
            .supported_properties
            .iter()
            .filter(|property| property.access.writable)
            .map(|property| property.property_id)
            .collect::<Vec<_>>();
        // Each log's BUFFER_READY configuration is writable too (#1347),
        // as are the message texts and the inhibit pair (#1329).
        let reporting = [
            PropertyIdentifier::NOTIFICATION_THRESHOLD,
            PropertyIdentifier::NOTIFICATION_CLASS,
            PropertyIdentifier::EVENT_ENABLE,
            PropertyIdentifier::NOTIFY_TYPE,
            PropertyIdentifier::EVENT_DETECTION_ENABLE,
        ];
        let options = [
            PropertyIdentifier::EVENT_MESSAGE_TEXTS_CONFIG,
            PropertyIdentifier::EVENT_ALGORITHM_INHIBIT_REF,
            PropertyIdentifier::EVENT_ALGORITHM_INHIBIT,
        ];
        assert_eq!(
            writable.len(),
            expected.len() + reporting.len() + options.len(),
            "{object_type:?}"
        );
        for property in expected.iter().chain(&reporting).chain(&options) {
            assert!(writable.contains(property), "{object_type:?} {property:?}");
        }
        // A Trend Log samples a BACnet property, so Table 12-29 footnotes 1
        // and 8 require its window, Log_Interval and Log_DeviceObjectProperty
        // (#1481). Table 12-35 requires the last two outright, and neither it
        // nor Table 12-31 requires a window.
        let optional = |property| {
            support
                .supported_properties
                .iter()
                .find(|row| row.property_id == property)
                .map(|row| row.access.optional)
        };
        let trend = object_type == ObjectType::TREND_LOG;
        for property in [
            PropertyIdentifier::START_TIME,
            PropertyIdentifier::STOP_TIME,
        ] {
            assert_eq!(
                optional(property),
                Some(!trend),
                "{object_type:?} {property:?}"
            );
        }
        if object_type != ObjectType::EVENT_LOG {
            for property in [
                PropertyIdentifier::LOG_INTERVAL,
                PropertyIdentifier::LOG_DEVICE_OBJECT_PROPERTY,
            ] {
                assert_eq!(
                    optional(property),
                    Some(false),
                    "{object_type:?} {property:?}"
                );
            }
        }
        // The three tables require every BUFFER_READY row of a log that
        // reports intrinsically, as these do, but Event_Message_Texts,
        // which they only permit (#1485).
        let read_only = [
            PropertyIdentifier::RECORDS_SINCE_NOTIFICATION,
            PropertyIdentifier::LAST_NOTIFY_RECORD,
            PropertyIdentifier::ACKED_TRANSITIONS,
            PropertyIdentifier::EVENT_TIME_STAMPS,
        ];
        for property in reporting.iter().chain(&read_only) {
            assert_eq!(
                optional(*property),
                Some(false),
                "{object_type:?} {property:?}"
            );
        }
        // The Trend Log's two COV rows (#1480) are writable above; that
        // Table 12-29 only permits them is pinned with the RPM rows in
        // handlers/tests/property_metadata/log.rs.
        let texts = PropertyIdentifier::EVENT_MESSAGE_TEXTS;
        for property in options.iter().chain([&texts]) {
            assert_eq!(optional(*property), Some(true), "{object_type:?}");
        }
    }
}

#[test]
fn pics_file_resize_writability_matches_single_instance_metadata() {
    use bacnet_objects::database::ObjectDatabase;
    use bacnet_objects::file::FileObject;
    use bacnet_types::enums::FileAccessMethod;

    fn property_access(
        file: FileObject,
        property: PropertyIdentifier,
    ) -> Option<super::PropertyAccess> {
        let mut db = ObjectDatabase::new();
        db.add(Box::new(file)).unwrap();
        PicsGenerator::new(&db, &ServerConfig::default(), &PicsConfig::default())
            .generate()
            .supported_object_types
            .into_iter()
            .find(|support| support.object_type == ObjectType::FILE)
            .unwrap()
            .supported_properties
            .into_iter()
            .find(|support| support.property_id == property)
            .map(|support| support.access)
    }

    let stream = FileObject::new(1, "STREAM", "raw").unwrap();
    assert!(
        property_access(stream, PropertyIdentifier::FILE_SIZE)
            .unwrap()
            .writable
    );
    let stream = FileObject::new(2, "STREAM", "raw").unwrap();
    assert!(property_access(stream, PropertyIdentifier::RECORD_COUNT).is_none());

    let mut record = FileObject::new(3, "RECORD", "raw").unwrap();
    record.set_file_access_method(FileAccessMethod::RECORD_ACCESS.to_raw());
    assert!(
        !property_access(record, PropertyIdentifier::FILE_SIZE)
            .unwrap()
            .writable
    );
    let mut record = FileObject::new(4, "RECORD", "raw").unwrap();
    record.set_file_access_method(FileAccessMethod::RECORD_ACCESS.to_raw());
    assert!(
        property_access(record, PropertyIdentifier::RECORD_COUNT)
            .unwrap()
            .writable
    );

    let mut read_only_stream = FileObject::new(5, "RO-STREAM", "raw").unwrap();
    read_only_stream.set_read_only(true);
    assert!(
        !property_access(read_only_stream, PropertyIdentifier::FILE_SIZE)
            .unwrap()
            .writable
    );
    let mut read_only_record = FileObject::new(6, "RO-RECORD", "raw").unwrap();
    read_only_record.set_file_access_method(FileAccessMethod::RECORD_ACCESS.to_raw());
    read_only_record.set_read_only(true);
    assert!(
        !property_access(read_only_record, PropertyIdentifier::RECORD_COUNT)
            .unwrap()
            .writable
    );
}