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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Credential Data Input Present_Value and Reliability writes while
//! Out_Of_Service is TRUE (Clauses 12.36.4, 12.36.7 and 12.36.8, Table 12-43
//! footnote 1, #1168).

use std::sync::Arc;

use bacnet_types::constructed::{BACnetAuthenticationFactor, BACnetAuthenticationFactorFormat};
use bacnet_types::enums::{AuthenticationFactorType as F, ErrorClass, ErrorCode};
use bacnet_types::enums::{PropertyIdentifier as P, Reliability};

use super::*;
use crate::clock::{ClockFrame, ClockReader};

/// A clock that always reads 2026-10-02 (a Friday) at `hour`:30.
pub(super) struct FixedClock(pub(super) u8);

impl ClockReader for FixedClock {
    fn read_clock(&self) -> Option<ClockFrame> {
        Some(ClockFrame {
            local_date: Date {
                year: 126,
                month: 10,
                day: 2,
                day_of_week: 5,
            },
            local_time: Time {
                hour: self.0,
                minute: 30,
                second: 0,
                hundredths: 0,
            },
            utc_offset: 0,
            daylight_savings_status: false,
        })
    }
}

/// Update_Time as the datetime choice the clock above stamps at `hour`.
pub(super) fn stamped(hour: u8) -> PropertyValue {
    PropertyValue::ApplicationData(vec![0x2E, 0xA4, 126, 10, 2, 5, 0xB4, hour, 30, 0, 0, 0x2F])
}

/// An update time in the sequence-number choice, context tag [1].
pub(super) fn sequence(number: u16) -> PropertyValue {
    let [high, low] = number.to_be_bytes();
    PropertyValue::ApplicationData(if high == 0 {
        vec![0x19, low]
    } else {
        vec![0x1A, high, low]
    })
}

/// A reader of Wiegand 26 cards (class 0) and of vendor 260's format 7
/// (class 3), whose last read was a Wiegand 26 card at 09:30.
pub(super) fn reader() -> CredentialDataInputObject {
    let mut reader = CredentialDataInputObject::new(1, "CDI-1").unwrap();
    reader
        .set_supported_formats([
            (BACnetAuthenticationFactorFormat::standard(F::WIEGAND26), 0),
            (BACnetAuthenticationFactorFormat::custom(260, 7), 3),
        ])
        .unwrap();
    reader
        .set_present_value(card(&[0x12, 0x34, 0x56]), stamp(9))
        .unwrap();
    reader.bind_clock_internal(Some(Arc::new(FixedClock(11))));
    reader
}

pub(super) fn card(value: &[u8]) -> BACnetAuthenticationFactor {
    BACnetAuthenticationFactor {
        format_type: F::WIEGAND26,
        format_class: 0,
        value: value.to_vec(),
    }
}

/// `hour`:30 on 2026-10-02.
pub(super) fn stamp(hour: u8) -> BACnetTimeStamp {
    BACnetTimeStamp::DateTime {
        date: Date {
            year: 126,
            month: 10,
            day: 2,
            day_of_week: 5,
        },
        time: Time {
            hour,
            minute: 30,
            second: 0,
            hundredths: 0,
        },
    }
}

/// A factor's bytes: format type `[0]`, format class `[1]`, value `[2]`.
pub(super) fn factor(format_type: u8, class: u8, value: &[u8]) -> Vec<u8> {
    let mut bytes = vec![0x09, format_type, 0x19, class, 0x28 | value.len() as u8];
    bytes.extend_from_slice(value);
    bytes
}

pub(super) fn data(bytes: Vec<u8>) -> PropertyValue {
    PropertyValue::ApplicationData(bytes)
}

pub(super) fn read(reader: &CredentialDataInputObject, property: P) -> PropertyValue {
    reader.read_property(property, None).unwrap()
}

pub(super) fn write(
    reader: &mut CredentialDataInputObject,
    property: P,
    value: PropertyValue,
) -> Result<(), Error> {
    reader.write_property(property, None, value, None)
}

pub(super) fn set_out_of_service(reader: &mut CredentialDataInputObject, out_of_service: bool) {
    write(
        reader,
        P::OUT_OF_SERVICE,
        PropertyValue::Boolean(out_of_service),
    )
    .unwrap();
}

/// Present_Value, Update_Time, Reliability and Status_Flags as served.
fn served(reader: &CredentialDataInputObject) -> [PropertyValue; 4] {
    [
        P::PRESENT_VALUE,
        P::UPDATE_TIME,
        P::RELIABILITY,
        P::STATUS_FLAGS,
    ]
    .map(|property| read(reader, property))
}

