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
//! Access Point Authentication_Policy_List and Authentication_Policy_Names
//! (Clauses 12.31.10 to 12.31.13, Table 12-36; #1325): the application sets
//! them, the count follows, an invalid policy in effect drops the active
//! policy to zero and Reliability to CONFIGURATION_ERROR, and Reliability
//! takes simulated writes while out of service (Clause 12.31.8).

use bacnet_encoding::constructed::encode_authentication_policy;
use bacnet_types::constructed::{BACnetAuthenticationPolicy, BACnetAuthenticationPolicyEntry};
use bacnet_types::enums::{ErrorClass, ErrorCode, PropertyIdentifier as P};

use super::*;

const LIST: P = P::AUTHENTICATION_POLICY_LIST;
const NAMES: P = P::AUTHENTICATION_POLICY_NAMES;
const POLICY: P = P::ACTIVE_AUTHENTICATION_POLICY;
const POLICIES: P = P::NUMBER_OF_AUTHENTICATION_POLICIES;

fn read(point: &AccessPointObject, property: P) -> PropertyValue {
    point.read_property(property, None).unwrap()
}

fn write(point: &mut AccessPointObject, property: P, value: PropertyValue) -> Result<(), Error> {
    point.write_property(property, None, value, None)
}

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

fn oid(object_type: ObjectType, instance: u32) -> ObjectIdentifier {
    ObjectIdentifier::new(object_type, instance).unwrap()
}

/// An entry reading Credential Data Input `instance` here, at step `index`.
fn entry(instance: u32, index: u32) -> BACnetAuthenticationPolicyEntry {
    BACnetAuthenticationPolicyEntry {
        credential_data_input: oid(ObjectType::CREDENTIAL_DATA_INPUT, instance).into(),
        index,
    }
}

fn policy(entries: Vec<BACnetAuthenticationPolicyEntry>) -> BACnetAuthenticationPolicy {
    BACnetAuthenticationPolicy {
        policy: entries,
        order_enforced: true,
        timeout: 30,
    }
}

/// A card alone, then a card and a PIN.
fn card_and_pin() -> Vec<(&'static str, BACnetAuthenticationPolicy)> {
    vec![
        ("card", policy(vec![entry(1, 1)])),
        ("card and PIN", policy(vec![entry(1, 1), entry(2, 2)])),
    ]
}

fn encoded(policy: &BACnetAuthenticationPolicy) -> PropertyValue {
    let mut buf = BytesMut::new();
    encode_authentication_policy(&mut buf, policy);
    PropertyValue::ApplicationData(buf.to_vec())
}

fn reliability(point: &AccessPointObject) -> Reliability {
    let PropertyValue::Enumerated(raw) = read(point, P::RELIABILITY) else {
        panic!("Reliability is an Enumerated");
    };
    Reliability::from_raw(raw)
}

/// Whether Status_Flags carries FAULT.
fn fault(point: &AccessPointObject) -> bool {
    let PropertyValue::BitString { data, .. } = read(point, P::STATUS_FLAGS) else {
        panic!("Status_Flags is a BIT STRING");
    };
    data[0] & 0x40 != 0
}

