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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
use std::borrow::Cow;

use bacnet_objects::traits::BACnetObject;
use bacnet_types::enums::{
    EngineeringUnits, ErrorClass, ErrorCode, ObjectType, PropertyIdentifier,
};
use bacnet_types::error::Error;
use bacnet_types::primitives::{ObjectIdentifier, PropertyValue};

use super::*;

// ── Minimal test objects ───────────────────────────────────────────

struct TestAnalogInput {
    oid: ObjectIdentifier,
    name: String,
}

impl BACnetObject for TestAnalogInput {
    fn object_identifier(&self) -> ObjectIdentifier {
        self.oid
    }
    fn object_name(&self) -> &str {
        &self.name
    }
    fn read_property(
        &self,
        _property: PropertyIdentifier,
        _array_index: Option<u32>,
    ) -> Result<PropertyValue, Error> {
        Ok(PropertyValue::Real(0.0))
    }
    fn write_property(
        &mut self,
        _property: PropertyIdentifier,
        _array_index: Option<u32>,
        _value: PropertyValue,
        _priority: Option<u8>,
    ) -> Result<(), Error> {
        Err(Error::protocol(
            ErrorClass::PROPERTY,
            ErrorCode::WRITE_ACCESS_DENIED,
        ))
    }
    fn property_list(&self) -> Cow<'static, [PropertyIdentifier]> {
        static PROPS: [PropertyIdentifier; 8] = [
            PropertyIdentifier::OBJECT_IDENTIFIER,
            PropertyIdentifier::OBJECT_NAME,
            PropertyIdentifier::OBJECT_TYPE,
            PropertyIdentifier::PROPERTY_LIST,
            PropertyIdentifier::PRESENT_VALUE,
            PropertyIdentifier::STATUS_FLAGS,
            PropertyIdentifier::OUT_OF_SERVICE,
            PropertyIdentifier::UNITS,
        ];
        Cow::Borrowed(&PROPS)
    }
}

struct TestBinaryValue {
    oid: ObjectIdentifier,
    name: String,
}

impl BACnetObject for TestBinaryValue {
    fn object_identifier(&self) -> ObjectIdentifier {
        self.oid
    }
    fn object_name(&self) -> &str {
        &self.name
    }
    fn read_property(
        &self,
        _property: PropertyIdentifier,
        _array_index: Option<u32>,
    ) -> Result<PropertyValue, Error> {
        Ok(PropertyValue::Boolean(false))
    }
    fn write_property(
        &mut self,
        _property: PropertyIdentifier,
        _array_index: Option<u32>,
        _value: PropertyValue,
        _priority: Option<u8>,
    ) -> Result<(), Error> {
        Ok(())
    }
    fn property_list(&self) -> Cow<'static, [PropertyIdentifier]> {
        static PROPS: [PropertyIdentifier; 6] = [
            PropertyIdentifier::OBJECT_IDENTIFIER,
            PropertyIdentifier::OBJECT_NAME,
            PropertyIdentifier::OBJECT_TYPE,
            PropertyIdentifier::PROPERTY_LIST,
            PropertyIdentifier::PRESENT_VALUE,
            PropertyIdentifier::STATUS_FLAGS,
        ];
        Cow::Borrowed(&PROPS)
    }
}

struct TestDevice {
    oid: ObjectIdentifier,
    name: String,
}

impl BACnetObject for TestDevice {
    fn object_identifier(&self) -> ObjectIdentifier {
        self.oid
    }
    fn object_name(&self) -> &str {
        &self.name
    }
    fn read_property(
        &self,
        property: PropertyIdentifier,
        _array_index: Option<u32>,
    ) -> Result<PropertyValue, Error> {
        // The PICS prints the Device's Vendor_Name. Its Vendor_Identifier
        // isn't an unsigned value, so the ServerConfig's vendor ID stands in.
        Ok(match property {
            PropertyIdentifier::VENDOR_NAME => PropertyValue::CharacterString("Acme Corp".into()),
            PropertyIdentifier::VENDOR_IDENTIFIER => PropertyValue::Null,
            _ => PropertyValue::Unsigned(0),
        })
    }
    fn write_property(
        &mut self,
        _property: PropertyIdentifier,
        _array_index: Option<u32>,
        _value: PropertyValue,
        _priority: Option<u8>,
    ) -> Result<(), Error> {
        Err(Error::protocol(
            ErrorClass::PROPERTY,
            ErrorCode::WRITE_ACCESS_DENIED,
        ))
    }
    fn property_list(&self) -> Cow<'static, [PropertyIdentifier]> {
        static PROPS: [PropertyIdentifier; 6] = [
            PropertyIdentifier::OBJECT_IDENTIFIER,
            PropertyIdentifier::OBJECT_NAME,
            PropertyIdentifier::OBJECT_TYPE,
            PropertyIdentifier::PROPERTY_LIST,
            PropertyIdentifier::PROTOCOL_VERSION,
            PropertyIdentifier::PROTOCOL_REVISION,
        ];
        Cow::Borrowed(&PROPS)
    }
    /// Device is not createable or deleteable; mirrors the real DeviceObject.
    fn is_createable(&self) -> bool {
        false
    }
    fn is_deleteable(&self) -> bool {
        false
    }
}

