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
485
486
487
488
489
490
//! Foreign-`Event_State` recovery (PR-#290 review blocker 2) and the
//! zero-padded CHANGE_OF_BITSTRING comparison width (review F2).
//!
//! Rewriting `Event_Parameters` to a DIFFERENT algorithm can leave the
//! enrollment holding a state the new algorithm's conditions never name
//! (13.3's per-algorithm `pCurrentState` letters): a HIGH_LIMIT left by
//! OUT_OF_RANGE, say, under new CHANGE_OF_STATE parameters whose conditions
//! only speak of NORMAL/OFFNORMAL. Keyed strictly, no condition ever
//! matches and the ghost state persists forever — a regression against the
//! stateless base evaluators, which computed unconditionally. Each arm now
//! recovers per [`ArmEvaluation`]'s documented rule: evaluate as from
//! NORMAL and indicate the computed state, through the ordinary actions
//! path INCLUDING the direction rule's delay gating.
//!
//! The monitored property's datatype must satisfy the new algorithm for a
//! recovery to be observable at all, so the fixtures monitor a property
//! every arm can read (`NOTIFICATION_CLASS`, an Unsigned readable by
//! extract_real and extract_enumerated alike; a second object's
//! `EVENT_ENABLE` bitstring for the COBS case).

use super::super::*;
use bacnet_objects::analog::AnalogInputObject;
use bacnet_objects::event_enrollment::EventEnrollmentObject;
use bacnet_objects::traits::BACnetObject;
use bacnet_types::constructed::{
    BACnetDeviceObjectPropertyReference, BACnetEventParameter, BACnetPropertyStates,
    ChangeOfValueCriteria,
};
use bacnet_types::enums::EngineeringUnits;

/// EE (instance 1) monitoring `AI-1.NOTIFICATION_CLASS` (an Unsigned), with
/// the given event type + parameters.
fn setup_on_notification_class(
    event_type: EventType,
    params: BACnetEventParameter,
    notification_class: u32,
) -> (ObjectDatabase, ObjectIdentifier) {
    let mut db = ObjectDatabase::new();

    let mut ai = AnalogInputObject::new(1, "AI-1", EngineeringUnits::DEGREES_CELSIUS).unwrap();
    ai.write_property(
        PropertyIdentifier::NOTIFICATION_CLASS,
        None,
        PropertyValue::Unsigned(notification_class as u64),
        None,
    )
    .unwrap();
    let ai_oid = ai.object_identifier();
    db.add(Box::new(ai)).unwrap();

    let mut ee = EventEnrollmentObject::new(1, "EE-1", event_type).unwrap();
    ee.set_object_property_reference(Some(BACnetDeviceObjectPropertyReference::new_local(
        ai_oid,
        PropertyIdentifier::NOTIFICATION_CLASS.to_raw(),
    )))
    .unwrap();
    ee.set_event_parameters(params);
    ee.set_event_enable(EventTransitionBits::all());
    let ee_oid = ee.object_identifier();
    db.add(Box::new(ee)).unwrap();
    (db, ee_oid)
}

/// Rewrite `Event_Parameters` framed, the network-faithful way.
fn rewrite_params(
    db: &mut ObjectDatabase,
    ee_oid: &ObjectIdentifier,
    params: BACnetEventParameter,
) {
    let mut scratch =
        EventEnrollmentObject::new(1, "scratch", EventType::CHANGE_OF_BITSTRING).unwrap();
    scratch.set_event_parameters(params);
    let reframed = scratch
        .read_property(PropertyIdentifier::EVENT_PARAMETERS, None)
        .unwrap();
    db.get_mut(ee_oid)
        .unwrap()
        .write_property(PropertyIdentifier::EVENT_PARAMETERS, None, reframed, 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:?}"),
    }
}

