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
use crate::analog::AnalogValueObject;
use crate::binary::BinaryValueObject;
use crate::command_source::test_origin;
use crate::multistate::MultiStateValueObject;
use crate::present_value_access::PresentValueAccess;
use crate::property_metadata::PropertyWriteCapability;
use crate::traits::BACnetObject;
use bacnet_types::enums::{ErrorClass, ErrorCode, PropertyIdentifier as P};
use bacnet_types::error::Error;
use bacnet_types::primitives::PropertyValue;

const LOWER_PRIORITY: u8 = 8;
const HIGHER_PRIORITY: u8 = 4;

/// The rows present only under Commandable access.
const COMMANDABLE_ONLY: [P; 7] = [
    P::PRIORITY_ARRAY,
    P::RELINQUISH_DEFAULT,
    P::CURRENT_COMMAND_PRIORITY,
    P::VALUE_SOURCE,
    P::VALUE_SOURCE_ARRAY,
    P::LAST_COMMAND_TIME,
    P::AUDIT_PRIORITY_FILTER,
];

/// One Value object per family, built with `access`, and two different Present_Values each
/// accepts.
fn values(
    access: PresentValueAccess,
) -> [(Box<dyn BACnetObject>, PropertyValue, PropertyValue); 3] {
    [
        (
            Box::new(AnalogValueObject::with_access(1, "AV-1", 62, access).unwrap()),
            PropertyValue::Real(21.5),
            PropertyValue::Real(30.0),
        ),
        (
            Box::new(BinaryValueObject::with_access(1, "BV-1", access).unwrap()),
            PropertyValue::Enumerated(1),
            PropertyValue::Enumerated(0),
        ),
        (
            Box::new(MultiStateValueObject::with_access(1, "MSV-1", 3, access).unwrap()),
            PropertyValue::Unsigned(2),
            PropertyValue::Unsigned(3),
        ),
    ]
}

/// The same three objects built with `new`, and two different Present_Values each accepts.
fn commandable_values() -> [(Box<dyn BACnetObject>, PropertyValue, PropertyValue); 3] {
    [
        (
            Box::new(AnalogValueObject::new(1, "AV-1", 62).unwrap()),
            PropertyValue::Real(21.5),
            PropertyValue::Real(30.0),
        ),
        (
            Box::new(BinaryValueObject::new(1, "BV-1").unwrap()),
            PropertyValue::Enumerated(1),
            PropertyValue::Enumerated(0),
        ),
        (
            Box::new(MultiStateValueObject::new(1, "MSV-1", 3).unwrap()),
            PropertyValue::Unsigned(2),
            PropertyValue::Unsigned(3),
        ),
    ]
}

fn assert_protocol<T: std::fmt::Debug>(
    result: Result<T, Error>,
    class: ErrorClass,
    code: ErrorCode,
) {
    match result {
        Err(Error::Protocol {
            class: actual_class,
            code: actual_code,
        }) => {
            assert_eq!(actual_class, class.to_raw() as u32);
            assert_eq!(actual_code, code.to_raw() as u32);
        }
        other => panic!("expected {class:?} / {code:?}, got {other:?}"),
    }
}

/// A peer's WriteProperty to Present_Value at `priority`.
fn write_present_value(
    object: &mut dyn BACnetObject,
    value: PropertyValue,
    priority: u8,
) -> Result<(), Error> {
    object.write_property_from(
        P::PRESENT_VALUE,
        None,
        value,
        Some(priority),
        &test_origin(),
    )
}

fn take_out_of_service(object: &mut dyn BACnetObject) {
    object
        .write_property(P::OUT_OF_SERVICE, None, PropertyValue::Boolean(true), None)
        .unwrap();
}

fn present_value(object: &dyn BACnetObject) -> PropertyValue {
    object.read_property(P::PRESENT_VALUE, None).unwrap()
}

fn present_value_capability(object: &dyn BACnetObject) -> PropertyWriteCapability {
    object
        .property_metadata()
        .iter()
        .find(|row| row.property_identifier == P::PRESENT_VALUE)
        .expect("Present_Value is always present")
        .write_capability
}

/// The highest occupied priority is Present_Value, and with every priority
/// relinquished it is Relinquish_Default.
#[test]
fn new_is_commandable() {
    for (mut object, lower, higher) in commandable_values() {
        let relinquish_default = object.read_property(P::RELINQUISH_DEFAULT, None).unwrap();

        write_present_value(object.as_mut(), lower.clone(), LOWER_PRIORITY).unwrap();
        assert_eq!(present_value(object.as_ref()), lower);

        write_present_value(object.as_mut(), higher.clone(), HIGHER_PRIORITY).unwrap();
        assert_eq!(present_value(object.as_ref()), higher);

        write_present_value(object.as_mut(), lower.clone(), LOWER_PRIORITY).unwrap();
        assert_eq!(
            present_value(object.as_ref()),
            higher,
            "a lower priority must not override a higher one"
        );

        write_present_value(object.as_mut(), PropertyValue::Null, HIGHER_PRIORITY).unwrap();
        assert_eq!(present_value(object.as_ref()), lower);

        write_present_value(object.as_mut(), PropertyValue::Null, LOWER_PRIORITY).unwrap();
        assert_eq!(present_value(object.as_ref()), relinquish_default);
    }
}

