bacnet-server 0.15.0

BACnet server: APDU dispatch and service handlers
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
483
484
//! Same-state transition actions (#166; ASHRAE 135-2020 Clause 13.2.2.1.4).
//!
//! Every indicated transition runs the same actions, including transitions
//! with identical source and destination states such as OFFNORMAL→OFFNORMAL.
//! The pre-#166
//! evaluator dropped every evaluation whose result equaled the current state;
//! these tests pin the indication-driven replacement:
//!
//! - a genuine same-state indication (CHANGE_OF_STATE condition (c),
//!   CHANGE_OF_VALUE's only transition kind) *executes the actions*: the
//!   specific state is stored, the `Acked_Transitions` bit is maintained per
//!   the referenced Notification Class's `Ack_Required` (Clause 13.2.3), and
//!   the transition is emitted with its `Event_Enable`-scoped `distribute`;
//! - a *persisting* condition that satisfies no algorithm condition
//!   (OUT_OF_RANGE sitting in HIGH_LIMIT, COS sitting at the SAME alarm
//!   value) still emits nothing (Clause 13.3 requires a satisfied algorithm
//!   condition before indicating a transition).
//!
//! CHANGE_OF_VALUE's same-state coverage lives in `change_of_value.rs`.

use super::super::*;
use super::*;
use bacnet_objects::binary::BinaryInputObject;
use bacnet_objects::event_enrollment::EventEnrollmentObject;
use bacnet_objects::notification_class::NotificationClass;
use bacnet_objects::traits::BACnetObject;
use bacnet_types::bitstring::EventTransitionBits;
use bacnet_types::constructed::{
    BACnetDeviceObjectPropertyReference, BACnetEventParameter, BACnetPropertyStates,
};

/// CHANGE_OF_STATE fixture: a BinaryInput monitored against `alarm_values`,
/// with `time_delay` 0 unless overridden.
fn setup_cos(
    present_value: u32,
    alarm_values: &[u32],
    time_delay: u32,
) -> (ObjectDatabase, ObjectIdentifier, ObjectIdentifier) {
    let mut db = ObjectDatabase::new();

    let mut bi = BinaryInputObject::new(1, "BI-1").unwrap();
    bi.set_present_value(present_value);
    let bi_oid = bi.object_identifier();
    db.add(Box::new(bi)).unwrap();

    let mut ee = EventEnrollmentObject::new(3, "EE-COS", EventType::CHANGE_OF_STATE).unwrap();
    ee.set_object_property_reference(Some(BACnetDeviceObjectPropertyReference::new_local(
        bi_oid,
        PropertyIdentifier::PRESENT_VALUE.to_raw(),
    )))
    .unwrap();
    ee.set_event_parameters(BACnetEventParameter::ChangeOfState {
        time_delay,
        list_of_values: alarm_values
            .iter()
            .map(|v| BACnetPropertyStates::BinaryValue(*v))
            .collect(),
    });
    ee.set_event_enable(EventTransitionBits::all());
    let ee_oid = ee.object_identifier();
    db.add(Box::new(ee)).unwrap();

    (db, ee_oid, bi_oid)
}

/// Move the monitored binary value (input writes need Out_Of_Service, as in
/// the delays suite).
fn set_monitored(db: &mut ObjectDatabase, bi_oid: &ObjectIdentifier, value: u32) {
    let bi = db.get_mut(bi_oid).unwrap();
    bi.write_property(
        PropertyIdentifier::OUT_OF_SERVICE,
        None,
        PropertyValue::Boolean(true),
        None,
    )
    .unwrap();
    bi.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Enumerated(value),
        None,
    )
    .unwrap();
}

fn event_state(db: &ObjectDatabase, ee_oid: &ObjectIdentifier) -> EventState {
    match db
        .get(ee_oid)
        .unwrap()
        .read_property(PropertyIdentifier::EVENT_STATE, None)
        .unwrap()
    {
        PropertyValue::Enumerated(v) => EventState::from_raw(v),
        other => panic!("EVENT_STATE must read Enumerated, got {other:?}"),
    }
}