/// HIGH_LIMIT left by OUT_OF_RANGE under rewritten CHANGE_OF_STATE
/// parameters: the value is in NO alarm list, so the algorithm settles at
/// NORMAL — reached through the actions path (state stored), gated by the
/// normal-direction delay (here Time_Delay=2 via the absent-TDN fallback),
/// NOT by an unwedging dodge.
#[test]
fn foreign_high_limit_recovers_under_cos_params() {
    let (mut db, ee_oid) = setup_on_notification_class(
        EventType::OUT_OF_RANGE,
        BACnetEventParameter::OutOfRange {
            time_delay: 0,
            low_limit: 20.0,
            high_limit: 80.0,
            deadband: 2.0,
        },
        90, // unreadable as a bool — monitored as Unsigned(90) > 80 -> HIGH_LIMIT
    );
    assert_eq!(
        evaluate_event_enrollments(&mut db, 1)[0].change.to,
        EventState::HIGH_LIMIT,
        "OOR drives the enrollment to HIGH_LIMIT first"
    );

    rewrite_params(
        &mut db,
        &ee_oid,
        BACnetEventParameter::ChangeOfState {
            time_delay: 2,
            list_of_values: vec![BACnetPropertyStates::UnsignedValue(999)],
        },
    );
    // HIGH_LIMIT is outside COS's {NORMAL, OFFNORMAL}: recovery is
    // indicated, gated by pTimeDelayNormal(=TD fallback)=2.
    for pass in 1..=2 {
        assert!(
            evaluate_event_enrollments(&mut db, 1).is_empty(),
            "recovery pass {pass}: gated by the normal-direction delay"
        );
        assert_eq!(event_state(&db, &ee_oid), EventState::HIGH_LIMIT);
    }
    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::NORMAL);
    // Event_Type follows the rewritten parameters (Clause 12.12.5, #1868),
    // so the recovery is labeled CHANGE_OF_STATE.
    assert_eq!(transitions[0].event_type, EventType::CHANGE_OF_STATE);
    assert_eq!(event_state(&db, &ee_oid), EventState::NORMAL);

    // And the arm is alive afterwards from inside its own state space.
    assert!(evaluate_event_enrollments(&mut db, 1).is_empty());
}

/// OFFNORMAL left by CHANGE_OF_STATE under rewritten OUT_OF_RANGE
/// parameters, value in band: recovers to NORMAL immediately (TD=0).
#[test]
fn foreign_offnormal_recovers_under_oor_params() {
    let (mut db, ee_oid) = setup_on_notification_class(
        EventType::CHANGE_OF_STATE,
        BACnetEventParameter::ChangeOfState {
            time_delay: 0,
            list_of_values: vec![BACnetPropertyStates::UnsignedValue(1)],
        },
        1,
    );
    assert_eq!(
        evaluate_event_enrollments(&mut db, 1)[0].change.to,
        EventState::OFFNORMAL
    );

    // low_limit 0 keeps the monitored value (1) in the OOR band.
    rewrite_params(
        &mut db,
        &ee_oid,
        BACnetEventParameter::OutOfRange {
            time_delay: 0,
            low_limit: 0.0,
            high_limit: 80.0,
            deadband: 2.0,
        },
    );
    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::NORMAL,
        "in-band value under the new OOR params settles NORMAL"
    );
    // Event_Type follows the rewritten parameters (Clause 12.12.5, #1868).
    assert_eq!(transitions[0].event_type, EventType::OUT_OF_RANGE);
    assert_eq!(event_state(&db, &ee_oid), EventState::NORMAL);
}