// ── Helpers ────────────────────────────────────────────────────────

fn make_test_db() -> ObjectDatabase {
    let mut db = ObjectDatabase::new();
    db.add(Box::new(TestDevice {
        oid: ObjectIdentifier::new(ObjectType::DEVICE, 1).unwrap(),
        name: "Test Device".into(),
    }))
    .unwrap();
    db.add(Box::new(TestAnalogInput {
        oid: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap(),
        name: "AI-1".into(),
    }))
    .unwrap();
    db.add(Box::new(TestAnalogInput {
        oid: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 2).unwrap(),
        name: "AI-2".into(),
    }))
    .unwrap();
    db.add(Box::new(TestBinaryValue {
        oid: ObjectIdentifier::new(ObjectType::BINARY_VALUE, 1).unwrap(),
        name: "BV-1".into(),
    }))
    .unwrap();
    db
}

fn make_pics_config() -> PicsConfig {
    PicsConfig {
        device_profile: DeviceProfile::BAsc,
        data_link_layers: vec![DataLinkSupport::BipV4],
        network_layer: NetworkLayerSupport {
            router: false,
            bbmd: false,
            foreign_device: false,
        },
        character_sets: vec![CharacterSet::Utf8],
        special_functionality: vec!["Intrinsic event reporting".into()],
    }
}

// ── Tests ──────────────────────────────────────────────────────────

#[test]
fn generate_pics_basic() {
    let db = make_test_db();
    let server_config = ServerConfig {
        device: bacnet_objects::DeviceIdentity::new(1, 999).expect("valid Device identity"),
        ..ServerConfig::default()
    };
    let pics_config = make_pics_config();
    let pics = generate_pics(&db, &server_config, &pics_config);

    assert_eq!(pics.vendor_info.vendor_id, 999);
    assert_eq!(pics.vendor_info.vendor_name, "Acme Corp");
    assert_eq!(pics.device_profile, DeviceProfile::BAsc);
    assert_eq!(pics.character_sets, vec![CharacterSet::Utf8]);
    assert_eq!(pics.data_link_layers, vec![DataLinkSupport::BipV4]);
}

#[test]
fn all_object_types_listed() {
    let db = make_test_db();
    let server_config = ServerConfig::default();
    let pics_config = make_pics_config();
    let pics = generate_pics(&db, &server_config, &pics_config);

    let types: Vec<ObjectType> = pics
        .supported_object_types
        .iter()
        .map(|ot| ot.object_type)
        .collect();
    assert!(types.contains(&ObjectType::DEVICE));
    assert!(types.contains(&ObjectType::ANALOG_INPUT));
    assert!(types.contains(&ObjectType::BINARY_VALUE));
    // 3 distinct types in our test DB
    assert_eq!(types.len(), 3);
}

#[test]
fn object_type_properties_populated() {
    let db = make_test_db();
    let unmigrated = db
        .iter_objects()
        .find_map(|(_, object)| {
            (object.object_identifier().object_type() == ObjectType::ANALOG_INPUT).then_some(object)
        })
        .unwrap();
    assert!(
        unmigrated.property_metadata().is_empty(),
        "the test fixture must exercise the legacy PICS fallback"
    );
    let server_config = ServerConfig::default();
    let pics_config = make_pics_config();
    let pics = generate_pics(&db, &server_config, &pics_config);

    let ai = pics
        .supported_object_types
        .iter()
        .find(|ot| ot.object_type == ObjectType::ANALOG_INPUT)
        .expect("ANALOG_INPUT should be in PICS");

    // AI has 8 properties in our test fixture
    assert_eq!(ai.supported_properties.len(), 8);

    // The TestAnalogInput stub does not override is_writable_property, so it
    // inherits the default historical_writable_default heuristic, which reports
    // PRESENT_VALUE read-only for ANALOG_INPUT. The real AnalogInputObject
    // overrides this to writable-when-out-of-service — see
    // pics_input_present_value_writable_only_when_out_of_service.
    let pv = ai
        .supported_properties
        .iter()
        .find(|p| p.property_id == PropertyIdentifier::PRESENT_VALUE)
        .expect("PRESENT_VALUE should exist");
    assert!(pv.access.readable);
    assert!(!pv.access.writable);
}