fn acked_transitions(db: &ObjectDatabase, ee_oid: &ObjectIdentifier) -> EventTransitionBits {
    match db
        .get(ee_oid)
        .unwrap()
        .read_property(PropertyIdentifier::ACKED_TRANSITIONS, None)
        .unwrap()
    {
        PropertyValue::BitString { data, .. } => EventTransitionBits::from_bacnet(&data),
        other => panic!("ACKED_TRANSITIONS must read BitString, got {other:?}"),
    }
}

/// Clause 13.3.2's optional condition (c) re-indicates OFFNORMAL for a match
/// in pAlarmValues that differs from the value at the last OFFNORMAL
/// indication. It is implemented so the Clause 13.2.2.1.4 actions execute for
/// the OFFNORMAL→OFFNORMAL same-state transition: the transition is emitted
/// and `Event_State` stores the specific state.
#[test]
fn cos_moving_between_alarm_values_reindicates_offnormal() {
    let (mut db, ee_oid, _bi_oid) = setup_cos(1, &[1, 0], 0);

    // Value 1 is an alarm value: NORMAL -> OFFNORMAL (condition (a)).
    let transitions = evaluate_event_enrollments(&mut db, 1);
    assert_eq!(transitions.len(), 1);
    assert_eq!(transitions[0].change.from, EventState::NORMAL);
    assert_eq!(transitions[0].change.to, EventState::OFFNORMAL);

    // Holding at the SAME alarm value satisfies no condition: silence.
    assert!(
        evaluate_event_enrollments(&mut db, 1).is_empty(),
        "value unchanged at the causing alarm value: no condition true, no transition"
    );

    // Moving to a DIFFERENT alarm value re-indicates OFFNORMAL (condition (c)).
    set_monitored(&mut db, &_bi_oid, 0);
    let transitions = evaluate_event_enrollments(&mut db, 1);
    assert_eq!(
        transitions.len(),
        1,
        "condition (c): OFFNORMAL -> OFFNORMAL must be emitted"
    );
    assert_eq!(transitions[0].change.from, EventState::OFFNORMAL);
    assert_eq!(transitions[0].change.to, EventState::OFFNORMAL);
    assert_eq!(transitions[0].event_type, EventType::CHANGE_OF_STATE);
    assert_eq!(
        event_state(&db, &ee_oid),
        EventState::OFFNORMAL,
        "the specific state is stored, unchanged in value"
    );

    // And the new causing value silences further passes until the value moves
    // again.
    assert!(
        evaluate_event_enrollments(&mut db, 1).is_empty(),
        "no re-fire while holding the NEW causing value"
    );
    set_monitored(&mut db, &_bi_oid, 1);
    assert_eq!(
        evaluate_event_enrollments(&mut db, 1).len(),
        1,
        "moving back re-indicates again (causing value tracked per fire, not per config)"
    );
}

/// Condition (c)'s pTimeDelay persistence requirement is honored too: with a nonzero delay the
/// same-state re-indication counts down like any offnormal indication, and a
/// flip back to the ORIGINAL alarm value mid-countdown re-seeds it (the
/// condition identity is the matched value).
#[test]
fn cos_same_state_reindication_is_delayed_and_value_discriminated() {
    let (mut db, _ee_oid, bi_oid) = setup_cos(1, &[1, 0], 3);
    for _ in 0..3 {
        assert!(evaluate_event_enrollments(&mut db, 1).is_empty());
    }
    assert_eq!(
        evaluate_event_enrollments(&mut db, 1)[0].change.to,
        EventState::OFFNORMAL,
        "condition (a) fires after Time_Delay"
    );

    // Move to the other alarm value: (c)'s countdown starts, does not fire
    // immediately.
    set_monitored(&mut db, &bi_oid, 0);
    for pass in 1..=2 {
        assert!(
            evaluate_event_enrollments(&mut db, 1).is_empty(),
            "(c) pass {pass}: pTimeDelay countdown must gate the same-state re-indication"
        );
    }
    // Flip back mid-countdown: re-seed (condition identity changed), still no
    // premature fire.
    set_monitored(&mut db, &bi_oid, 1);
    assert!(evaluate_event_enrollments(&mut db, 1).is_empty());
    set_monitored(&mut db, &bi_oid, 0);
    for pass in 1..=3 {
        assert!(
            evaluate_event_enrollments(&mut db, 1).is_empty(),
            "re-seeded (c) pass {pass}"
        );
    }
    let transitions = evaluate_event_enrollments(&mut db, 1);
    assert_eq!(transitions.len(), 1);
    assert_eq!(transitions[0].change.from, EventState::OFFNORMAL);
    assert_eq!(transitions[0].change.to, EventState::OFFNORMAL);
}

