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
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
use super::{GlobalGroupObject, GroupObject, StructuredViewObject};
use std::borrow::Cow;

use bacnet_types::enums::PropertyIdentifier as P;

use crate::property_metadata::{
    PropertyConformance::{Optional, RequiredRead},
    PropertyMetadata,
    PropertyWriteCapability::{Always, ReadOnly},
};

// Canonical effective rows for the Group trio (ASHRAE 135-2020; PDF = printed + 2):
// - Group (type 11, §12.14 Table 12-17; printed pp. 243-244 / PDF pp. 245-246)
// - GlobalGroup (type 26, §12.50 Table 12-57; printed pp. 490-492 / PDF pp. 492-494)
// - StructuredView (type 29, §12.29 Table 12-34; printed p. 351 / PDF pp. 353-355)
// Order preserves each legacy projection; PROPERTY_LIST is appended so the
// projection helper omits it while required_properties keeps it. Only
// implemented rows are described: table rows the objects do not serve (Group
// audit/tag/profile rows; GlobalGroup intrinsic-reporting, COVU, audit, tag
// and profile rows; StructuredView subordinate tags/relationships rows) stay
// absent until dispatch exists.
// Object_Identifier, Object_Name, and Object_Type carry the table R code and
// have no network write route, so RequiredRead/ReadOnly. Object_Name
// explicitly documents the denial: a rename falls through to
// WRITE_ACCESS_DENIED. Description is Optional with a routed CharacterString
// write arm, so Optional/Always. List_Of_Group_Members, Group_Members,
// Present_Value, Group_Member_Names (Table 12-57 O), Node_Type, Node_Subtype
// (Table 12-34 O), Subordinate_List, and Subordinate_Annotations
// (Table 12-34 O) have no network write route — group values are populated
// locally, never commanded — so table-code conformance with ReadOnly; the R
// rows are RequiredRead and the three O rows are Optional.
// Status_Flags, Out_Of_Service, and Reliability are GlobalGroup rows only,
// served through the shared common read arms. Table codes map faithfully
// (DESCRIPTION O → Optional precedent): Table 12-57 lists Status_Flags R,
// Out_Of_Service R, and Reliability O, so GlobalGroup Reliability is
// Optional/ReadOnly, Status_Flags RequiredRead/ReadOnly and Out_Of_Service
// RequiredRead/Always with its routed Boolean write arm. Event_State and
// Member_Status_Flags (both R, #1092) are RequiredRead/ReadOnly: Event_State
// stays NORMAL because the object has no intrinsic reporting, and
// Member_Status_Flags is derived from the stored Present_Value. Group Table 12-17
// and StructuredView Table 12-34 carry none of the three, so those objects
// serve none of them (#1064 removed the rows the 0.1.0 import carried).
// Presence is None throughout: the implementation models no commandable,
// intrinsic-reporting, or paired-text gating on this family.
// The trio is not createable at runtime (the network factory builds only the
// eight analog/binary/multi-state input/output/value types, so the
// is_createable=false default holds) and remains deleteable (delete denies
// only Device and NetworkPort, so the is_deleteable=true default holds);
// neither needs an override. Array gating, writability, and COV also keep
// their defaults: Present_Value admits an index only on GlobalGroup
// (BACnetARRAY per Table 12-57) and rejects it on Group (BACnetLIST per
// Table 12-17); List_Of_Group_Members stays index-rejecting while
// Group_Members, Group_Member_Names, Subordinate_List, and
// Subordinate_Annotations admit one.
const GROUP_BASE: &[PropertyMetadata] = &[
    PropertyMetadata::new(P::OBJECT_IDENTIFIER, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::OBJECT_NAME, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::DESCRIPTION, Optional, None, Always),
    PropertyMetadata::new(P::OBJECT_TYPE, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::LIST_OF_GROUP_MEMBERS, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::PRESENT_VALUE, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::PROPERTY_LIST, RequiredRead, None, ReadOnly),
];