#[test]
fn device_not_createable_or_deleteable() {
    let db = make_test_db();
    let server_config = ServerConfig::default();
    let pics_config = make_pics_config();
    let pics = generate_pics(&db, &server_config, &pics_config);

    let dev = pics
        .supported_object_types
        .iter()
        .find(|ot| ot.object_type == ObjectType::DEVICE)
        .expect("DEVICE should be in PICS");
    assert!(!dev.createable);
    assert!(!dev.deleteable);
}

#[test]
fn real_device_and_network_port_overrides_not_createable_or_deleteable() {
    // Directly exercise the real DeviceObject and NetworkPortObject trait
    // overrides (not the TestDevice stub) so a regression flipping either
    // override to true fails here rather than silently changing PICS output.
    use bacnet_objects::device::{DeviceConfig, DeviceObject};
    use bacnet_objects::network_port::NetworkPortObject;

    let device = DeviceObject::new(DeviceConfig {
        identity: bacnet_objects::DeviceIdentity::default().with_name("Dev"),
        ..Default::default()
    })
    .unwrap();
    assert!(!device.is_createable(), "Device must not be createable");
    assert!(!device.is_deleteable(), "Device must not be deleteable");

    let np = NetworkPortObject::new_non_bip(
        1,
        "NP-1",
        bacnet_types::enums::NetworkType::from_raw(0),
        0,
        Default::default(),
        1476,
    )
    .unwrap();
    assert!(!np.is_createable(), "NetworkPort must not be createable");
    assert!(!np.is_deleteable(), "NetworkPort must not be deleteable");
}

#[test]
fn services_match_implementation() {
    let db = make_test_db();
    let server_config = ServerConfig::default();
    let pics_config = make_pics_config();
    let pics = generate_pics(&db, &server_config, &pics_config);

    let service_names: Vec<&str> = pics
        .supported_services
        .iter()
        .map(|s| s.service_name.as_str())
        .collect();

    // Executor services
    assert!(service_names.contains(&"ReadProperty"));
    assert!(service_names.contains(&"WriteProperty"));
    assert!(service_names.contains(&"ReadPropertyMultiple"));
    assert!(service_names.contains(&"SubscribeCOV"));
    assert!(service_names.contains(&"CreateObject"));
    assert!(service_names.contains(&"DeleteObject"));
    assert!(service_names.contains(&"WhoIs"));
    assert!(
        service_names.contains(&"WriteGroup"),
        "server PICS lists the WriteGroup it executes on Channels"
    );

    // Initiator services
    assert!(service_names.contains(&"I-Am"));
    assert!(service_names.contains(&"ConfirmedCOVNotification"));

    // Check initiator/executor flags on ReadProperty
    let rp = pics
        .supported_services
        .iter()
        .find(|s| s.service_name == "ReadProperty")
        .expect("ReadProperty should be listed");
    // It initiates ReadProperty too, for a Channel member in another device.
    assert!(rp.initiator);
    assert!(rp.executor);

    // I-Am is initiator only
    let iam = pics
        .supported_services
        .iter()
        .find(|s| s.service_name == "I-Am")
        .expect("I-Am should be listed");
    assert!(iam.initiator);
    assert!(!iam.executor);
}