#[test]
fn cos_initial_delay_accepts_any_continuously_matched_alarm_value() {
    let (mut db, _ee_oid, bi_oid) = setup_cos(1, &[1, 0], 2);
    assert!(evaluate_event_enrollments(&mut db, 1).is_empty());

    set_monitored(&mut db, &bi_oid, 0);
    assert!(evaluate_event_enrollments(&mut db, 1).is_empty());
    assert_eq!(
        evaluate_event_enrollments(&mut db, 1)[0].change.to,
        EventState::OFFNORMAL
    );
}

/// OUT_OF_RANGE has no same-state condition (Clause 13.3.6 (a)–(h) are all
/// state-changing): sitting above the high limit emits exactly one
/// transition, then silence. This is the pin for the OTHER half of the
/// 13.2.2.1.4 fix — same-state actions run when the algorithm *indicates*,
/// never as a per-poll re-fire.
#[test]
fn oor_persisting_offnormal_emits_nothing() {
    let (mut db, ee_oid, _ai_oid) = setup_out_of_range(85.0, 80.0, 20.0, 2.0);
    assert_eq!(evaluate_event_enrollments(&mut db, 1).len(), 1);
    for pass in 1..=5 {
        assert!(
            evaluate_event_enrollments(&mut db, 1).is_empty(),
            "pass {pass}: persisting HIGH_LIMIT satisfies no condition \
             (Clause 13.3 introduction) — nothing may be emitted"
        );
    }
    assert_eq!(event_state(&db, &ee_oid), EventState::HIGH_LIMIT);
}

/// Clause 13.3.6 (d): HIGH_LIMIT → LOW_LIMIT across the band stores the
/// SPECIFIC returned state (13.2.2.1.4 forbids collapsing it to OFFNORMAL).
#[test]
fn oor_across_band_stores_the_specific_state() {
    let (mut db, ee_oid, ai_oid) = setup_out_of_range(85.0, 80.0, 20.0, 2.0);
    assert_eq!(evaluate_event_enrollments(&mut db, 1).len(), 1);

    let ai = db.get_mut(&ai_oid).unwrap();
    ai.write_property(
        PropertyIdentifier::OUT_OF_SERVICE,
        None,
        PropertyValue::Boolean(true),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(15.0),
        None,
    )
    .unwrap();

    let transitions = evaluate_event_enrollments(&mut db, 1);
    assert_eq!(transitions.len(), 1);
    assert_eq!(transitions[0].change.from, EventState::HIGH_LIMIT);
    assert_eq!(
        transitions[0].change.to,
        EventState::LOW_LIMIT,
        "the specific state LOW_LIMIT is stored and reported, not collapsed"
    );
    assert_eq!(event_state(&db, &ee_oid), EventState::LOW_LIMIT);
}