#[test]
fn access_point_serves_the_policy_arrays_once_the_application_sets_them() {
    let mut point = AccessPointObject::new(1, "AP-1").unwrap();
    for property in [LIST, NAMES] {
        assert!(!point.property_list().contains(&property));
        assert!(point.read_property(property, None).is_err());
        assert!(point.is_array_property(property));
    }
    let policies = card_and_pin();
    point.set_authentication_policies(policies.clone()).unwrap();
    // Both arrays are as long as the count, which follows them (footnote 1).
    assert_eq!(read(&point, POLICIES), PropertyValue::Unsigned(2));
    assert_eq!(
        read(&point, LIST),
        PropertyValue::List(policies.iter().map(|(_, p)| encoded(p)).collect())
    );
    assert_eq!(
        read(&point, NAMES),
        PropertyValue::List(vec![
            PropertyValue::CharacterString("card".into()),
            PropertyValue::CharacterString("card and PIN".into()),
        ])
    );
    for property in [LIST, NAMES] {
        assert_eq!(
            point.read_property(property, Some(0)).unwrap(),
            PropertyValue::Unsigned(2)
        );
        assert!(matches!(
            point.read_property(property, Some(3)),
            Err(Error::Protocol { code, .. }) if code == ErrorCode::INVALID_ARRAY_INDEX.to_raw() as u32
        ));
    }
    assert_eq!(
        point.read_property(LIST, Some(2)).unwrap(),
        encoded(&policies[1].1)
    );
    assert_eq!(
        point.read_property(NAMES, Some(1)).unwrap(),
        PropertyValue::CharacterString("card".into())
    );
    // Optional rows before Property_List, read-only over the network.
    let list = point.property_list();
    assert_eq!(&list[list.len() - 2..], &[LIST, NAMES]);
    for property in [LIST, NAMES] {
        assert!(!point.required_properties().contains(&property));
        assert!(!point.is_writable_property(property));
    }
    // The first policy is valid and in effect.
    assert_eq!(read(&point, POLICY), PropertyValue::Unsigned(1));
    assert_eq!(reliability(&point), Reliability::NO_FAULT_DETECTED);
}

#[test]
fn access_point_policy_arrays_and_count_refuse_network_writes() {
    let mut point = AccessPointObject::new(1, "AP-1").unwrap();
    point.set_authentication_policies(card_and_pin()).unwrap();
    let before = [
        read(&point, LIST),
        read(&point, NAMES),
        read(&point, POLICIES),
    ];
    for (property, value) in [
        (LIST, read(&point, LIST)),
        (NAMES, read(&point, NAMES)),
        (POLICIES, PropertyValue::Unsigned(1)),
    ] {
        assert_error(
            write(&mut point, property, value),
            ErrorCode::WRITE_ACCESS_DENIED,
        );
    }
    assert_error(
        point.write_property(
            NAMES,
            Some(1),
            PropertyValue::CharacterString("x".into()),
            None,
        ),
        ErrorCode::WRITE_ACCESS_DENIED,
    );
    assert_eq!(
        [
            read(&point, LIST),
            read(&point, NAMES),
            read(&point, POLICIES)
        ],
        before
    );
    // No policies at all is no count (Clause 12.31.11).
    let none: Vec<(&str, BACnetAuthenticationPolicy)> = Vec::new();
    assert_error(
        point.set_authentication_policies(none),
        ErrorCode::VALUE_OUT_OF_RANGE,
    );
    assert_eq!(
        [
            read(&point, LIST),
            read(&point, NAMES),
            read(&point, POLICIES)
        ],
        before
    );
}