#[test]
fn text_output_contains_key_sections() {
    let db = make_test_db();
    let server_config = ServerConfig {
        device: bacnet_objects::DeviceIdentity::new(1, 42).expect("valid Device identity"),
        ..ServerConfig::default()
    };
    let pics_config = make_pics_config();
    let pics = generate_pics(&db, &server_config, &pics_config);
    let text = pics.generate_text();

    assert!(text.contains("Protocol Implementation Conformance Statement"));
    assert!(text.contains("Vendor ID:"));
    assert!(text.contains("42"));
    assert!(text.contains("Acme Corp"));
    assert!(text.contains("B-ASC"));
    assert!(text.contains("Supported Object Types"));
    assert!(text.contains("ANALOG_INPUT"));
    assert!(text.contains("Supported Services"));
    assert!(text.contains("ReadProperty"));
    assert!(text.contains("Data Link Layer Support"));
    assert!(text.contains("BACnet/IP (Annex J)"));
    assert!(text.contains("Character Sets Supported"));
    assert!(text.contains("ISO 10646 (UTF-8)"));
    assert!(text.contains("Special Functionality"));
    assert!(text.contains("Intrinsic event reporting"));
}

#[test]
fn markdown_output_has_tables() {
    let db = make_test_db();
    let server_config = ServerConfig::default();
    let pics_config = make_pics_config();
    let pics = generate_pics(&db, &server_config, &pics_config);
    let md = pics.generate_markdown();

    assert!(md.contains("# BACnet Protocol Implementation Conformance Statement"));
    assert!(md.contains("| Field | Value |"));
    assert!(md.contains("| Service | Initiator | Executor |"));
    assert!(md.contains("| Property | Access |"));
    assert!(md.contains("## Supported Object Types"));
    assert!(md.contains("## Supported Services"));
}

#[test]
fn empty_database_produces_empty_object_list() {
    let db = ObjectDatabase::new();
    let server_config = ServerConfig::default();
    let pics_config = PicsConfig::default();
    let pics = generate_pics(&db, &server_config, &pics_config);

    assert!(pics.supported_object_types.is_empty());
    assert!(!pics.supported_services.is_empty());
}

#[test]
fn device_profile_display() {
    assert_eq!(DeviceProfile::BAac.to_string(), "B-AAC");
    assert_eq!(DeviceProfile::BAsc.to_string(), "B-ASC");
    assert_eq!(DeviceProfile::BOws.to_string(), "B-OWS");
    assert_eq!(DeviceProfile::BBc.to_string(), "B-BC");
    assert_eq!(DeviceProfile::BOp.to_string(), "B-OP");
    assert_eq!(DeviceProfile::BRouter.to_string(), "B-ROUTER");
    assert_eq!(DeviceProfile::BGw.to_string(), "B-GW");
    assert_eq!(DeviceProfile::BSc.to_string(), "B-SC");
    assert_eq!(
        DeviceProfile::Custom("MyProfile".into()).to_string(),
        "MyProfile"
    );
}

#[test]
fn property_access_display() {
    let rw = PropertyAccess {
        readable: true,
        writable: true,
        optional: false,
    };
    assert_eq!(rw.to_string(), "RW");

    let ro = PropertyAccess {
        readable: true,
        writable: false,
        optional: true,
    };
    assert_eq!(ro.to_string(), "RO");

    let wo = PropertyAccess {
        readable: false,
        writable: true,
        optional: false,
    };
    assert_eq!(wo.to_string(), "W");
}

#[test]
fn binary_value_present_value_is_writable() {
    let db = make_test_db();
    let server_config = ServerConfig::default();
    let pics_config = make_pics_config();
    let pics = generate_pics(&db, &server_config, &pics_config);

    let bv = pics
        .supported_object_types
        .iter()
        .find(|ot| ot.object_type == ObjectType::BINARY_VALUE)
        .expect("BINARY_VALUE should be in PICS");

    let pv = bv
        .supported_properties
        .iter()
        .find(|p| p.property_id == PropertyIdentifier::PRESENT_VALUE)
        .expect("PRESENT_VALUE should exist on BV");
    assert!(
        pv.access.writable,
        "BinaryValue PRESENT_VALUE should be writable"
    );
}

// ── Real-object PICS writability/createability tests ───────────────────────
//
// The tests above use minimal stub objects. The tests below use the real
// bacnet-objects implementations to verify that PICS writable flags and
// createability match the objects' own `write_property` arms and the runtime
// `handle_create_object` factory. This is the core regression guard for the
// shared-truth-source fix (issue #115): PICS and runtime dispatch must agree.

use bacnet_objects::analog::{AnalogInputObject, AnalogOutputObject, AnalogValueObject};
use bacnet_objects::binary::{BinaryInputObject, BinaryOutputObject, BinaryValueObject};
use bacnet_objects::multistate::{
    MultiStateInputObject, MultiStateOutputObject, MultiStateValueObject,
};