#[test]
fn read_only_and_writable_have_no_command_prioritization() {
    for access in [PresentValueAccess::ReadOnly, PresentValueAccess::Writable] {
        for (mut object, _, _) in values(access) {
            for property in COMMANDABLE_ONLY {
                assert!(
                    !object.property_list().contains(&property),
                    "{access:?} lists {property:?}"
                );
                assert_protocol(
                    object.read_property(property, None),
                    ErrorClass::PROPERTY,
                    ErrorCode::UNKNOWN_PROPERTY,
                );
            }

            assert_protocol(
                object.write_property(P::RELINQUISH_DEFAULT, None, PropertyValue::Null, None),
                ErrorClass::PROPERTY,
                ErrorCode::UNKNOWN_PROPERTY,
            );
            assert_protocol(
                object.write_property_from(
                    P::VALUE_SOURCE,
                    None,
                    PropertyValue::Null,
                    Some(LOWER_PRIORITY),
                    &test_origin(),
                ),
                ErrorClass::PROPERTY,
                ErrorCode::UNKNOWN_PROPERTY,
            );
        }
    }
}

#[test]
fn read_only_refuses_peer_writes_while_in_service() {
    for (mut object, value, _) in values(PresentValueAccess::ReadOnly) {
        let before = present_value(object.as_ref());

        assert_protocol(
            write_present_value(object.as_mut(), value, LOWER_PRIORITY),
            ErrorClass::PROPERTY,
            ErrorCode::WRITE_ACCESS_DENIED,
        );
        assert_eq!(present_value(object.as_ref()), before);
        assert_eq!(
            present_value_capability(object.as_ref()),
            PropertyWriteCapability::WhenOutOfService
        );
    }
}

/// Out_Of_Service TRUE makes Present_Value writable for testing.
#[test]
fn read_only_accepts_peer_writes_while_out_of_service() {
    for (mut object, value, _) in values(PresentValueAccess::ReadOnly) {
        take_out_of_service(object.as_mut());

        write_present_value(object.as_mut(), value.clone(), LOWER_PRIORITY).unwrap();

        assert_eq!(present_value(object.as_ref()), value);
    }
}

/// Each write replaces Present_Value, whatever priority it carries.
#[test]
fn writable_takes_every_peer_write_directly() {
    for (mut object, first, second) in values(PresentValueAccess::Writable) {
        write_present_value(object.as_mut(), first, HIGHER_PRIORITY).unwrap();
        write_present_value(object.as_mut(), second.clone(), LOWER_PRIORITY).unwrap();

        assert_eq!(present_value(object.as_ref()), second);
        assert_eq!(
            present_value_capability(object.as_ref()),
            PropertyWriteCapability::Always
        );
    }
}

#[test]
fn the_application_sets_present_value_while_in_service() {
    for access in [PresentValueAccess::ReadOnly, PresentValueAccess::Writable] {
        for (mut object, value, _) in values(access) {
            object.set_present_value_internal(value.clone()).unwrap();

            assert_eq!(present_value(object.as_ref()), value, "{access:?}");
        }
    }
}

/// Out_Of_Service TRUE keeps software local to the device from changing Present_Value.
#[test]
fn the_application_is_refused_while_out_of_service() {
    for access in [PresentValueAccess::ReadOnly, PresentValueAccess::Writable] {
        for (mut object, value, _) in values(access) {
            take_out_of_service(object.as_mut());
            let before = present_value(object.as_ref());

            assert_protocol(
                object.set_present_value_internal(value),
                ErrorClass::PROPERTY,
                ErrorCode::WRITE_ACCESS_DENIED,
            );
            assert_eq!(present_value(object.as_ref()), before, "{access:?}");
        }
    }
}

#[test]
fn the_application_does_not_set_a_commandable_present_value() {
    for (mut object, value, _) in commandable_values() {
        assert_protocol(
            object.set_present_value_internal(value),
            ErrorClass::OBJECT,
            ErrorCode::OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED,
        );
    }
}

