bacnet-objects 0.13.0

BACnet object model: traits, database, and standard object types
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
//! Workspace-wide property-metadata exactness audit: issue #261 close-out.
//!
//! This test proves that no supported object type uses the trait fallback
//! paths in `traits.rs` (empty `property_metadata` default, the universal-4
//! `required_properties` default, the heuristic `is_writable_property`
//! default). Every supported `ObjectType` gets one representative, and each
//! representative must expose non-empty canonical metadata from which the
//! GENERATED comparison surfaces (`required_properties`, `property_list`,
//! `is_writable_property`) are exactly derived. Any fallback regression or
//! required/optional drift fails this test.
//!
//! Deliberately out of scope (kept, not removed):
//! - The fallback defaults themselves stay for downstream custom objects.
//! - `NETWORK_SECURITY` (38) is deprecated (Clause 24 deleted) with no impl.
//! - `WRITE_GROUP` is a service family, not an object type, so it is not an
//!   `ObjectType` member at all.
//!
//! Event_Time_Stamps pinning (grounded in the local licensed
//! ASHRAE 135-2020 PDF; paraphrased, no verbatim text):
//! - The nine intrinsic-reporting input/output/value tables mark
//!   Event_Time_Stamps with an Optional base code plus the paired
//!   conditional footnotes (required when intrinsic reporting is supported;
//!   present only when it is supported): Tables 12-2 (Analog Input,
//!   printed p. 166), 12-3 (Analog Output, p. 172), 12-4 (Analog Value,
//!   p. 179), 12-6 (Binary Input, p. 190), 12-8 (Binary Output, p. 197),
//!   12-10 (Binary Value, p. 205), 12-21 (Multi-state Input, p. 268),
//!   12-22 (Multi-state Output, p. 275), 12-23 (Multi-state Value, p. 281).
//!   The `Optional` base code with `IntrinsicReportingRequired` below keeps
//!   the table code and records the footnote that makes the row required of
//!   these objects, which all report intrinsically, so RPM REQUIRED and the
//!   PICS list it (#1485).
//! - Table 12-14 (Event Enrollment, p. 232) and Table 12-61
//!   (Alert Enrollment, p. 504) mark Event_Time_Stamps unconditionally
//!   Required, which the `RequiredRead` rows below match.
//!
//! If the Standard ever reclassified these rows, the flip would change
//! PICS/RPM-REQUIRED output and needs an owner compatibility decision first;
//! do not "fix" a pin failure by editing the pins.

use std::collections::HashSet;
use std::sync::{Arc, Mutex};

use bacnet_types::constructed::{BACnetDeviceObjectReference, BACnetStageLimitValue};
use bacnet_types::enums::{EventType, ObjectType, PropertyIdentifier};
use bacnet_types::error::Error;
use bacnet_types::primitives::{ObjectIdentifier, PropertyValue};