const NO_UNITS: EngineeringUnits = EngineeringUnits::NO_UNITS;

/// Build a database with one of each of the 9 core I/O/V object types.
fn make_real_objects_db() -> ObjectDatabase {
    let mut db = ObjectDatabase::new();
    let ai = AnalogInputObject::new(1, "ai-1", NO_UNITS).unwrap();
    db.add(Box::new(ai)).unwrap();
    let ao = AnalogOutputObject::new(1, "ao-1", NO_UNITS).unwrap();
    db.add(Box::new(ao)).unwrap();
    let av = AnalogValueObject::new(1, "av-1", NO_UNITS).unwrap();
    db.add(Box::new(av)).unwrap();
    db.add(Box::new(BinaryInputObject::new(1, "bi-1").unwrap()))
        .unwrap();
    db.add(Box::new(BinaryOutputObject::new(1, "bo-1").unwrap()))
        .unwrap();
    db.add(Box::new(BinaryValueObject::new(1, "bv-1").unwrap()))
        .unwrap();
    db.add(Box::new(MultiStateInputObject::new(1, "msi-1", 2).unwrap()))
        .unwrap();
    db.add(Box::new(
        MultiStateOutputObject::new(1, "mso-1", 2).unwrap(),
    ))
    .unwrap();
    db.add(Box::new(MultiStateValueObject::new(1, "msv-1", 2).unwrap()))
        .unwrap();
    db
}

/// Helper: look up a property's writable flag in a PICS ObjectTypeSupport.
fn pics_writable(pics: &Pics, object_type: ObjectType, pid: PropertyIdentifier) -> bool {
    pics.supported_object_types
        .iter()
        .find(|ot| ot.object_type == object_type)
        .and_then(|ot| {
            ot.supported_properties
                .iter()
                .find(|p| p.property_id == pid)
        })
        .map(|p| p.access.writable)
        .expect("property should be in the PICS list")
}

#[test]
fn pics_event_properties_writable_on_analog_types() {
    // The old heuristic omitted LIMIT_ENABLE, NOTIFY_TYPE, TIME_DELAY,
    // EVENT_ENABLE — which the objects actually accept via write_generic_event_properties!.
    let db = make_real_objects_db();
    let pics = generate_pics(&db, &ServerConfig::default(), &make_pics_config());

    for ot in [
        ObjectType::ANALOG_INPUT,
        ObjectType::ANALOG_OUTPUT,
        ObjectType::ANALOG_VALUE,
    ] {
        for pid in [
            PropertyIdentifier::LIMIT_ENABLE,
            PropertyIdentifier::NOTIFY_TYPE,
            PropertyIdentifier::TIME_DELAY,
            PropertyIdentifier::EVENT_ENABLE,
            PropertyIdentifier::HIGH_LIMIT,
            PropertyIdentifier::LOW_LIMIT,
            PropertyIdentifier::DEADBAND,
            PropertyIdentifier::NOTIFICATION_CLASS,
        ] {
            assert!(
                pics_writable(&pics, ot, pid),
                "{ot:?}: {pid:?} should be writable (accepted by write_generic_event_properties!)"
            );
        }
    }
}

/// Before #229 the four types below had no write path for the event set at all
/// — Time_Delay and Notify_Type were absent entirely — so PICS advertised what
/// no client could commission and no notification could ever be distributed.
#[test]
fn pics_event_properties_writable_on_binary_and_multistate_types() {
    let db = make_real_objects_db();
    let pics = generate_pics(&db, &ServerConfig::default(), &make_pics_config());

    for ot in [
        ObjectType::BINARY_INPUT,
        ObjectType::BINARY_VALUE,
        ObjectType::MULTI_STATE_INPUT,
        ObjectType::MULTI_STATE_VALUE,
    ] {
        for pid in [
            PropertyIdentifier::EVENT_ENABLE,
            PropertyIdentifier::NOTIFY_TYPE,
            PropertyIdentifier::TIME_DELAY,
            PropertyIdentifier::NOTIFICATION_CLASS,
        ] {
            assert!(
                pics_writable(&pics, ot, pid),
                "{ot:?}: {pid:?} should be writable (accepted by write_generic_event_properties!)"
            );
        }
        // Denied by the same macro, so PICS must not advertise it writable.
        assert!(
            !pics_writable(&pics, ot, PropertyIdentifier::ACKED_TRANSITIONS),
            "{ot:?}: ACKED_TRANSITIONS must stay read-only"
        );
    }
}

