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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
use super::*;

// --- LightingOutputObject ---

#[test]
fn lighting_output_create() {
    let obj = LightingOutputObject::new(1, "LO-1").unwrap();
    assert_eq!(obj.object_name(), "LO-1");
    assert_eq!(
        obj.object_identifier().object_type(),
        ObjectType::LIGHTING_OUTPUT
    );
    assert_eq!(obj.object_identifier().instance_number(), 1);
}

#[test]
fn lighting_output_read_present_value() {
    let obj = LightingOutputObject::new(1, "LO-1").unwrap();
    let pv = obj.read_property(PropertyIdentifier::PRESENT_VALUE, None);
    assert_eq!(pv.unwrap(), PropertyValue::Real(0.0));
}

#[test]
fn lighting_output_read_object_type() {
    let obj = LightingOutputObject::new(1, "LO-1").unwrap();
    let ot = obj
        .read_property(PropertyIdentifier::OBJECT_TYPE, None)
        .unwrap();
    assert_eq!(
        ot,
        PropertyValue::Enumerated(ObjectType::LIGHTING_OUTPUT.to_raw())
    );
}

#[test]
fn lighting_output_write_pv_commandable() {
    let mut obj = LightingOutputObject::new(1, "LO-1").unwrap();
    // Write at priority 8
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(75.0),
        Some(8),
    )
    .unwrap();
    let pv = obj
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(pv, PropertyValue::Real(75.0));

    // Write at priority 1 (higher) overrides
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(50.0),
        Some(1),
    )
    .unwrap();
    let pv = obj
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(pv, PropertyValue::Real(50.0));

    // Relinquish priority 1 — falls back to priority 8 value
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Null,
        Some(1),
    )
    .unwrap();
    let pv = obj
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(pv, PropertyValue::Real(75.0));
}

#[test]
fn lighting_output_pv_out_of_range() {
    let mut obj = LightingOutputObject::new(1, "LO-1").unwrap();
    let result = obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(101.0),
        Some(16),
    );
    assert!(result.is_err());

    // -1.0 is the WARN special value; -0.5 is just out of range.
    let result = obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(-0.5),
        Some(16),
    );
    assert!(result.is_err());
}

#[test]
fn lighting_output_priority_array_read() {
    let mut obj = LightingOutputObject::new(1, "LO-1").unwrap();
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(50.0),
        Some(8),
    )
    .unwrap();

    // Read array size (index 0)
    let size = obj
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(0))
        .unwrap();
    assert_eq!(size, PropertyValue::Unsigned(16));

    // Read slot 8
    let slot = obj
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(8))
        .unwrap();
    assert_eq!(slot, PropertyValue::Real(50.0));

    // Read empty slot 1
    let slot = obj
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(1))
        .unwrap();
    assert_eq!(slot, PropertyValue::Null);
}

#[test]
fn lighting_output_priority_array_tracks_present_value_command() {
    let mut obj = LightingOutputObject::new(1, "LO-1").unwrap();
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(33.0),
        Some(5),
    )
    .unwrap();
    let pv = obj
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(pv, PropertyValue::Real(33.0));
}

#[test]
fn lighting_output_relinquish_default() {
    let obj = LightingOutputObject::new(1, "LO-1").unwrap();
    let rd = obj
        .read_property(PropertyIdentifier::RELINQUISH_DEFAULT, None)
        .unwrap();
    assert_eq!(rd, PropertyValue::Real(0.0));
}