/// OFFNORMAL under a COV enrollment's increment criterion: Figure 13-10's
/// only arrow is ToNormal — the recovery indicates NORMAL and installs the
/// current sample as the detection baseline (13.3.3's rule), afterwards
/// unchanged.
#[test]
fn foreign_offnormal_under_cov_recovers_and_establishes_baseline() {
    let (mut db, ee_oid) = setup_on_notification_class(
        EventType::CHANGE_OF_STATE,
        BACnetEventParameter::ChangeOfState {
            time_delay: 0,
            list_of_values: vec![BACnetPropertyStates::UnsignedValue(3)],
        },
        3,
    );
    assert_eq!(
        evaluate_event_enrollments(&mut db, 1)[0].change.to,
        EventState::OFFNORMAL
    );

    rewrite_params(
        &mut db,
        &ee_oid,
        BACnetEventParameter::ChangeOfValue {
            time_delay: 0,
            criteria: ChangeOfValueCriteria::ReferencedPropertyIncrement(5.0),
        },
    );
    let transitions = evaluate_event_enrollments(&mut db, 1);
    assert_eq!(
        transitions.len(),
        1,
        "COV's only target is NORMAL: the foreign state recovers"
    );
    assert_eq!(transitions[0].change.from, EventState::OFFNORMAL);
    assert_eq!(transitions[0].change.to, EventState::NORMAL);
    assert_eq!(event_state(&db, &ee_oid), EventState::NORMAL);

    // Baseline installed at the recovered value (3.0): a +6 move crosses the
    // increment against IT and indicates NORMAL->NORMAL.
    let ai_oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap();
    db.get_mut(&ai_oid)
        .unwrap()
        .write_property(
            PropertyIdentifier::NOTIFICATION_CLASS,
            None,
            PropertyValue::Unsigned(9),
            None,
        )
        .unwrap();
    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::NORMAL);
}

/// HIGH_LIMIT under COBS parameters (recovery to NORMAL because the alarm
/// pattern does not match). The prior state is seeded through the internal
/// channel — the only in-tree route that leaves HIGH_LIMIT on a COBS
/// enrollment, since no algorithm combination produces it there (bitstring
/// monitored property, no crosswise-readable predecessor).
#[test]
fn foreign_high_limit_recovers_under_cobs_params() {
    let mut db = ObjectDatabase::new();

    // Target exposing a bitstring property: EVENT_ENABLE = all three
    // transitions → wire 0xE0.
    let mut target = EventEnrollmentObject::new(96, "Target", EventType::NONE).unwrap();
    target.set_event_enable(EventTransitionBits::all());
    let target_oid = target.object_identifier();
    db.add(Box::new(target)).unwrap();

    let mut ee = EventEnrollmentObject::new(97, "EE-cobs", EventType::CHANGE_OF_BITSTRING).unwrap();
    ee.set_object_property_reference(Some(BACnetDeviceObjectPropertyReference::new_local(
        target_oid,
        PropertyIdentifier::EVENT_ENABLE.to_raw(),
    )))
    .unwrap();
    ee.set_event_parameters(BACnetEventParameter::ChangeOfBitstring {
        time_delay: 0,
        bitmask: (5, vec![0x80]), // significant: TO_OFFNORMAL bit
        list_of_values: vec![(5, vec![0x00])], // alarm when that bit CLEAR
    });
    ee.set_event_enable(EventTransitionBits::all());
    // Internal channel: simulate the state an OOR predecessor would have left.
    ee.set_event_state_internal(EventState::HIGH_LIMIT).unwrap();
    let ee_oid = ee.object_identifier();
    db.add(Box::new(ee)).unwrap();

    let transitions = evaluate_event_enrollments(&mut db, 1);
    assert_eq!(transitions.len(), 1, "COBS recovers the foreign HIGH_LIMIT");
    assert_eq!(transitions[0].change.from, EventState::HIGH_LIMIT);
    assert_eq!(
        transitions[0].change.to,
        EventState::NORMAL,
        "bit set while the alarm pattern wants it clear -> no match -> settles NORMAL"
    );
    assert_eq!(event_state(&db, &ee_oid), EventState::NORMAL);
}