/// Clause 13.2.3 on a received transition: with the referenced Notification
/// Class requiring acknowledgment of TO_OFFNORMAL, the corresponding
/// `Acked_Transitions` bit is CLEARED (ack owed) when the transition fires —
/// on the same-state re-indication too, because all transitions run the
/// same actions.
#[test]
fn acked_transitions_bit_clears_when_notification_class_requires_ack() {
    let (mut db, ee_oid, bi_oid) = setup_cos(1, &[1, 0], 0);

    // Reference a Notification Class (instance 7) requiring TO_OFFNORMAL ack.
    let mut nc = NotificationClass::new(7, "NC-7").unwrap();
    nc.ack_required = EventTransitionBits::TO_OFFNORMAL;
    db.add(Box::new(nc)).unwrap();
    db.get_mut(&ee_oid)
        .unwrap()
        .write_property(
            PropertyIdentifier::NOTIFICATION_CLASS,
            None,
            PropertyValue::Unsigned(7),
            None,
        )
        .unwrap();

    assert_eq!(
        acked_transitions(&db, &ee_oid),
        EventTransitionBits::all(),
        "initial condition: no event of any type has ever occurred (Clause 12.12)"
    );

    // NORMAL -> OFFNORMAL with ack required: bit 0 clears.
    assert_eq!(evaluate_event_enrollments(&mut db, 1).len(), 1);
    assert_eq!(
        acked_transitions(&db, &ee_oid),
        EventTransitionBits::TO_FAULT | EventTransitionBits::TO_NORMAL,
        "TO_OFFNORMAL ack owed -> Acked_Transitions bit 0 cleared (13.2.3)"
    );

    // The OFFNORMAL -> OFFNORMAL re-indication is a fresh transition received:
    // the bit is cleared again (it starts cleared; the assertion that matters
    // is it does not SET).
    set_monitored(&mut db, &bi_oid, 0);
    assert_eq!(evaluate_event_enrollments(&mut db, 1).len(), 1);
    assert_eq!(
        acked_transitions(&db, &ee_oid),
        EventTransitionBits::TO_FAULT | EventTransitionBits::TO_NORMAL
    );
}

/// Clause 13.2.3 sets the bit when acknowledgment is not required. With no
/// Notification Class object resolvable, a fired transition leaves the bit at
/// the acknowledged state; with a class that requires nothing, the same — a
/// transition is never stranded unacknowledged for want of a class object.
#[test]
fn acked_transitions_bit_sets_when_no_ack_required() {
    let (mut db, ee_oid, _bi_oid) = setup_cos(1, &[1], 0);
    // No Notification Class object exists at all.
    assert_eq!(evaluate_event_enrollments(&mut db, 1).len(), 1);
    assert_eq!(
        acked_transitions(&db, &ee_oid),
        EventTransitionBits::all(),
        "no Ack_Required available -> the bit is set (13.2.3)"
    );

    // Same with a Notification Class referenced but requiring nothing: the
    // bit is set — from a CLEARED start this pass, so the set half is the
    // one proven to run (starting from 0b111 would leave the test blind to
    // a missing action, exactly the false-green pairing the sibling test
    // guards against).
    let nc = NotificationClass::new(3, "NC-3").unwrap();
    db.add(Box::new(nc)).unwrap();
    {
        let obj = db.get_mut(&ee_oid).unwrap();
        obj.write_property(
            PropertyIdentifier::NOTIFICATION_CLASS,
            None,
            PropertyValue::Unsigned(3),
            None,
        )
        .unwrap();
        // Internal channel, staging an already-owed TO_NORMAL ack.
        obj.set_acked_transitions_internal(EventTransitionBits::TO_NORMAL, false)
            .unwrap();
    }
    assert_eq!(
        acked_transitions(&db, &ee_oid),
        EventTransitionBits::TO_OFFNORMAL | EventTransitionBits::TO_FAULT
    );
    set_monitored(&mut db, &_bi_oid, 0);
    // Value 0 is not in the alarm list [1]: OFFNORMAL -> NORMAL, TO_NORMAL
    // is not ack-required, so its bit is SET by the transition.
    assert_eq!(evaluate_event_enrollments(&mut db, 1).len(), 1);
    assert_eq!(
        acked_transitions(&db, &ee_oid),
        EventTransitionBits::all(),
        "TO_NORMAL with ack not required -> bit 2 set by the transition action"
    );
}