#[test]
fn access_point_invalid_policy_in_effect_drops_to_zero_and_faults() {
    let remote_device = BACnetDeviceObjectReference {
        device_identifier: Some(oid(ObjectType::DEVICE, 9)),
        object_identifier: oid(ObjectType::CREDENTIAL_DATA_INPUT, 3),
    };
    let not_a_device = BACnetDeviceObjectReference {
        device_identifier: Some(oid(ObjectType::ANALOG_VALUE, 9)),
        object_identifier: oid(ObjectType::CREDENTIAL_DATA_INPUT, 3),
    };
    let malformed = [
        // No entries (Clause 12.31.12).
        vec![],
        // An entry naming another object type.
        vec![BACnetAuthenticationPolicyEntry {
            credential_data_input: oid(ObjectType::ACCESS_POINT, 1).into(),
            index: 1,
        }],
        // A device identifier that isn't a Device.
        vec![BACnetAuthenticationPolicyEntry {
            credential_data_input: not_a_device,
            index: 1,
        }],
        // Indexes that don't start at 1, skip a step or go back.
        vec![entry(1, 0)],
        vec![entry(1, 2)],
        vec![entry(1, 1), entry(2, 3)],
        vec![entry(1, 1), entry(2, 2), entry(3, 1)],
    ];
    for entries in malformed {
        let mut point = AccessPointObject::new(1, "AP-1").unwrap();
        point
            .set_authentication_policies([
                ("bad", policy(entries.clone())),
                card_and_pin()[0].clone(),
            ])
            .unwrap();
        assert_eq!(
            read(&point, POLICY),
            PropertyValue::Unsigned(0),
            "{entries:?}"
        );
        assert_eq!(reliability(&point), Reliability::CONFIGURATION_ERROR);
        assert!(fault(&point));
        // Naming the invalid policy is refused; naming the valid one puts
        // it in effect (Clause 12.31.10), but the invalid policy in the list
        // keeps the configuration error (Clause 12.31.12).
        assert_error(
            write(&mut point, POLICY, PropertyValue::Unsigned(1)),
            ErrorCode::VALUE_OUT_OF_RANGE,
        );
        assert_eq!(read(&point, POLICY), PropertyValue::Unsigned(0));
        write(&mut point, POLICY, PropertyValue::Unsigned(2)).unwrap();
        assert_eq!(read(&point, POLICY), PropertyValue::Unsigned(2));
        assert_eq!(reliability(&point), Reliability::CONFIGURATION_ERROR);
        // A list with every policy valid clears it.
        point.set_authentication_policies(card_and_pin()).unwrap();
        assert_eq!(read(&point, POLICY), PropertyValue::Unsigned(2));
        assert_eq!(reliability(&point), Reliability::NO_FAULT_DETECTED);
        assert!(!fault(&point));
    }
    // Choices at one step, a remote reader and a policy with no timeout or
    // ordering are all well formed.
    for entries in [
        vec![
            entry(1, 1),
            entry(2, 1),
            entry(3, 2),
            entry(4, 2),
            entry(5, 3),
        ],
        vec![BACnetAuthenticationPolicyEntry {
            credential_data_input: remote_device,
            index: 1,
        }],
    ] {
        let mut point = AccessPointObject::new(1, "AP-1").unwrap();
        let unordered = BACnetAuthenticationPolicy {
            policy: entries.clone(),
            order_enforced: false,
            timeout: 0,
        };
        point
            .set_authentication_policies([("ok", unordered)])
            .unwrap();
        assert_eq!(
            read(&point, POLICY),
            PropertyValue::Unsigned(1),
            "{entries:?}"
        );
        assert_eq!(reliability(&point), Reliability::NO_FAULT_DETECTED);
    }
}

#[test]
fn access_point_policy_arrays_follow_the_count() {
    let mut point = AccessPointObject::new(1, "AP-1").unwrap();
    point.set_authentication_policies(card_and_pin()).unwrap();
    write(&mut point, POLICY, PropertyValue::Unsigned(2)).unwrap();
    // Growing adds an empty policy, unordered with no timeout (Clause
    // 12.31.12.2), and an empty name. An empty policy can't be put in
    // effect, and while the list holds one the point reports the
    // configuration error (Clause 12.31.12).
    point.set_number_of_authentication_policies(3).unwrap();
    assert_eq!(reliability(&point), Reliability::CONFIGURATION_ERROR);
    assert_eq!(
        point.read_property(LIST, Some(3)).unwrap(),
        encoded(&BACnetAuthenticationPolicy::default())
    );
    assert_eq!(
        point.read_property(NAMES, Some(3)).unwrap(),
        PropertyValue::CharacterString(String::new())
    );
    for property in [LIST, NAMES] {
        assert_eq!(
            point.read_property(property, Some(0)).unwrap(),
            PropertyValue::Unsigned(3)
        );
    }
    assert_eq!(read(&point, POLICY), PropertyValue::Unsigned(2));
    assert_error(
        write(&mut point, POLICY, PropertyValue::Unsigned(3)),
        ErrorCode::VALUE_OUT_OF_RANGE,
    );
    // Shrinking below the policy in effect drops it and its elements.
    point.set_number_of_authentication_policies(1).unwrap();
    assert_eq!(
        read(&point, NAMES),
        PropertyValue::List(vec![PropertyValue::CharacterString("card".into())])
    );
    assert_eq!(read(&point, POLICY), PropertyValue::Unsigned(0));
    assert_eq!(reliability(&point), Reliability::CONFIGURATION_ERROR);
    // A new list replaces the old; a usable policy 1 doesn't put itself in
    // effect, so a client picks it.
    point.set_authentication_policies(card_and_pin()).unwrap();
    assert_eq!(read(&point, POLICY), PropertyValue::Unsigned(0));
    write(&mut point, POLICY, PropertyValue::Unsigned(1)).unwrap();
    assert_eq!(reliability(&point), Reliability::NO_FAULT_DETECTED);
}