#[test]
fn lighting_output_lighting_properties() {
    let mut obj = LightingOutputObject::new(1, "LO-1").unwrap();

    // TRACKING_VALUE
    let tv = obj
        .read_property(PropertyIdentifier::TRACKING_VALUE, None)
        .unwrap();
    assert_eq!(tv, PropertyValue::Real(0.0));

    // LIGHTING_COMMAND: operation NONE until written.
    let lc = obj
        .read_property(PropertyIdentifier::LIGHTING_COMMAND, None)
        .unwrap();
    assert_eq!(lc, PropertyValue::ApplicationData(vec![0x09, 0x00]));

    // Write LIGHTING_COMMAND: STEP_UP (3).
    obj.write_property(
        PropertyIdentifier::LIGHTING_COMMAND,
        None,
        PropertyValue::ApplicationData(vec![0x09, 0x03]),
        None,
    )
    .unwrap();
    let lc = obj
        .read_property(PropertyIdentifier::LIGHTING_COMMAND, None)
        .unwrap();
    assert_eq!(lc, PropertyValue::ApplicationData(vec![0x09, 0x03]));

    // LIGHTING_COMMAND_DEFAULT_PRIORITY
    let lcdp = obj
        .read_property(PropertyIdentifier::LIGHTING_COMMAND_DEFAULT_PRIORITY, None)
        .unwrap();
    assert_eq!(lcdp, PropertyValue::Unsigned(16));

    // IN_PROGRESS
    let ip = obj
        .read_property(PropertyIdentifier::IN_PROGRESS, None)
        .unwrap();
    assert_eq!(ip, PropertyValue::Enumerated(0));

    // BLINK_WARN_ENABLE
    let bwe = obj
        .read_property(PropertyIdentifier::BLINK_WARN_ENABLE, None)
        .unwrap();
    assert_eq!(bwe, PropertyValue::Boolean(false));

    // EGRESS_TIME
    let et = obj
        .read_property(PropertyIdentifier::EGRESS_TIME, None)
        .unwrap();
    assert_eq!(et, PropertyValue::Unsigned(0));

    // EGRESS_ACTIVE
    let ea = obj
        .read_property(PropertyIdentifier::EGRESS_ACTIVE, None)
        .unwrap();
    assert_eq!(ea, PropertyValue::Boolean(false));
}

#[test]
fn lighting_output_property_list() {
    let obj = LightingOutputObject::new(1, "LO-1").unwrap();
    let props = obj.property_list();
    assert!(props.contains(&PropertyIdentifier::PRESENT_VALUE));
    assert!(props.contains(&PropertyIdentifier::TRACKING_VALUE));
    assert!(props.contains(&PropertyIdentifier::LIGHTING_COMMAND));
    assert!(props.contains(&PropertyIdentifier::PRIORITY_ARRAY));
    assert!(props.contains(&PropertyIdentifier::RELINQUISH_DEFAULT));
}

// --- BinaryLightingOutputObject ---

#[test]
fn binary_lighting_output_create() {
    let obj = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    assert_eq!(obj.object_name(), "BLO-1");
    assert_eq!(
        obj.object_identifier().object_type(),
        ObjectType::BINARY_LIGHTING_OUTPUT
    );
    assert_eq!(obj.object_identifier().instance_number(), 1);
}

#[test]
fn binary_lighting_output_read_present_value() {
    let obj = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    let pv = obj.read_property(PropertyIdentifier::PRESENT_VALUE, None);
    assert_eq!(pv.unwrap(), PropertyValue::Enumerated(0)); // off
}

#[test]
fn binary_lighting_output_read_object_type() {
    let obj = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    let ot = obj
        .read_property(PropertyIdentifier::OBJECT_TYPE, None)
        .unwrap();
    assert_eq!(
        ot,
        PropertyValue::Enumerated(ObjectType::BINARY_LIGHTING_OUTPUT.to_raw())
    );
}

#[test]
fn binary_lighting_output_write_pv_commandable() {
    let mut obj = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    // Write on (1) at priority 8
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Enumerated(1),
        Some(8),
    )
    .unwrap();
    let pv = obj
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(pv, PropertyValue::Enumerated(1));

    // WARN is an operation, not a stored command. Priority 1 has no existing
    // ON slot, so this is an accepted no-op.
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Enumerated(2),
        Some(1),
    )
    .unwrap();
    let pv = obj
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(pv, PropertyValue::Enumerated(1));
    assert_eq!(
        obj.read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(1))
            .unwrap(),
        PropertyValue::Null
    );

    // Relinquish priority 1 — falls back to priority 8 (on)
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Null,
        Some(1),
    )
    .unwrap();
    let pv = obj
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(pv, PropertyValue::Enumerated(1));
}

#[test]
fn binary_lighting_output_pv_out_of_range() {
    let mut obj = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    let result = obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Enumerated(6),
        Some(16),
    );
    assert!(result.is_err());
}