use crate::access_control::{
    AccessCredentialObject, AccessDoorObject, AccessPointObject, AccessRightsObject,
    AccessUserObject, AccessZoneObject, CredentialDataInputObject,
};
use crate::accumulator::{AccumulatorObject, PulseConverterObject};
use crate::analog::{AnalogInputObject, AnalogOutputObject, AnalogValueObject};
use crate::audit::{AuditLogObject, AuditLogPersistence, AuditLogSnapshot, AuditReporterObject};
use crate::averaging::AveragingObject;
use crate::binary::{BinaryInputObject, BinaryOutputObject, BinaryValueObject};
use crate::channel::ChannelObject;
use crate::color::{ColorObject, ColorTemperatureObject};
use crate::command::CommandObject;
use crate::device::DeviceObject;
use crate::elevator::{ElevatorGroupObject, EscalatorObject, LiftObject};
use crate::event_enrollment::{AlertEnrollmentObject, EventEnrollmentObject};
use crate::event_log::EventLogObject;
use crate::file::FileObject;
use crate::group::{GlobalGroupObject, GroupObject, StructuredViewObject};
use crate::life_safety::{LifeSafetyPointObject, LifeSafetyZoneObject};
use crate::lighting::{BinaryLightingOutputObject, LightingOutputObject};
use crate::load_control::LoadControlObject;
use crate::loop_obj::LoopObject;
use crate::multistate::{MultiStateInputObject, MultiStateOutputObject, MultiStateValueObject};
use crate::network_port::NetworkPortObject;
use crate::notification_class::NotificationClass;
use crate::notification_forwarder::NotificationForwarderObject;
use crate::program::ProgramObject;
use crate::property_metadata::{
    required_properties_from_metadata, PropertyConformance, PropertyPresenceCondition,
};
use crate::schedule::{CalendarObject, ScheduleObject};
use crate::staging::{StagingConfig, StagingObject};
use crate::timer::TimerObject;
use crate::traits::BACnetObject;
use crate::trend::{TrendLogMultipleObject, TrendLogObject};
use crate::value_types::{
    BitStringValueObject, CharacterStringValueObject, DatePatternValueObject,
    DateTimePatternValueObject, DateTimeValueObject, DateValueObject, IntegerValueObject,
    LargeAnalogValueObject, OctetStringValueObject, PositiveIntegerValueObject,
    TimePatternValueObject, TimeValueObject,
};

#[derive(Default)]
struct MemoryAuditLogPersistence(Mutex<Option<AuditLogSnapshot>>);

impl AuditLogPersistence for MemoryAuditLogPersistence {
    fn load(&self, _expected_object: ObjectIdentifier) -> Result<Option<AuditLogSnapshot>, Error> {
        Ok(self.0.lock().unwrap().clone())
    }

    fn commit(&self, snapshot: &AuditLogSnapshot) -> Result<(), Error> {
        *self.0.lock().unwrap() = Some(snapshot.clone());
        Ok(())
    }
}

fn staging_config() -> StagingConfig {
    StagingConfig {
        present_value: 5.0,
        min_present_value: -1.0,
        units: 62,
        priority_for_writing: 8,
        stages: vec![
            BACnetStageLimitValue {
                limit: 10.0,
                values: vec![false, true],
                deadband: 1.0,
            },
            BACnetStageLimitValue {
                limit: 20.0,
                values: vec![true, false],
                deadband: 2.0,
            },
            BACnetStageLimitValue {
                limit: 30.0,
                values: vec![true, true],
                deadband: 1.0,
            },
        ],
        target_references: vec![
            BACnetDeviceObjectReference {
                device_identifier: None,
                object_identifier: ObjectIdentifier::new(ObjectType::BINARY_OUTPUT, 1).unwrap(),
            },
            BACnetDeviceObjectReference {
                device_identifier: None,
                object_identifier: ObjectIdentifier::new(ObjectType::BINARY_VALUE, 2).unwrap(),
            },
        ],
        stage_names: Some(vec!["Low".into(), "Middle".into(), "High".into()]),
    }
}