#[test]
fn pics_priority_array_read_only_on_commandable_types() {
    let db = make_real_objects_db();
    let pics = generate_pics(&db, &ServerConfig::default(), &make_pics_config());

    // Commandable types accept PRESENT_VALUE commands; PRIORITY_ARRAY is read-only.
    // RELINQUISH_DEFAULT grew a validated write arm
    // in #270 (the standard permits writability), so the PICS advertises it.
    for ot in [
        ObjectType::ANALOG_OUTPUT,
        ObjectType::ANALOG_VALUE,
        ObjectType::BINARY_OUTPUT,
        ObjectType::BINARY_VALUE,
        ObjectType::MULTI_STATE_OUTPUT,
        ObjectType::MULTI_STATE_VALUE,
    ] {
        assert!(
            !pics_writable(&pics, ot, PropertyIdentifier::PRIORITY_ARRAY),
            "{ot:?}: PRIORITY_ARRAY should be read-only"
        );
        assert!(
            pics_writable(&pics, ot, PropertyIdentifier::PRESENT_VALUE),
            "{ot:?}: PRESENT_VALUE should be writable"
        );
        assert!(
            pics_writable(&pics, ot, PropertyIdentifier::RELINQUISH_DEFAULT),
            "{ot:?}: RELINQUISH_DEFAULT should be writable (#270)"
        );
    }
}

#[test]
fn pics_input_present_value_writable_only_when_out_of_service() {
    let db = make_real_objects_db();
    let pics = generate_pics(&db, &ServerConfig::default(), &make_pics_config());

    // Input types (AI, BI, MSI) accept PRESENT_VALUE writes when
    // out-of-service. PICS reports the type-level writability (the runtime
    // enforces the out-of-service guard), so PRESENT_VALUE is writable.
    // This was a false-negative in the old heuristic for AI (AI was excluded);
    // the trait override now mirrors the real write_property arm.
    for ot in [
        ObjectType::ANALOG_INPUT,
        ObjectType::BINARY_INPUT,
        ObjectType::MULTI_STATE_INPUT,
    ] {
        assert!(
            pics_writable(&pics, ot, PropertyIdentifier::PRESENT_VALUE),
            "{ot:?}: PRESENT_VALUE should be writable (accepted when out-of-service)"
        );
    }
}

#[test]
fn pics_state_text_writable_on_multistate_types() {
    use PropertyIdentifier as P;
    let db = make_real_objects_db();
    let pics = generate_pics(&db, &ServerConfig::default(), &make_pics_config());

    // All three multistate types accept STATE_TEXT writes, by element or
    // whole. A whole write sets Number_Of_States (#1443), whose row stays
    // read-only, since no write naming it is taken.
    for ot in [
        ObjectType::MULTI_STATE_INPUT,
        ObjectType::MULTI_STATE_OUTPUT,
        ObjectType::MULTI_STATE_VALUE,
    ] {
        assert!(pics_writable(&pics, ot, P::STATE_TEXT), "{ot:?}");
        assert!(!pics_writable(&pics, ot, P::NUMBER_OF_STATES), "{ot:?}");
    }
}

#[test]
fn pics_fixed_readonly_properties_never_writable() {
    let db = make_real_objects_db();
    let pics = generate_pics(&db, &ServerConfig::default(), &make_pics_config());

    // The universal identifiers below are never writable. Number_Of_States
    // is likewise never written itself, though a whole State_Text write
    // resizes it (#1443).
    for ot in pics.supported_object_types.iter() {
        for pid in [
            PropertyIdentifier::OBJECT_IDENTIFIER,
            PropertyIdentifier::OBJECT_TYPE,
            PropertyIdentifier::PROPERTY_LIST,
            PropertyIdentifier::STATUS_FLAGS,
            PropertyIdentifier::NUMBER_OF_STATES,
        ] {
            if ot.supported_properties.iter().any(|p| p.property_id == pid) {
                assert!(
                    !pics_writable(&pics, ot.object_type, pid),
                    "{ot:?}: {pid:?} must never be writable"
                );
            }
        }
    }
}