fn status_flags(fault: bool, out_of_service: bool) -> PropertyValue {
    let mut flags = StatusFlags::empty();
    flags.set(StatusFlags::FAULT, fault);
    flags.set(StatusFlags::OUT_OF_SERVICE, out_of_service);
    PropertyValue::BitString {
        unused_bits: 4,
        data: vec![flags.bits() << 4],
    }
}

pub(super) fn assert_property_error(result: Result<(), Error>, code: ErrorCode) {
    assert!(
        matches!(result, Err(Error::Protocol { class, code: actual })
            if class == ErrorClass::PROPERTY.to_raw() as u32
                && actual == code.to_raw() as u32),
        "expected PROPERTY / {code:?}, got {result:?}"
    );
}

/// The reader's own values: the Wiegand 26 card at 09:00, no fault.
fn device(out_of_service: bool) -> [PropertyValue; 4] {
    [
        data(factor(8, 0, &[0x12, 0x34, 0x56])),
        data(vec![0x2E, 0xA4, 126, 10, 2, 5, 0xB4, 9, 30, 0, 0, 0x2F]),
        PropertyValue::Enumerated(Reliability::NO_FAULT_DETECTED.to_raw()),
        status_flags(false, out_of_service),
    ]
}

#[test]
fn credential_data_input_refuses_simulated_rows_in_service() {
    let mut reader = reader();
    for (property, value) in [
        (P::PRESENT_VALUE, data(factor(8, 0, &[0x01]))),
        (
            P::RELIABILITY,
            PropertyValue::Enumerated(Reliability::UNRELIABLE_OTHER.to_raw()),
        ),
        // In service the refusal comes before any datatype check.
        (P::PRESENT_VALUE, PropertyValue::Real(1.0)),
        (P::RELIABILITY, PropertyValue::Null),
    ] {
        assert_property_error(
            write(&mut reader, property, value),
            ErrorCode::WRITE_ACCESS_DENIED,
        );
        assert_eq!(served(&reader), device(false), "{property:?}");
    }
    // Both rows have a write route, open only out of service.
    assert!(reader.is_writable_property(P::PRESENT_VALUE));
    assert!(reader.is_writable_property(P::RELIABILITY));
}

#[test]
fn credential_data_input_takes_simulated_rows_out_of_service() {
    let mut reader = reader();
    set_out_of_service(&mut reader, true);
    assert_eq!(served(&reader), device(true));

    // A vendor 260 factor, class 3: served, with Update_Time stamped from the
    // Device clock at 11:30.
    write(&mut reader, P::PRESENT_VALUE, data(factor(2, 3, &[0xAB]))).unwrap();
    assert_eq!(read(&reader, P::PRESENT_VALUE), data(factor(2, 3, &[0xAB])));
    assert_eq!(read(&reader, P::UPDATE_TIME), stamped(11));

    // The form the server hands over, one chunk per context tag, is the same
    // factor. The same card again is a new read and stamps again.
    reader.bind_clock_internal(Some(Arc::new(FixedClock(12))));
    for _ in 0..2 {
        write(
            &mut reader,
            P::PRESENT_VALUE,
            PropertyValue::List(vec![
                data(vec![0x09, 0x08]),
                data(vec![0x19, 0x00]),
                data(vec![0x2B, 0x12, 0x34, 0x56]),
            ]),
        )
        .unwrap();
        assert_eq!(
            read(&reader, P::PRESENT_VALUE),
            data(factor(8, 0, &[0x12, 0x34, 0x56]))
        );
        assert_eq!(read(&reader, P::UPDATE_TIME), stamped(12));
    }

    // UNDEFINED and ERROR, each with class 0, need no declared format.
    for format_type in [0, 1] {
        write(
            &mut reader,
            P::PRESENT_VALUE,
            data(factor(format_type, 0, &[])),
        )
        .unwrap();
        assert_eq!(
            read(&reader, P::PRESENT_VALUE),
            data(factor(format_type, 0, &[]))
        );
    }

    // A simulated fault sets the FAULT flag and leaves Update_Time alone; a
    // proprietary value is taken too.
    reader.bind_clock_internal(Some(Arc::new(FixedClock(13))));
    for reliability in [Reliability::UNRELIABLE_OTHER, Reliability::from_raw(64)] {
        write(
            &mut reader,
            P::RELIABILITY,
            PropertyValue::Enumerated(reliability.to_raw()),
        )
        .unwrap();
        assert_eq!(
            read(&reader, P::RELIABILITY),
            PropertyValue::Enumerated(reliability.to_raw())
        );
        assert_eq!(read(&reader, P::STATUS_FLAGS), status_flags(true, true));
        assert_eq!(read(&reader, P::UPDATE_TIME), stamped(12));
    }
}