#[test]
fn a_value_of_the_wrong_kind_is_refused() {
    for access in [PresentValueAccess::ReadOnly, PresentValueAccess::Writable] {
        for (mut object, _, _) in values(access) {
            let before = present_value(object.as_ref());

            assert_protocol(
                object.set_present_value_internal(PropertyValue::Boolean(true)),
                ErrorClass::PROPERTY,
                ErrorCode::INVALID_DATA_TYPE,
            );
            assert_eq!(present_value(object.as_ref()), before, "{access:?}");
        }
    }
}

fn property_write(
    object: &mut dyn BACnetObject,
    sourced: bool,
    index: Option<u32>,
    value: PropertyValue,
    priority: Option<u8>,
) -> Result<(), Error> {
    if sourced {
        object.write_property_from(P::PRESENT_VALUE, index, value, priority, &test_origin())
    } else {
        object.write_property(P::PRESENT_VALUE, index, value, priority)
    }
}

#[test]
fn noncommandable_network_null_is_a_successful_noop() {
    for access in [PresentValueAccess::Writable, PresentValueAccess::ReadOnly] {
        for sourced in [true, false] {
            for (mut object, value, _) in values(access) {
                object.set_present_value_internal(value.clone()).unwrap();
                if access == PresentValueAccess::ReadOnly {
                    take_out_of_service(object.as_mut());
                }
                for priority in [None, Some(1), Some(6), Some(16)] {
                    property_write(
                        object.as_mut(),
                        sourced,
                        None,
                        PropertyValue::Null,
                        priority,
                    )
                    .unwrap();
                    assert_eq!(present_value(object.as_ref()), value);
                }
            }
        }
    }
}

#[test]
fn noncommandable_context_free_writes_match_sourced_writes() {
    for access in [PresentValueAccess::Writable, PresentValueAccess::ReadOnly] {
        for sourced in [false, true] {
            for (mut object, first, second) in values(access) {
                if access == PresentValueAccess::ReadOnly {
                    let before = present_value(object.as_ref());
                    for value in [first.clone(), PropertyValue::Null] {
                        assert_protocol(
                            property_write(object.as_mut(), sourced, None, value, None),
                            ErrorClass::PROPERTY,
                            ErrorCode::WRITE_ACCESS_DENIED,
                        );
                        assert_eq!(present_value(object.as_ref()), before);
                    }
                    take_out_of_service(object.as_mut());
                }
                for (value, priority) in [(first, None), (second, Some(6))] {
                    property_write(object.as_mut(), sourced, None, value.clone(), priority)
                        .unwrap();
                    assert_eq!(present_value(object.as_ref()), value);
                }
            }
        }
    }
}

#[test]
fn noncommandable_write_errors_preserve_value_and_internal_null_is_invalid() {
    for access in [PresentValueAccess::Writable, PresentValueAccess::ReadOnly] {
        for sourced in [false, true] {
            for ((mut object, value, _), invalid_range) in values(access).into_iter().zip([
                PropertyValue::Real(f32::NAN),
                PropertyValue::Enumerated(2),
                PropertyValue::Unsigned(4),
            ]) {
                object.set_present_value_internal(value.clone()).unwrap();
                assert_protocol(
                    object.set_present_value_internal(PropertyValue::Null),
                    ErrorClass::PROPERTY,
                    ErrorCode::INVALID_DATA_TYPE,
                );
                assert_eq!(present_value(object.as_ref()), value);
                if access == PresentValueAccess::ReadOnly {
                    take_out_of_service(object.as_mut());
                }
                for (index, invalid, code) in [
                    (
                        None,
                        PropertyValue::Boolean(true),
                        ErrorCode::INVALID_DATA_TYPE,
                    ),
                    (None, invalid_range, ErrorCode::VALUE_OUT_OF_RANGE),
                    (
                        Some(0),
                        PropertyValue::Null,
                        ErrorCode::PROPERTY_IS_NOT_AN_ARRAY,
                    ),
                    (Some(1), value.clone(), ErrorCode::PROPERTY_IS_NOT_AN_ARRAY),
                ] {
                    assert_protocol(
                        property_write(object.as_mut(), sourced, index, invalid, Some(6)),
                        ErrorClass::PROPERTY,
                        code,
                    );
                    assert_eq!(present_value(object.as_ref()), value);
                }
            }
        }
    }
}

#[test]
fn commandable_context_free_present_value_still_requires_origin() {
    for (mut object, value, _) in commandable_values() {
        let before = present_value(object.as_ref());
        for value in [value, PropertyValue::Null] {
            assert_protocol(
                object.write_property(P::PRESENT_VALUE, None, value, Some(8)),
                ErrorClass::PROPERTY,
                ErrorCode::WRITE_ACCESS_DENIED,
            );
            assert_eq!(present_value(object.as_ref()), before);
        }
    }
}