/// A cleared `Event_Enable` bit suppresses only distribution — Clause 12.12
/// uses the property to control whether notifications are distributed,
/// never the same-state transition actions: the transition
/// is still emitted (with `distribute == false`) and `Event_State` stored.
#[test]
fn event_enable_suppresses_distribution_not_same_state_actions() {
    let (mut db, ee_oid, bi_oid) = setup_cos(1, &[1, 0], 0);
    db.get_mut(&ee_oid)
        .unwrap()
        .write_property(
            PropertyIdentifier::EVENT_ENABLE,
            None,
            PropertyValue::BitString {
                unused_bits: 5,
                data: vec![0x60], // wire bits: TO_NORMAL + TO_FAULT, NOT TO_OFFNORMAL
            },
            None,
        )
        .unwrap();

    let transitions = evaluate_event_enrollments(&mut db, 1);
    assert_eq!(transitions.len(), 1);
    assert!(
        !transitions[0].distribute,
        "TO_OFFNORMAL cleared: distribution suppressed, transition reported"
    );
    assert_eq!(event_state(&db, &ee_oid), EventState::OFFNORMAL);

    set_monitored(&mut db, &bi_oid, 0);
    let transitions = evaluate_event_enrollments(&mut db, 1);
    assert_eq!(
        transitions.len(),
        1,
        "condition (c)'s same-state transition is emitted even with distribution off"
    );
    assert!(!transitions[0].distribute);
    assert_eq!(transitions[0].change.from, EventState::OFFNORMAL);
    assert_eq!(transitions[0].change.to, EventState::OFFNORMAL);
}

/// The ack maintenace is direction-complete: a recovery (OFFNORMAL -> NORMAL)
/// whose Notification Class requires TO_NORMAL acknowledgment clears the
/// TO_NORMAL bit — the evaluator does not privilege the offnormal direction.
#[test]
fn acked_transitions_to_normal_clear_with_ack_required() {
    let (mut db, ee_oid, bi_oid) = setup_cos(1, &[1], 0);

    let mut nc = NotificationClass::new(9, "NC-9").unwrap();
    nc.ack_required = EventTransitionBits::TO_NORMAL;
    db.add(Box::new(nc)).unwrap();
    db.get_mut(&ee_oid)
        .unwrap()
        .write_property(
            PropertyIdentifier::NOTIFICATION_CLASS,
            None,
            PropertyValue::Unsigned(9),
            None,
        )
        .unwrap();

    // NORMAL -> OFFNORMAL (not ack-required): TO_OFFNORMAL bit stays set.
    assert_eq!(evaluate_event_enrollments(&mut db, 1).len(), 1);
    assert_eq!(acked_transitions(&db, &ee_oid), EventTransitionBits::all());

    // OFFNORMAL -> NORMAL with TO_NORMAL ack required: bit 2 clears.
    set_monitored(&mut db, &bi_oid, 0);
    assert_eq!(evaluate_event_enrollments(&mut db, 1).len(), 1);
    assert_eq!(
        acked_transitions(&db, &ee_oid),
        EventTransitionBits::TO_OFFNORMAL | EventTransitionBits::TO_FAULT,
        "TO_NORMAL ack owed -> bit 2 cleared (13.2.3 is direction-complete)"
    );
}

/// 13.2.3 gates the bit on Ack_Required, NEVER on Event_Enable (Clause
/// 12.12 scopes Event_Enable to external distribution): a transition with
/// distribution suppressed still clears its ack-owed bit.
#[test]
fn ack_bit_maintenance_is_independent_of_event_enable() {
    let (mut db, ee_oid, _bi_oid) = setup_cos(1, &[1], 0);

    let mut nc = NotificationClass::new(11, "NC-11").unwrap();
    nc.ack_required = EventTransitionBits::TO_OFFNORMAL;
    db.add(Box::new(nc)).unwrap();
    {
        let obj = db.get_mut(&ee_oid).unwrap();
        obj.write_property(
            PropertyIdentifier::NOTIFICATION_CLASS,
            None,
            PropertyValue::Unsigned(11),
            None,
        )
        .unwrap();
        obj.write_property(
            PropertyIdentifier::EVENT_ENABLE,
            None,
            PropertyValue::BitString {
                unused_bits: 5,
                data: vec![0x60], // TO_FAULT + TO_NORMAL only; TO_OFFNORMAL not distributed
            },
            None,
        )
        .unwrap();
    }

    let transitions = evaluate_event_enrollments(&mut db, 1);
    assert_eq!(transitions.len(), 1);
    assert!(
        !transitions[0].distribute,
        "TO_OFFNORMAL distribution is suppressed"
    );
    assert_eq!(
        acked_transitions(&db, &ee_oid),
        EventTransitionBits::TO_FAULT | EventTransitionBits::TO_NORMAL,
        "...while the ack-owed bit STILL clears — Event_Enable never scopes it"
    );
}