#[test]
fn pics_createability_matches_runtime_factory() {
    let db = make_real_objects_db();
    let pics = generate_pics(&db, &ServerConfig::default(), &make_pics_config());

    // The 8 types handle_create_object actually constructs must be createable.
    for ot in [
        ObjectType::ANALOG_INPUT,
        ObjectType::ANALOG_OUTPUT,
        ObjectType::BINARY_INPUT,
        ObjectType::BINARY_OUTPUT,
        ObjectType::BINARY_VALUE,
        ObjectType::MULTI_STATE_INPUT,
        ObjectType::MULTI_STATE_OUTPUT,
        ObjectType::MULTI_STATE_VALUE,
    ] {
        let entry = pics
            .supported_object_types
            .iter()
            .find(|e| e.object_type == ot)
            .expect("type should be in PICS");
        assert!(
            entry.createable,
            "{ot:?} should be createable (factory constructs it)"
        );
    }

    // AnalogValue is NOT in the factory — PICS must not advertise createability.
    let av = pics
        .supported_object_types
        .iter()
        .find(|e| e.object_type == ObjectType::ANALOG_VALUE)
        .expect("ANALOG_VALUE should be in PICS");
    assert!(
        !av.createable,
        "AnalogValue must NOT be createable (factory rejects it with UNSUPPORTED_OBJECT_TYPE)"
    );
}

#[test]
fn pics_writability_matches_runtime_write_property() {
    // Cross-check: PICS reports LIMIT_ENABLE writable on AnalogInput AND
    // write_property actually accepts it. The old heuristic reported it
    // non-writable (false-negative); the trait override fixes both.
    use bacnet_objects::traits::BACnetObject;
    use bacnet_types::bitstring::LimitEnable;

    let mut ai = AnalogInputObject::new(1, "ai-1", NO_UNITS).unwrap();
    // PICS (via the trait method) must report it writable.
    assert!(
        ai.is_writable_property(PropertyIdentifier::LIMIT_ENABLE),
        "is_writable_property must report LIMIT_ENABLE writable on AnalogInput"
    );
    // And the runtime write_property must accept it.
    let bits = LimitEnable::all().to_bacnet();
    let result = ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![bits],
        },
        None,
    );
    assert!(
        result.is_ok(),
        "write_property must accept LIMIT_ENABLE, got: {result:?}"
    );

    // Cross-check a property PICS reports non-writable: STATUS_FLAGS.
    assert!(
        !ai.is_writable_property(PropertyIdentifier::STATUS_FLAGS),
        "STATUS_FLAGS must not be writable on AnalogInput"
    );
    let result = ai.write_property(
        PropertyIdentifier::STATUS_FLAGS,
        None,
        PropertyValue::Boolean(true),
        None,
    );
    assert!(
        result.is_err(),
        "write_property must reject STATUS_FLAGS, got: {result:?}"
    );
}

#[test]
fn executed_services_match_dispatch_table() {
    // The three-way truth chain for #192: dispatch arms (requests/mod.rs +
    // unconfirmed.rs choice consts) -> BACnetServicesSupported bits ->
    // device::EXECUTED_SERVICES, which feeds both the Device object's
    // Protocol_Services_Supported and the PICS executor column. If a dispatch
    // arm is added or removed without updating the choice const or the device
    // constant, this test fails.
    use bacnet_types::enums::{ServiceSupported, UnconfirmedServiceChoice};

    assert!(
        crate::server::EXECUTED_UNCONFIRMED.contains(&UnconfirmedServiceChoice::WRITE_GROUP),
        "inbound WriteGroup runs on the Channel objects"
    );

    let mut from_dispatch: Vec<u8> = crate::server::EXECUTED_CONFIRMED
        .iter()
        .map(|c| {
            ServiceSupported::from_confirmed_choice(*c)
                .expect("dispatched confirmed choice has a defined bit")
                .to_raw()
        })
        .chain(crate::server::EXECUTED_UNCONFIRMED.iter().map(|c| {
            ServiceSupported::from_unconfirmed_choice(*c)
                .expect("dispatched unconfirmed choice has a defined bit")
                .to_raw()
        }))
        .collect();
    from_dispatch.sort_unstable();

    let mut declared: Vec<u8> = bacnet_objects::device::EXECUTED_SERVICES
        .iter()
        .map(|s| s.to_raw())
        .collect();
    declared.sort_unstable();

    assert_eq!(
        declared, from_dispatch,
        "device::EXECUTED_SERVICES must equal the dispatch table's executed set"
    );
}