#[test]
fn binary_lighting_output_all_named_pv_commands_are_accepted_without_storing_operations() {
    for val in 0..=5 {
        let mut obj = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
        obj.write_property(
            PropertyIdentifier::PRESENT_VALUE,
            None,
            PropertyValue::Enumerated(val),
            Some(16),
        )
        .unwrap();
        let pv = obj
            .read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap();
        let expected = u32::from(val == 1);
        assert_eq!(pv, PropertyValue::Enumerated(expected));
        let slot = obj
            .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(16))
            .unwrap();
        assert!(matches!(
            slot,
            PropertyValue::Null | PropertyValue::Enumerated(0 | 1)
        ));
    }
}

#[test]
fn binary_lighting_output_priority_array() {
    let mut obj = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Enumerated(1),
        Some(5),
    )
    .unwrap();

    // Read array size
    let size = obj
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(0))
        .unwrap();
    assert_eq!(size, PropertyValue::Unsigned(16));

    // Read slot 5
    let slot = obj
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(5))
        .unwrap();
    assert_eq!(slot, PropertyValue::Enumerated(1));

    // Read empty slot 1
    let slot = obj
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(1))
        .unwrap();
    assert_eq!(slot, PropertyValue::Null);
}

#[test]
fn binary_lighting_output_priority_array_tracks_present_value_command() {
    let mut obj = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    obj.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Enumerated(1),
        Some(3),
    )
    .unwrap();
    let pv = obj
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(pv, PropertyValue::Enumerated(1));

    for operation in 2..=5 {
        assert!(obj
            .write_property(
                PropertyIdentifier::PRIORITY_ARRAY,
                Some(3),
                PropertyValue::Enumerated(operation),
                None,
            )
            .is_err());
        assert_eq!(
            obj.read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(3))
                .unwrap(),
            PropertyValue::Enumerated(1)
        );
    }
}

#[test]
fn binary_lighting_output_property_list() {
    let obj = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    let props = obj.property_list();
    assert!(props.contains(&PropertyIdentifier::PRESENT_VALUE));
    assert!(props.contains(&PropertyIdentifier::BLINK_WARN_ENABLE));
    assert!(props.contains(&PropertyIdentifier::EGRESS_TIME));
    assert!(props.contains(&PropertyIdentifier::PRIORITY_ARRAY));
    assert!(props.contains(&PropertyIdentifier::RELINQUISH_DEFAULT));
}

// ---------------------------------------------------------------------------
// #270 — writable Relinquish_Default (Lighting Output + Binary Lighting Output)
// ---------------------------------------------------------------------------

/// Lighting Output: Relinquish_Default is validated like a commanded
/// Present_Value (finite Real within the 0..=100 light level) and — with an
/// all-NULL priority array — Present_Value immediately resolves to it.
#[test]
fn lighting_output_relinquish_default_write_recaptures_present_value() {
    let mut lo = LightingOutputObject::new(1, "LO-1").unwrap();
    assert!(lo.is_writable_property(PropertyIdentifier::RELINQUISH_DEFAULT));

    lo.write_property(
        PropertyIdentifier::RELINQUISH_DEFAULT,
        None,
        PropertyValue::Real(75.0),
        None,
    )
    .unwrap();
    assert_eq!(
        lo.read_property(PropertyIdentifier::RELINQUISH_DEFAULT, None)
            .unwrap(),
        PropertyValue::Real(75.0)
    );
    assert_eq!(
        lo.read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap(),
        PropertyValue::Real(75.0),
        "with an empty priority array, PV must resolve to the written default"
    );

    for value in [
        PropertyValue::Real(-1.0),     // below the light level range
        PropertyValue::Real(100.5),    // above it
        PropertyValue::Real(f32::NAN), // non-finite
        PropertyValue::Enumerated(1),  // wrong type
    ] {
        assert!(lo
            .write_property(PropertyIdentifier::RELINQUISH_DEFAULT, None, value, None)
            .is_err());
        assert_eq!(
            lo.read_property(PropertyIdentifier::RELINQUISH_DEFAULT, None)
                .unwrap(),
            PropertyValue::Real(75.0),
            "refused writes must leave Relinquish_Default untouched"
        );
    }
    assert_eq!(
        lo.read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap(),
        PropertyValue::Real(75.0)
    );
}