/// One representative per supported `ObjectType`: 64 total.
///
/// Excluded by design (see module docs): 38 NETWORK_SECURITY.
fn supported_representatives() -> Vec<Box<dyn BACnetObject>> {
    let alert_source = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap();
    vec![
        Box::new(AnalogInputObject::new(1, "AI-1", 62).unwrap()),
        Box::new(AnalogOutputObject::new(1, "AO-1", 62).unwrap()),
        Box::new(AnalogValueObject::new(1, "AV-1", 62).unwrap()),
        Box::new(BinaryInputObject::new(1, "BI-1").unwrap()),
        Box::new(BinaryOutputObject::new(1, "BO-1").unwrap()),
        Box::new(BinaryValueObject::new(1, "BV-1").unwrap()),
        Box::new(CalendarObject::new(1, "CAL-1").unwrap()),
        Box::new(CommandObject::new(1, "CMD-1").unwrap()),
        Box::new(DeviceObject::new(Default::default()).unwrap()),
        Box::new(EventEnrollmentObject::new(1, "EE-1", EventType::CHANGE_OF_BITSTRING).unwrap()),
        Box::new(FileObject::new(1, "FILE-1", "raw").unwrap()),
        Box::new(GroupObject::new(1, "GRP-1").unwrap()),
        Box::new(LoopObject::new(1, "LOOP-1", 62).unwrap()),
        Box::new(MultiStateInputObject::new(1, "MSI-1", 3).unwrap()),
        Box::new(MultiStateOutputObject::new(1, "MSO-1", 3).unwrap()),
        Box::new(NotificationClass::new(1, "NC-1").unwrap()),
        Box::new(ProgramObject::new(1, "PRG-1").unwrap()),
        Box::new(ScheduleObject::new(1, "SCH-1", PropertyValue::Null).unwrap()),
        Box::new(AveragingObject::new(1, "AVG-1").unwrap()),
        Box::new(MultiStateValueObject::new(1, "MSV-1", 3).unwrap()),
        Box::new(TrendLogObject::new(1, "TL-1", 3).unwrap()),
        Box::new(LifeSafetyPointObject::new(1, "LSP-1").unwrap()),
        Box::new(LifeSafetyZoneObject::new(1, "LSZ-1").unwrap()),
        Box::new(AccumulatorObject::new(1, "ACC-1", 95).unwrap()),
        Box::new(PulseConverterObject::new(1, "PC-1", 62).unwrap()),
        Box::new(EventLogObject::new(1, "EL-1", 3).unwrap()),
        Box::new(GlobalGroupObject::new(1, "GG-1").unwrap()),
        Box::new(TrendLogMultipleObject::new(1, "TLM-1", 3).unwrap()),
        Box::new(LoadControlObject::new(1, "LC-1").unwrap()),
        Box::new(StructuredViewObject::new(1, "SV-1").unwrap()),
        Box::new(AccessDoorObject::new(1, "DOOR-1").unwrap()),
        Box::new(TimerObject::new(1, "TMR-1").unwrap()),
        Box::new(AccessCredentialObject::new(1, "CRED-1").unwrap()),
        Box::new(AccessPointObject::new(1, "AP-1").unwrap()),
        Box::new(AccessRightsObject::new(1, "AR-1").unwrap()),
        Box::new(AccessUserObject::new(1, "USER-1").unwrap()),
        Box::new(AccessZoneObject::new(1, "ZONE-1").unwrap()),
        Box::new(CredentialDataInputObject::new(1, "CDI-1").unwrap()),
        // 38 NETWORK_SECURITY: deprecated, no impl — excluded.
        Box::new(BitStringValueObject::new(1, "BSV-1").unwrap()),
        Box::new(CharacterStringValueObject::new(1, "CSV-1").unwrap()),
        Box::new(DatePatternValueObject::new(1, "DPV-1").unwrap()),
        Box::new(DateValueObject::new(1, "DV-1").unwrap()),
        Box::new(DateTimePatternValueObject::new(1, "DTPV-1").unwrap()),
        Box::new(DateTimeValueObject::new(1, "DTV-1").unwrap()),
        Box::new(IntegerValueObject::new(1, "IV-1").unwrap()),
        Box::new(LargeAnalogValueObject::new(1, "LAV-1").unwrap()),
        Box::new(OctetStringValueObject::new(1, "OSV-1").unwrap()),
        Box::new(PositiveIntegerValueObject::new(1, "PIV-1").unwrap()),
        Box::new(TimePatternValueObject::new(1, "TPV-1").unwrap()),
        Box::new(TimeValueObject::new(1, "TV-1").unwrap()),
        Box::new(NotificationForwarderObject::new(1, "NF-1").unwrap()),
        Box::new(AlertEnrollmentObject::new(1, "AE-1", alert_source).unwrap()),
        Box::new(ChannelObject::new(1, "CH-1", 1).unwrap()),
        Box::new(LightingOutputObject::new(1, "LO-1").unwrap()),
        Box::new(BinaryLightingOutputObject::new(1, "BLO-1").unwrap()),
        Box::new(
            NetworkPortObject::new_non_bip(
                1,
                "NP-1",
                bacnet_types::enums::NetworkType::from_raw(0),
                0,
                Default::default(),
                1476,
            )
            .unwrap(),
        ),
        Box::new(ElevatorGroupObject::new(1, "EG-1").unwrap()),
        Box::new(EscalatorObject::new(1, "ESC-1").unwrap()),
        Box::new(LiftObject::new(1, "LIFT-1", 3).unwrap()),
        Box::new(StagingObject::new(1, "STG-1", staging_config()).unwrap()),
        Box::new(
            AuditLogObject::new(1, "AL-1", 3, Arc::new(MemoryAuditLogPersistence::default()))
                .unwrap(),
        ),
        Box::new(AuditReporterObject::new(1, "AR-1").unwrap()),
        Box::new(ColorObject::new(1, "CLR-1").unwrap()),
        Box::new(ColorTemperatureObject::new(1, "CT-1").unwrap()),
    ]
}