const GLOBAL_GROUP_BASE: &[PropertyMetadata] = &[
    PropertyMetadata::new(P::OBJECT_IDENTIFIER, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::OBJECT_NAME, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::DESCRIPTION, Optional, None, Always),
    PropertyMetadata::new(P::OBJECT_TYPE, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::GROUP_MEMBERS, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::PRESENT_VALUE, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::GROUP_MEMBER_NAMES, Optional, None, ReadOnly),
    PropertyMetadata::new(P::STATUS_FLAGS, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::EVENT_STATE, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::MEMBER_STATUS_FLAGS, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::OUT_OF_SERVICE, RequiredRead, None, Always),
    PropertyMetadata::new(P::RELIABILITY, Optional, None, ReadOnly),
    PropertyMetadata::new(P::PROPERTY_LIST, RequiredRead, None, ReadOnly),
];

const STRUCTURED_VIEW_BASE: &[PropertyMetadata] = &[
    PropertyMetadata::new(P::OBJECT_IDENTIFIER, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::OBJECT_NAME, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::DESCRIPTION, Optional, None, Always),
    PropertyMetadata::new(P::OBJECT_TYPE, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::NODE_TYPE, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::NODE_SUBTYPE, Optional, None, ReadOnly),
    PropertyMetadata::new(P::SUBORDINATE_LIST, RequiredRead, None, ReadOnly),
    PropertyMetadata::new(P::SUBORDINATE_ANNOTATIONS, Optional, None, ReadOnly),
    PropertyMetadata::new(P::PROPERTY_LIST, RequiredRead, None, ReadOnly),
];

pub(super) fn for_group_object(_object: &GroupObject) -> Cow<'_, [PropertyMetadata]> {
    Cow::Borrowed(GROUP_BASE)
}

pub(super) fn for_global_group_object(_object: &GlobalGroupObject) -> Cow<'_, [PropertyMetadata]> {
    Cow::Borrowed(GLOBAL_GROUP_BASE)
}