#[test]
fn credential_data_input_without_a_clock_stamps_update_time_with_a_sequence_number() {
    let mut reader = reader();
    reader.bind_clock_internal(None);
    set_out_of_service(&mut reader, true);
    // Each simulated read takes the object's next number, from 1, so Update_Time
    // moves even for the same factor; a Reliability write takes none.
    for number in 1..=2 {
        write(&mut reader, P::PRESENT_VALUE, data(factor(8, 0, &[0x77]))).unwrap();
        assert_eq!(read(&reader, P::UPDATE_TIME), sequence(number));
    }
    write(
        &mut reader,
        P::RELIABILITY,
        PropertyValue::Enumerated(Reliability::UNRELIABLE_OTHER.to_raw()),
    )
    .unwrap();
    assert_eq!(read(&reader, P::UPDATE_TIME), sequence(2));

    // Dropping Wiegand 26 resets the factor served and then the reader's
    // factor set aside, each an update with the next number.
    reader
        .set_supported_formats([(BACnetAuthenticationFactorFormat::custom(260, 7), 3)])
        .unwrap();
    assert_eq!(read(&reader, P::UPDATE_TIME), sequence(3));
    set_out_of_service(&mut reader, false);
    assert_eq!(
        [
            read(&reader, P::PRESENT_VALUE),
            read(&reader, P::UPDATE_TIME)
        ],
        [data(factor(0, 0, &[])), sequence(4)]
    );

    // With a clock again, updates take its date and time.
    reader.bind_clock_internal(Some(Arc::new(FixedClock(12))));
    set_out_of_service(&mut reader, true);
    write(&mut reader, P::PRESENT_VALUE, data(factor(2, 3, &[0x01]))).unwrap();
    assert_eq!(read(&reader, P::UPDATE_TIME), stamped(12));
}

#[test]
fn sequence_numbers_climb_to_the_top_of_the_range_and_skip_zero() {
    assert_eq!(super::next_sequence(0), 1);
    assert_eq!(super::next_sequence(1), 2);
    assert_eq!(super::next_sequence(u16::MAX - 1), u16::MAX);
    assert_eq!(super::next_sequence(u16::MAX), 1);
}

#[test]
fn credential_data_input_simulated_rows_outside_their_datatypes_are_refused_unchanged() {
    const INVALID_DATA_TYPE: ErrorCode = ErrorCode::INVALID_DATA_TYPE;
    const VALUE_OUT_OF_RANGE: ErrorCode = ErrorCode::VALUE_OUT_OF_RANGE;
    let mut reader = reader();
    set_out_of_service(&mut reader, true);
    let mut wide_type = vec![0x0A, 0x01, 0x00];
    wide_type.extend_from_slice(&[0x19, 0x00, 0x28]);
    let mut trailing = factor(8, 0, &[0x01]);
    trailing.extend_from_slice(&[0x39, 0x00]);
    for (property, value, code) in [
        // Not one BACnetAuthenticationFactor.
        (
            P::PRESENT_VALUE,
            PropertyValue::Enumerated(8),
            INVALID_DATA_TYPE,
        ),
        (P::PRESENT_VALUE, PropertyValue::Null, INVALID_DATA_TYPE),
        (
            P::PRESENT_VALUE,
            data(vec![0x09, 0x08, 0x19, 0x00]),
            INVALID_DATA_TYPE,
        ),
        (
            P::PRESENT_VALUE,
            data(vec![0x19, 0x00, 0x09, 0x08, 0x28]),
            INVALID_DATA_TYPE,
        ),
        (P::PRESENT_VALUE, data(trailing), INVALID_DATA_TYPE),
        (
            P::PRESENT_VALUE,
            PropertyValue::List(vec![
                data(vec![0x09, 0x08]),
                PropertyValue::Unsigned(0),
                data(vec![0x28]),
            ]),
            INVALID_DATA_TYPE,
        ),
        // 25 and 256 lie past the closed production.
        (
            P::PRESENT_VALUE,
            data(factor(25, 0, &[])),
            VALUE_OUT_OF_RANGE,
        ),
        (P::PRESENT_VALUE, data(wide_type), VALUE_OUT_OF_RANGE),
        // A format the reader doesn't declare, and a declared one with
        // another class.
        (
            P::PRESENT_VALUE,
            data(factor(9, 0, &[0x01])),
            VALUE_OUT_OF_RANGE,
        ),
        (
            P::PRESENT_VALUE,
            data(factor(8, 3, &[0x01])),
            VALUE_OUT_OF_RANGE,
        ),
        (
            P::PRESENT_VALUE,
            data(factor(2, 0, &[0x01])),
            VALUE_OUT_OF_RANGE,
        ),
        // UNDEFINED and ERROR carry class 0.
        (
            P::PRESENT_VALUE,
            data(factor(0, 1, &[])),
            VALUE_OUT_OF_RANGE,
        ),
        (
            P::PRESENT_VALUE,
            data(factor(1, 2, &[])),
            VALUE_OUT_OF_RANGE,
        ),
        // 11 is reserved for ASHRAE, 65536 past the datatype.
        (
            P::RELIABILITY,
            PropertyValue::Enumerated(11),
            VALUE_OUT_OF_RANGE,
        ),
        (
            P::RELIABILITY,
            PropertyValue::Enumerated(65_536),
            VALUE_OUT_OF_RANGE,
        ),
        (
            P::RELIABILITY,
            PropertyValue::Unsigned(7),
            INVALID_DATA_TYPE,
        ),
        (P::RELIABILITY, PropertyValue::Null, INVALID_DATA_TYPE),
    ] {
        assert_property_error(write(&mut reader, property, value), code);
        assert_eq!(served(&reader), device(true), "{property:?} {code:?}");
    }
}