/// Zero-padded comparison width (review F2): mask [FF FF], alarm [00 01],
/// monitored [00] — the alarm's second significant byte never observed — is
/// NOT a match (an alarm pattern must agree over the whole width). The
/// truncating comparison reported OFFNORMAL on the shared first byte.
#[test]
fn cobs_mask_wider_than_monitored_value_is_not_a_match() {
    let mut db = ObjectDatabase::new();

    let mut ee =
        EventEnrollmentObject::new(97, "EE-cobs-w", EventType::CHANGE_OF_BITSTRING).unwrap();
    // Monitor a 1-byte bitstring (EVENT_ENABLE of a target with internal
    // 0x00 → wire 0x00).
    let mut target = EventEnrollmentObject::new(96, "Target", EventType::NONE).unwrap();
    target.set_event_enable(EventTransitionBits::empty());
    let target_oid = target.object_identifier();
    db.add(Box::new(target)).unwrap();
    ee.set_object_property_reference(Some(BACnetDeviceObjectPropertyReference::new_local(
        target_oid,
        PropertyIdentifier::EVENT_ENABLE.to_raw(),
    )))
    .unwrap();
    ee.set_event_parameters(BACnetEventParameter::ChangeOfBitstring {
        time_delay: 0,
        bitmask: (0, vec![0xFF, 0xFF]), // two significant BYTES
        list_of_values: vec![(0, vec![0x00, 0x01])], // alarm: second byte's low bit set
    });
    ee.set_event_enable(EventTransitionBits::all());
    let ee_oid = ee.object_identifier();
    db.add(Box::new(ee)).unwrap();

    // Monitored [0x00] zero-pads to [0x00, 0x00]; the alarm wants
    // [0x00, 0x01] under mask [0xFF, 0xFF] -> byte 1 disagrees -> NORMAL.
    assert!(
        evaluate_event_enrollments(&mut db, 1).is_empty(),
        "the alarm's second byte disagrees with the (zero-filled) monitored width"
    );
    assert_eq!(event_state(&db, &ee_oid), EventState::NORMAL);
}

/// FLOATING_LIMIT's reachable set is the same {NORMAL, HIGH_LIMIT,
/// LOW_LIMIT} triple, and its normalization is shared with OUT_OF_RANGE —
/// pin it independently anyway: a HIGH_LIMIT left under FL parameters whose
/// band (setpoint ± diffs) CONTAINS the value recovers to NORMAL.
#[test]
fn foreign_high_limit_recovers_under_floating_limit_params() {
    // Start OOR-typed so the wedge state (HIGH_LIMIT) is produced
    // organically: NC=90 > 80.
    let (mut db, ee_oid) = setup_on_notification_class(
        EventType::OUT_OF_RANGE,
        BACnetEventParameter::OutOfRange {
            time_delay: 0,
            low_limit: 20.0,
            high_limit: 80.0,
            deadband: 2.0,
        },
        90,
    );
    assert_eq!(
        evaluate_event_enrollments(&mut db, 1)[0].change.to,
        EventState::HIGH_LIMIT
    );

    // Rewrite to FLOATING_LIMIT with the setpoint reference pointing at the
    // monitored object's own PRESENT_VALUE (50.0): band = 50 ± (high_diff 60,
    // low_diff 60) = [-10, 110] contains NC=90 -> settles NORMAL. TD=0 keeps
    // the recovery immediate per the test-speed convention.
    let ai_oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap();
    db.get_mut(&ai_oid)
        .unwrap()
        .write_property(
            PropertyIdentifier::OUT_OF_SERVICE,
            None,
            PropertyValue::Boolean(true),
            None,
        )
        .unwrap();
    db.get_mut(&ai_oid)
        .unwrap()
        .write_property(
            PropertyIdentifier::PRESENT_VALUE,
            None,
            PropertyValue::Real(50.0),
            None,
        )
        .unwrap();
    rewrite_params(
        &mut db,
        &ee_oid,
        BACnetEventParameter::FloatingLimit {
            time_delay: 0,
            setpoint_reference: BACnetDeviceObjectPropertyReference::new_local(
                ai_oid,
                PropertyIdentifier::PRESENT_VALUE.to_raw(),
            ),
            low_diff_limit: 60.0,
            high_diff_limit: 60.0,
            deadband: 0.5,
        },
    );

    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::NORMAL,
        "FL from a foreign HIGH_LIMIT: value inside the band -> NORMAL"
    );
    assert_eq!(event_state(&db, &ee_oid), EventState::NORMAL);
}