pub(super) fn for_structured_view_object(
    _object: &StructuredViewObject,
) -> Cow<'_, [PropertyMetadata]> {
    Cow::Borrowed(STRUCTURED_VIEW_BASE)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::traits::BACnetObject;
    use bacnet_types::enums::{ErrorClass, ErrorCode, ObjectType};
    use bacnet_types::error::Error;
    use bacnet_types::primitives::PropertyValue;
    use std::collections::HashSet;

    fn assert_error(error: Error, expected: ErrorCode) {
        assert!(
            matches!(error, Error::Protocol { class, code }
                if class == ErrorClass::PROPERTY.to_raw() as u32
                    && code == expected.to_raw() as u32),
            "expected {expected:?}, got {error:?}"
        );
    }

    fn assert_exact_sets(object: &dyn BACnetObject, all: &[P], required: &[P]) {
        let metadata = object.property_metadata();
        assert!(matches!(metadata, Cow::Borrowed(_)));
        assert_eq!(metadata.len(), all.len() + 1);
        assert_eq!(object.property_list().as_ref(), all);
        assert_eq!(object.required_properties().as_ref(), required);
        assert_eq!(
            metadata
                .iter()
                .map(|row| row.property_identifier)
                .collect::<HashSet<_>>()
                .len(),
            metadata.len()
        );
        assert!(!object.is_createable());
        assert!(object.is_deleteable());
        assert!(!object.supports_cov());
        for row in metadata.iter() {
            assert_eq!(row.presence_condition, None);
            let expected = if required.contains(&row.property_identifier) {
                RequiredRead
            } else {
                Optional
            };
            assert_eq!(row.conformance, expected, "{:?}", row.property_identifier);
            object.read_property(row.property_identifier, None).unwrap();
        }
    }

    fn assert_indexed_property_list(object: &dyn BACnetObject, all: &[P]) {
        let wire: Vec<_> = all
            .iter()
            .filter(|&&p| !matches!(p, P::OBJECT_IDENTIFIER | P::OBJECT_NAME | P::OBJECT_TYPE))
            .map(|p| PropertyValue::Enumerated(p.to_raw()))
            .collect();
        assert!(object.is_array_property(P::PROPERTY_LIST));
        assert_eq!(
            object.read_property(P::PROPERTY_LIST, None).unwrap(),
            PropertyValue::List(wire.clone())
        );
        assert_eq!(
            object.read_property(P::PROPERTY_LIST, Some(0)).unwrap(),
            PropertyValue::Unsigned(wire.len() as u64)
        );
        for (index, value) in wire.iter().enumerate() {
            assert_eq!(
                object
                    .read_property(P::PROPERTY_LIST, Some(index as u32 + 1))
                    .unwrap(),
                *value
            );
        }
        for index in [wire.len() as u32 + 1, u32::MAX] {
            assert_error(
                object
                    .read_property(P::PROPERTY_LIST, Some(index))
                    .unwrap_err(),
                ErrorCode::INVALID_ARRAY_INDEX,
            );
        }
    }

    #[test]
    fn property_metadata_group_exact_sets_readable_rows_and_indexed_list() {
        let object = GroupObject::new(1, "G-1").unwrap();
        let all = [
            P::OBJECT_IDENTIFIER,
            P::OBJECT_NAME,
            P::DESCRIPTION,
            P::OBJECT_TYPE,
            P::LIST_OF_GROUP_MEMBERS,
            P::PRESENT_VALUE,
        ];
        let required = [
            P::OBJECT_IDENTIFIER,
            P::OBJECT_NAME,
            P::OBJECT_TYPE,
            P::LIST_OF_GROUP_MEMBERS,
            P::PRESENT_VALUE,
            P::PROPERTY_LIST,
        ];
        assert_exact_sets(&object, &all, &required);
        assert_indexed_property_list(&object, &all);
        // Default member stores read back empty.
        assert_eq!(
            object
                .read_property(P::LIST_OF_GROUP_MEMBERS, None)
                .unwrap(),
            PropertyValue::List(vec![])
        );
        assert_eq!(
            object.read_property(P::PRESENT_VALUE, None).unwrap(),
            PropertyValue::List(vec![])
        );
        // Group Present_Value is a BACnetLIST (Table 12-17), so an index is
        // rejected; GlobalGroup admits one (Table 12-57 BACnetARRAY).
        assert!(!object.is_array_property(P::PRESENT_VALUE));
        assert!(!object.is_array_property(P::LIST_OF_GROUP_MEMBERS));
    }

    #[test]
    fn property_metadata_global_group_exact_sets_readable_rows_and_indexed_list() {
        let object = GlobalGroupObject::new(1, "GG-1").unwrap();
        let all = [
            P::OBJECT_IDENTIFIER,
            P::OBJECT_NAME,
            P::DESCRIPTION,
            P::OBJECT_TYPE,
            P::GROUP_MEMBERS,
            P::PRESENT_VALUE,
            P::GROUP_MEMBER_NAMES,
            P::STATUS_FLAGS,
            P::EVENT_STATE,
            P::MEMBER_STATUS_FLAGS,
            P::OUT_OF_SERVICE,
            P::RELIABILITY,
        ];
        let required = [
            P::OBJECT_IDENTIFIER,
            P::OBJECT_NAME,
            P::OBJECT_TYPE,
            P::GROUP_MEMBERS,
            P::PRESENT_VALUE,
            P::STATUS_FLAGS,
            P::EVENT_STATE,
            P::MEMBER_STATUS_FLAGS,
            P::OUT_OF_SERVICE,
            P::PROPERTY_LIST,
        ];
        assert_exact_sets(&object, &all, &required);
        assert_indexed_property_list(&object, &all);
        // Default member stores read back empty.
        assert_eq!(
            object.read_property(P::GROUP_MEMBERS, None).unwrap(),
            PropertyValue::List(vec![])
        );
        assert_eq!(
            object.read_property(P::PRESENT_VALUE, None).unwrap(),
            PropertyValue::List(vec![])
        );
        assert_eq!(
            object.read_property(P::GROUP_MEMBER_NAMES, None).unwrap(),
            PropertyValue::List(vec![])
        );
        // GlobalGroup collections are BACnetARRAY (Table 12-57), including
        // Present_Value, which is scalar elsewhere.
        assert!(object.is_array_property(P::GROUP_MEMBERS));
        assert!(object.is_array_property(P::GROUP_MEMBER_NAMES));
        assert!(object.is_array_property(P::PRESENT_VALUE));
    }

    #[test]
    fn property_metadata_structured_view_exact_sets_readable_rows_and_indexed_list() {
        let object = StructuredViewObject::new(1, "SV-1").unwrap();
        let all = [
            P::OBJECT_IDENTIFIER,
            P::OBJECT_NAME,
            P::DESCRIPTION,
            P::OBJECT_TYPE,
            P::NODE_TYPE,
            P::NODE_SUBTYPE,
            P::SUBORDINATE_LIST,
            P::SUBORDINATE_ANNOTATIONS,
        ];
        let required = [
            P::OBJECT_IDENTIFIER,
            P::OBJECT_NAME,
            P::OBJECT_TYPE,
            P::NODE_TYPE,
            P::SUBORDINATE_LIST,
            P::PROPERTY_LIST,
        ];
        assert_exact_sets(&object, &all, &required);
        assert_indexed_property_list(&object, &all);
        // Default node/subordinate stores.
        assert_eq!(
            object.read_property(P::NODE_TYPE, None).unwrap(),
            PropertyValue::Enumerated(0)
        );
        assert_eq!(
            object.read_property(P::NODE_SUBTYPE, None).unwrap(),
            PropertyValue::CharacterString(String::new())
        );
        assert_eq!(
            object.read_property(P::SUBORDINATE_LIST, None).unwrap(),
            PropertyValue::List(vec![])
        );
        assert_eq!(
            object
                .read_property(P::SUBORDINATE_ANNOTATIONS, None)
                .unwrap(),
            PropertyValue::List(vec![])
        );
        // StructuredView subordinate collections are BACnetARRAY (Table 12-34).
        assert!(object.is_array_property(P::SUBORDINATE_LIST));
        assert!(object.is_array_property(P::SUBORDINATE_ANNOTATIONS));
    }

    #[test]
    fn property_metadata_group_trio_write_capabilities_match_dispatch() {
        let fresh: [fn() -> Box<dyn BACnetObject>; 3] = [
            || Box::new(GroupObject::new(1, "G-1").unwrap()),
            || Box::new(GlobalGroupObject::new(1, "GG-1").unwrap()),
            || Box::new(StructuredViewObject::new(1, "SV-1").unwrap()),
        ];
        for make in fresh {
            for out_of_service in [false, true] {
                let mut object = make();
                // Only GlobalGroup has Out_Of_Service (Table 12-57); Tables
                // 12-17 and 12-34 have none (#1064).
                let has_out_of_service =
                    object.object_identifier().object_type() == ObjectType::GLOBAL_GROUP;
                if has_out_of_service {
                    object
                        .write_property(
                            P::OUT_OF_SERVICE,
                            None,
                            PropertyValue::Boolean(out_of_service),
                            None,
                        )
                        .unwrap();
                }
                let original = object.property_metadata().into_owned();
                for row in &original {
                    let p = row.property_identifier;
                    let capability = match p {
                        P::DESCRIPTION | P::OUT_OF_SERVICE => Always,
                        _ => ReadOnly,
                    };
                    assert_eq!(row.write_capability, capability, "{p:?}");
                    assert_eq!(
                        object.is_writable_property(p),
                        capability.is_writable(),
                        "{p:?}"
                    );
                    let value = object.read_property(p, None).unwrap();
                    let result = object.write_property(p, None, value, None);
                    if capability.is_writable() {
                        result.unwrap();
                    } else {
                        assert_error(result.unwrap_err(), ErrorCode::WRITE_ACCESS_DENIED);
                    }
                }
                // Object_Name has no network write route: a rename falls
                // through to WRITE_ACCESS_DENIED even with a well-formed value.
                assert!(!object.is_writable_property(P::OBJECT_NAME));
                assert_error(
                    object
                        .write_property(
                            P::OBJECT_NAME,
                            None,
                            PropertyValue::CharacterString("renamed".into()),
                            None,
                        )
                        .unwrap_err(),
                    ErrorCode::WRITE_ACCESS_DENIED,
                );
                // Present_Value, member, and subordinate stores are populated
                // locally, never commanded: even their read-back values are
                // denied on write.
                for p in [
                    P::PRESENT_VALUE,
                    P::LIST_OF_GROUP_MEMBERS,
                    P::GROUP_MEMBERS,
                    P::GROUP_MEMBER_NAMES,
                    P::NODE_TYPE,
                    P::NODE_SUBTYPE,
                    P::SUBORDINATE_LIST,
                    P::SUBORDINATE_ANNOTATIONS,
                ] {
                    if object.read_property(p, None).is_ok() {
                        let value = object.read_property(p, None).unwrap();
                        assert_error(
                            object.write_property(p, None, value, None).unwrap_err(),
                            ErrorCode::WRITE_ACCESS_DENIED,
                        );
                        assert!(!object.is_writable_property(p));
                    }
                }
                // Description and Out_Of_Service reject mistyped values
                // without changing state; where the table has no
                // Out_Of_Service, there is no property to write.
                for (p, value) in [
                    (P::DESCRIPTION, PropertyValue::Unsigned(1)),
                    (P::OUT_OF_SERVICE, PropertyValue::Unsigned(1)),
                ] {
                    let expected = if p == P::OUT_OF_SERVICE && !has_out_of_service {
                        ErrorCode::UNKNOWN_PROPERTY
                    } else {
                        ErrorCode::INVALID_DATA_TYPE
                    };
                    assert_error(
                        object.write_property(p, None, value, None).unwrap_err(),
                        expected,
                    );
                }
                assert_eq!(object.property_metadata().as_ref(), original);
            }
        }
    }

    #[test]
    fn property_metadata_group_trio_unserved_rows_stay_unknown() {
        fn assert_unserved(object: &mut dyn BACnetObject, p: P) {
            assert!(!object.is_writable_property(p));
            assert_error(
                object.read_property(p, None).unwrap_err(),
                ErrorCode::UNKNOWN_PROPERTY,
            );
            assert_error(
                object
                    .write_property(p, None, PropertyValue::Null, None)
                    .unwrap_err(),
                ErrorCode::UNKNOWN_PROPERTY,
            );
        }

        // Tables 12-17 and 12-34 have no Status_Flags, Reliability or
        // Out_Of_Service (#1064).
        let removed = [P::STATUS_FLAGS, P::RELIABILITY, P::OUT_OF_SERVICE];
        let mut group = GroupObject::new(1, "G-1").unwrap();
        assert_unserved(&mut group, P::PROFILE_NAME);
        for p in removed {
            assert_unserved(&mut group, p);
        }
        let mut global = GlobalGroupObject::new(1, "GG-1").unwrap();
        // The intrinsic-reporting and COVU rows stay absent: the object has
        // no event algorithm and sends no unsubscribed COV.
        assert_unserved(&mut global, P::EVENT_ENABLE);
        assert_unserved(&mut global, P::COVU_PERIOD);
        let mut view = StructuredViewObject::new(1, "SV-1").unwrap();
        assert_unserved(&mut view, P::SUBORDINATE_TAGS);
        for p in removed {
            assert_unserved(&mut view, p);
        }
    }
}