fn assert_property_error(error: Error, expected_code: bacnet_types::enums::ErrorCode) {
    match error {
        Error::Protocol { class, code } => {
            assert_eq!(
                class,
                bacnet_types::enums::ErrorClass::PROPERTY.to_raw() as u32
            );
            assert_eq!(code, expected_code.to_raw() as u32);
        }
        other => panic!("expected PROPERTY / {expected_code:?}, got {other:?}"),
    }
}

fn assert_binary_lighting_output_command_state(
    blo: &BinaryLightingOutputObject,
    relinquish_default: u32,
    present_value: u32,
    priority_8: PropertyValue,
) {
    assert_eq!(
        blo.read_property(PropertyIdentifier::RELINQUISH_DEFAULT, None)
            .unwrap(),
        PropertyValue::Enumerated(relinquish_default)
    );
    assert_eq!(
        blo.read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap(),
        PropertyValue::Enumerated(present_value)
    );
    assert_eq!(
        blo.read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(8))
            .unwrap(),
        priority_8
    );
}

/// Binary Lighting Output Relinquish_Default admits only OFF/ON and recaptures
/// Present_Value immediately while the priority array is empty.
#[test]
fn binary_lighting_output_relinquish_default_accepts_off_on_and_recaptures_present_value() {
    let mut blo = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    assert!(blo.is_writable_property(PropertyIdentifier::RELINQUISH_DEFAULT));

    for value in [1, 0] {
        blo.set_relinquish_default(value).unwrap();
        assert_binary_lighting_output_command_state(&blo, value, value, PropertyValue::Null);
    }
}

#[test]
fn binary_lighting_output_relinquish_default_rejects_non_binary_values_atomically() {
    let mut blo = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    blo.set_relinquish_default(1).unwrap();
    blo.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Enumerated(0),
        Some(8),
    )
    .unwrap();

    for value in [2, 3, 4, 5, 255, u32::MAX] {
        assert_property_error(
            blo.set_relinquish_default(value)
                .expect_err("non-binary local default must be refused"),
            bacnet_types::enums::ErrorCode::VALUE_OUT_OF_RANGE,
        );
        assert_binary_lighting_output_command_state(&blo, 1, 0, PropertyValue::Enumerated(0));

        assert_property_error(
            blo.write_property(
                PropertyIdentifier::RELINQUISH_DEFAULT,
                None,
                PropertyValue::Enumerated(value),
                None,
            )
            .expect_err("non-binary object write must be refused"),
            bacnet_types::enums::ErrorCode::VALUE_OUT_OF_RANGE,
        );
        assert_binary_lighting_output_command_state(&blo, 1, 0, PropertyValue::Enumerated(0));
    }
}

#[test]
fn binary_lighting_output_relinquish_default_wrong_type_is_atomic() {
    let mut blo = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    blo.set_relinquish_default(1).unwrap();
    blo.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Enumerated(0),
        Some(8),
    )
    .unwrap();

    assert_property_error(
        blo.write_property(
            PropertyIdentifier::RELINQUISH_DEFAULT,
            None,
            PropertyValue::Unsigned(1),
            None,
        )
        .expect_err("wrong Relinquish_Default datatype must be refused"),
        bacnet_types::enums::ErrorCode::INVALID_DATA_TYPE,
    );
    assert_binary_lighting_output_command_state(&blo, 1, 0, PropertyValue::Enumerated(0));
}

#[test]
fn binary_lighting_output_active_command_outranks_relinquish_default() {
    let mut blo = BinaryLightingOutputObject::new(1, "BLO-1").unwrap();
    blo.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Enumerated(0),
        Some(8),
    )
    .unwrap();

    blo.set_relinquish_default(1).unwrap();
    assert_binary_lighting_output_command_state(&blo, 1, 0, PropertyValue::Enumerated(0));

    blo.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Null,
        Some(8),
    )
    .unwrap();
    assert_binary_lighting_output_command_state(&blo, 1, 1, PropertyValue::Null);
}