#[test]
fn access_point_reliability_takes_simulated_writes_only_out_of_service() {
    let mut point = AccessPointObject::new(1, "AP-1").unwrap();
    let simulated = PropertyValue::Enumerated(Reliability::UNRELIABLE_OTHER.to_raw());
    assert_error(
        write(&mut point, P::RELIABILITY, simulated.clone()),
        ErrorCode::WRITE_ACCESS_DENIED,
    );
    assert_eq!(reliability(&point), Reliability::NO_FAULT_DETECTED);

    write(&mut point, P::OUT_OF_SERVICE, PropertyValue::Boolean(true)).unwrap();
    write(&mut point, P::RELIABILITY, simulated).unwrap();
    assert_eq!(reliability(&point), Reliability::UNRELIABLE_OTHER);
    assert!(fault(&point));
    for (value, code) in [
        (PropertyValue::Unsigned(1), ErrorCode::INVALID_DATA_TYPE),
        (
            PropertyValue::Enumerated(70_000),
            ErrorCode::VALUE_OUT_OF_RANGE,
        ),
    ] {
        assert_error(write(&mut point, P::RELIABILITY, value), code);
    }
    assert_eq!(reliability(&point), Reliability::UNRELIABLE_OTHER);

    // Out of service Reliability is decoupled from the policies: an invalid
    // policy in effect still drops the active policy, but the simulation
    // stays served.
    point
        .set_authentication_policies([("empty", BACnetAuthenticationPolicy::default())])
        .unwrap();
    assert_eq!(read(&point, POLICY), PropertyValue::Unsigned(0));
    assert_eq!(reliability(&point), Reliability::UNRELIABLE_OTHER);
    write(
        &mut point,
        P::RELIABILITY,
        PropertyValue::Enumerated(Reliability::NO_FAULT_DETECTED.to_raw()),
    )
    .unwrap();
    assert!(!fault(&point));

    // The return to service serves the derived value again.
    write(&mut point, P::OUT_OF_SERVICE, PropertyValue::Boolean(false)).unwrap();
    assert_eq!(reliability(&point), Reliability::CONFIGURATION_ERROR);
    assert!(fault(&point));
    // Entering out of service again starts the simulation from it.
    write(&mut point, P::OUT_OF_SERVICE, PropertyValue::Boolean(true)).unwrap();
    assert_eq!(reliability(&point), Reliability::CONFIGURATION_ERROR);
}

#[test]
fn access_point_caps_the_policy_count_while_it_serves_the_arrays() {
    let names = |count: u32| (0..count).map(|n| (format!("p{n}"), policy(vec![entry(1, 1)])));
    let mut point = AccessPointObject::new(1, "AP-1").unwrap();
    // Without the arrays nothing is allocated, so any nonzero count goes.
    point
        .set_number_of_authentication_policies(u32::MAX)
        .unwrap();
    // A list past the cap is refused, and the point keeps what it had.
    assert_error(
        point.set_authentication_policies(names(MAX_AUTHENTICATION_POLICIES + 1)),
        ErrorCode::VALUE_OUT_OF_RANGE,
    );
    assert!(!point.property_list().contains(&LIST));
    point
        .set_authentication_policies(names(MAX_AUTHENTICATION_POLICIES))
        .unwrap();
    assert_eq!(
        read(&point, POLICIES),
        PropertyValue::Unsigned(MAX_AUTHENTICATION_POLICIES.into())
    );
    // Neither can a count grow the arrays past it.
    for count in [MAX_AUTHENTICATION_POLICIES + 1, u32::MAX] {
        assert_error(
            point.set_number_of_authentication_policies(count),
            ErrorCode::VALUE_OUT_OF_RANGE,
        );
    }
    assert_eq!(
        point.read_property(NAMES, Some(0)).unwrap(),
        PropertyValue::Unsigned(MAX_AUTHENTICATION_POLICIES.into())
    );
    point.set_number_of_authentication_policies(2).unwrap();
}