fn audit_object_type_coverage(objects: &[Box<dyn BACnetObject>]) {
    let mut raws: Vec<u32> = objects
        .iter()
        .map(|object| object.object_identifier().object_type().to_raw())
        .collect();
    raws.sort_unstable();
    let expected: Vec<u32> = (0..=64).filter(|raw| *raw != 38).collect();
    assert_eq!(
        raws, expected,
        "audit must cover exactly the 64 supported types"
    );
}

#[test]
fn audit_every_supported_type_derives_from_canonical_metadata() {
    let mut objects = supported_representatives();
    assert_eq!(objects.len(), 64, "one representative per supported type");
    audit_object_type_coverage(&objects);

    // Staging starts with Present_Stage uninitialized (reads
    // VALUE_NOT_INITIALIZED); the bundled server initializes it at startup.
    // Reach that steady state through the public Present_Value write route
    // before auditing row readability.
    for object in objects
        .iter_mut()
        .filter(|object| object.object_identifier().object_type() == ObjectType::STAGING)
    {
        object
            .write_property(
                PropertyIdentifier::PRESENT_VALUE,
                None,
                PropertyValue::Real(5.0),
                None,
            )
            .expect("staging representative must accept its startup initialization write");
    }

    for object in &objects {
        let object_type = object.object_identifier().object_type();
        let metadata = object.property_metadata();

        // No supported type may use the empty-metadata fallback.
        assert!(
            !metadata.is_empty(),
            "{object_type:?} must return non-empty canonical metadata"
        );
        assert!(
            metadata
                .iter()
                .any(|row| row.property_identifier == PropertyIdentifier::PROPERTY_LIST),
            "{object_type:?} metadata must contain PROPERTY_LIST"
        );
        let unique: HashSet<_> = metadata.iter().map(|row| row.property_identifier).collect();
        assert_eq!(
            unique.len(),
            metadata.len(),
            "{object_type:?} metadata identifiers must be unique"
        );

        // GENERATED comparison surface: required set derives from R/W rows.
        assert_eq!(
            object.required_properties().as_ref(),
            required_properties_from_metadata(metadata.as_ref()).as_ref(),
            "{object_type:?} required_properties must equal the metadata derivation"
        );

        // GENERATED comparison surface: legacy list is metadata minus PL.
        //
        // One documented exception: Device preserves its legacy projection,
        // which includes PROPERTY_LIST itself (`device/mod.rs`
        // `property_list`). The deviation is contained: PICS (`pics/mod.rs`)
        // and both RPM paths (`handlers/read_property.rs`,
        // `handlers/rpm_budget.rs`) expand migrated objects from metadata
        // directly, so the legacy list only serves direct `property_list()`
        // callers. Pin the deviation exactly here; canonicalizing it is a
        // production behavior change for a separate slice, not this audit.
        if object_type == ObjectType::DEVICE {
            let expected_with_pl: Vec<_> =
                metadata.iter().map(|row| row.property_identifier).collect();
            assert_eq!(
                object.property_list().as_ref(),
                expected_with_pl.as_slice(),
                "{object_type:?} property_list must keep its pinned legacy projection"
            );
        } else {
            let expected_list: Vec<_> = metadata
                .iter()
                .filter_map(|row| {
                    (row.property_identifier != PropertyIdentifier::PROPERTY_LIST)
                        .then_some(row.property_identifier)
                })
                .collect();
            assert_eq!(
                object.property_list().as_ref(),
                expected_list.as_slice(),
                "{object_type:?} property_list must equal metadata minus PROPERTY_LIST"
            );
        }

        // Every advertised row reads, and writability never drifts from dispatch.
        for row in metadata.iter() {
            assert!(
                crate::property_metadata_tests::metadata_row_reads(
                    object.as_ref(),
                    row.property_identifier
                ),
                "{object_type:?} must read {:?} without an array index",
                row.property_identifier
            );
            assert_eq!(
                object.is_writable_property(row.property_identifier),
                row.write_capability.is_writable(),
                "{object_type:?} is_writable must match {:?} write capability",
                row.property_identifier
            );
        }
    }
}