/// Foreign state AND the monitored value IS in the alarm list: the COS
/// arm's foreign-recovery branch indicates OFFNORMAL — through the actions
/// path (Event_State stored as OFFNORMAL, delay gated by Time_Delay since
/// the target is offnormal) — rather than sitting silent. This pins the
/// `matched` half of the branch the other recovery tests leave unlit.
#[test]
fn foreign_high_limit_with_matching_alarm_indicates_offnormal() {
    // Produce the foreign state organically: OOR-typed, NC=90 > high_limit.
    let (mut db, ee_oid) = setup_on_notification_class(
        EventType::OUT_OF_RANGE,
        BACnetEventParameter::OutOfRange {
            time_delay: 0,
            low_limit: 20.0,
            high_limit: 80.0,
            deadband: 2.0,
        },
        90,
    );
    assert_eq!(
        evaluate_event_enrollments(&mut db, 1)[0].change.to,
        EventState::HIGH_LIMIT
    );

    // Rewrite to COS whose alarm list CONTAINS the monitored value (90).
    // TD=2: the offnormal-direction delay gates the recovery.
    rewrite_params(
        &mut db,
        &ee_oid,
        BACnetEventParameter::ChangeOfState {
            time_delay: 2,
            list_of_values: vec![BACnetPropertyStates::UnsignedValue(90)],
        },
    );
    for pass in 1..=2 {
        assert!(
            evaluate_event_enrollments(&mut db, 1).is_empty(),
            "recovery-to-OFFNORMAL pass {pass}: gated by pTimeDelay"
        );
        assert_eq!(
            event_state(&db, &ee_oid),
            EventState::HIGH_LIMIT,
            "the ghost state persists until the delay elapses"
        );
    }
    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::OFFNORMAL);
    assert_eq!(
        event_state(&db, &ee_oid),
        EventState::OFFNORMAL,
        "the SPECIFIC indicated state is stored (13.2.2.1.4), not left at the ghost"
    );
}

/// HIGH_LIMIT left by OUT_OF_RANGE under rewritten `none` parameters: the
/// NONE algorithm has no offnormal states, so the enrollment returns to
/// NORMAL at once with a distributed TO_NORMAL (#1868).
#[test]
fn offnormal_returns_to_normal_under_none_params() {
    let (mut db, ee_oid) = setup_on_notification_class(
        EventType::OUT_OF_RANGE,
        BACnetEventParameter::OutOfRange {
            time_delay: 0,
            low_limit: 20.0,
            high_limit: 80.0,
            deadband: 2.0,
        },
        90,
    );
    assert_eq!(
        evaluate_event_enrollments(&mut db, 1)[0].change.to,
        EventState::HIGH_LIMIT
    );
    db.get_mut(&ee_oid)
        .unwrap()
        .write_property(
            PropertyIdentifier::EVENT_PARAMETERS,
            None,
            PropertyValue::ApplicationData(vec![0xF8, 0x14]),
            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::NORMAL);
    assert_eq!(transitions[0].event_type, EventType::NONE);
    assert!(transitions[0].distribute, "TO_NORMAL is enabled");
    assert_eq!(event_state(&db, &ee_oid), EventState::NORMAL);
    // NORMAL stays put under `none`.
    assert!(evaluate_event_enrollments(&mut db, 1).is_empty());
}