#[test]
fn credential_data_input_return_to_service_serves_the_reader_again() {
    let mut reader = reader();
    set_out_of_service(&mut reader, true);
    write(&mut reader, P::PRESENT_VALUE, data(factor(2, 3, &[0xAB]))).unwrap();
    write(
        &mut reader,
        P::RELIABILITY,
        PropertyValue::Enumerated(Reliability::UNRELIABLE_OTHER.to_raw()),
    )
    .unwrap();
    let simulated = served(&reader);

    // Out of service the application's reads go aside, and its Reliability
    // is refused as on the other Reliability carriers.
    reader.set_present_value(card(&[0x99]), stamp(10)).unwrap();
    assert_property_error(
        reader.set_reliability_internal(Reliability::COMMUNICATION_FAILURE),
        ErrorCode::WRITE_ACCESS_DENIED,
    );
    assert_eq!(served(&reader), simulated);

    // The return to service serves the reader's read and the Reliability
    // from before, dropping the simulation.
    set_out_of_service(&mut reader, false);
    assert_eq!(
        served(&reader),
        [
            data(factor(8, 0, &[0x99])),
            data(vec![0x2E, 0xA4, 126, 10, 2, 5, 0xB4, 10, 30, 0, 0, 0x2F]),
            PropertyValue::Enumerated(Reliability::NO_FAULT_DETECTED.to_raw()),
            status_flags(false, false),
        ]
    );

    // In service the application's values are served at once.
    reader
        .set_reliability_internal(Reliability::COMMUNICATION_FAILURE)
        .unwrap();
    assert_eq!(read(&reader, P::STATUS_FLAGS), status_flags(true, false));
    reader
        .set_reliability_internal(Reliability::NO_FAULT_DETECTED)
        .unwrap();
    reader
        .set_present_value(card(&[0x12, 0x34, 0x56]), stamp(9))
        .unwrap();
    assert_eq!(served(&reader), device(false));

    // A second period out of service starts from the reader's values.
    set_out_of_service(&mut reader, true);
    assert_eq!(served(&reader), device(true));
    set_out_of_service(&mut reader, false);
    assert_eq!(served(&reader), device(false));
}

#[test]
fn credential_data_input_null_out_of_service_write_keeps_the_simulation() {
    let mut reader = reader();
    set_out_of_service(&mut reader, true);
    write(&mut reader, P::PRESENT_VALUE, data(factor(1, 0, &[]))).unwrap();
    let simulated = served(&reader);
    write(&mut reader, P::OUT_OF_SERVICE, PropertyValue::Null).unwrap();
    assert_eq!(served(&reader), simulated);
    set_out_of_service(&mut reader, false);
    assert_eq!(served(&reader), device(false));
}

#[test]
fn credential_data_input_set_reliability_internal_checks_the_production() {
    let mut reader = reader();
    for raw in [11, 65_536] {
        let result = reader.set_reliability_internal(Reliability::from_raw(raw));
        assert_property_error(result, ErrorCode::VALUE_OUT_OF_RANGE);
    }
    assert_eq!(served(&reader), device(false));
    reader
        .set_reliability_internal(Reliability::from_raw(64))
        .unwrap();
    assert_eq!(read(&reader, P::RELIABILITY), PropertyValue::Enumerated(64));
}