fn event_time_stamps_row(object: &dyn BACnetObject) -> crate::property_metadata::PropertyMetadata {
    *object
        .property_metadata()
        .iter()
        .find(|row| row.property_identifier == PropertyIdentifier::EVENT_TIME_STAMPS)
        .expect("intrinsic-reporting family must expose EVENT_TIME_STAMPS")
}

#[test]
fn audit_event_time_stamps_classification_matches_clause_12() {
    let objects = supported_representatives();
    audit_object_type_coverage(&objects);
    let by_type = |object_type: ObjectType| -> &dyn BACnetObject {
        objects
            .iter()
            .find(|object| object.object_identifier().object_type() == object_type)
            .map(AsRef::as_ref)
            .expect("supported type must have a representative")
    };

    // Tables 12-2/3/4, 12-6/8/10, 12-21/22/23: Optional base code with the
    // condition intrinsic reporting requires (see module docs for pages).
    for object_type in [
        ObjectType::ANALOG_INPUT,
        ObjectType::ANALOG_OUTPUT,
        ObjectType::ANALOG_VALUE,
        ObjectType::BINARY_INPUT,
        ObjectType::BINARY_OUTPUT,
        ObjectType::BINARY_VALUE,
        ObjectType::MULTI_STATE_INPUT,
        ObjectType::MULTI_STATE_OUTPUT,
        ObjectType::MULTI_STATE_VALUE,
    ] {
        let row = event_time_stamps_row(by_type(object_type));
        assert_eq!(
            row.conformance,
            PropertyConformance::Optional,
            "{object_type:?} EVENT_TIME_STAMPS must stay Optional"
        );
        assert_eq!(
            row.presence_condition,
            Some(PropertyPresenceCondition::IntrinsicReportingRequired),
            "{object_type:?} EVENT_TIME_STAMPS must stay intrinsic-reporting conditional"
        );
        assert!(
            row.is_required(),
            "{object_type:?} EVENT_TIME_STAMPS is required of an intrinsic reporter"
        );
    }

    // Tables 12-14 and 12-61: unconditionally Required.
    for object_type in [ObjectType::EVENT_ENROLLMENT, ObjectType::ALERT_ENROLLMENT] {
        let row = event_time_stamps_row(by_type(object_type));
        assert_eq!(
            row.conformance,
            PropertyConformance::RequiredRead,
            "{object_type:?} EVENT_TIME_STAMPS must stay Required"
        );
        assert_eq!(
            row.presence_condition, None,
            "{object_type:?} EVENT_TIME_STAMPS must stay unconditional"
        );
    }
}

mod unknown_writes;

mod collection_classification;