bacnet-ops 0.15.0

UI-free BACnet operations core shared by the bacnet CLI, its TUI and its MCP server
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
//! Logged values named from a Log_DeviceObjectProperty reference, and the
//! fallbacks when it names nothing usable.

use bacnet_encoding::constructed::encode_device_object_property_reference;
use bacnet_types::constructed::BACnetDeviceObjectPropertyReference;
use bacnet_types::enums::{EventState, ObjectType, PropertyIdentifier};
use bacnet_types::primitives::{ObjectIdentifier, PropertyValue};
use bytes::BytesMut;
use std::ops::ControlFlow;

use super::{LogColumns, LoggedProperty, MAX_STATE_TEXT_OBJECTS};
use crate::format::{Datum, Floats, StateTexts, ValueFormat};

fn reference(
    object_type: ObjectType,
    instance: u32,
    property: PropertyIdentifier,
) -> BACnetDeviceObjectPropertyReference {
    BACnetDeviceObjectPropertyReference::new_local(
        ObjectIdentifier::new(object_type, instance).unwrap(),
        property.to_raw(),
    )
}

/// References encoded back to back, as a whole read of the property
/// carries them.
fn encoded(references: &[BACnetDeviceObjectPropertyReference]) -> Vec<u8> {
    let mut buf = BytesMut::new();
    for reference in references {
        encode_device_object_property_reference(&mut buf, reference);
    }
    buf.to_vec()
}

fn logged(column: Option<LoggedProperty>, value: &PropertyValue) -> String {
    let texts = StateTexts::default();
    ValueFormat::new(ObjectType::TREND_LOG, &texts, Floats::Exact).logged(column, value)
}

fn logged_data(column: Option<LoggedProperty>, value: &PropertyValue) -> Datum {
    let texts = StateTexts::default();
    ValueFormat::new(ObjectType::TREND_LOG, &texts, Floats::Exact).logged_data(column, value)
}

fn status_flags(bits: u8) -> PropertyValue {
    PropertyValue::BitString {
        unused_bits: 4,
        data: vec![bits << 4],
    }
}

/// An Event_State log names its enumerations and a Status_Flags log its
/// bits, in text and as data.
#[test]
fn event_state_and_status_flags_logs_are_named() {
    let columns = LogColumns::decode(&encoded(&[reference(
        ObjectType::ANALOG_INPUT,
        1,
        PropertyIdentifier::EVENT_STATE,
    )]));
    let event_state = columns.column(0);
    assert_eq!(
        event_state,
        Some(LoggedProperty {
            object: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap(),
            property: PropertyIdentifier::EVENT_STATE,
            index: None,
            device: None,
        })
    );
    let offnormal = PropertyValue::Enumerated(EventState::OFFNORMAL.to_raw());
    assert_eq!(logged(event_state, &offnormal), "offnormal");
    assert_eq!(
        logged_data(event_state, &offnormal),
        Datum::Enumerated {
            value: 2,
            name: Some("offnormal".into()),
        }
    );

    let columns = LogColumns::decode(&encoded(&[reference(
        ObjectType::ANALOG_INPUT,
        1,
        PropertyIdentifier::STATUS_FLAGS,
    )]));
    let flags = columns.column(0);
    // In alarm and out of service: the first and last of the four bits.
    assert_eq!(
        logged(flags, &status_flags(0b1001)),
        "in-alarm | out-of-service"
    );
    assert_eq!(logged(flags, &status_flags(0)), "none");
    assert_eq!(
        logged_data(flags, &status_flags(0b1000)),
        Datum::Bits {
            bits: "1000".into(),
            names: Some(vec!["in-alarm".into()]),
        }
    );
}

/// The monitored object's type names a binary object's state.
#[test]
fn a_binary_present_value_log_names_its_state() {
    let columns = LogColumns::decode(&encoded(&[reference(
        ObjectType::BINARY_VALUE,
        3,
        PropertyIdentifier::PRESENT_VALUE,
    )]));
    let column = columns.column(0);
    assert_eq!(logged(column, &PropertyValue::Enumerated(1)), "active");
    assert_eq!(logged(column, &PropertyValue::Enumerated(0)), "inactive");
    // A number past the two states names nothing.
    assert_eq!(logged(column, &PropertyValue::Enumerated(7)), "enum 7");
}

/// With no column named, and for a value whose datatype the property
/// doesn't name, the value prints as the log gives it.
#[test]
fn unnamed_and_mismatched_values_print_as_logged() {
    let offnormal = PropertyValue::Enumerated(2);
    assert_eq!(logged(None, &offnormal), "enum 2");
    assert_eq!(logged(None, &status_flags(0b1000)), "bits(1000)");
    assert_eq!(
        logged_data(None, &offnormal),
        Datum::Enumerated {
            value: 2,
            name: None,
        }
    );

    let state = LoggedProperty {
        object: ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap(),
        property: PropertyIdentifier::EVENT_STATE,
        index: None,
        device: None,
    };
    let event_state = Some(state);
    assert_eq!(logged(event_state, &PropertyValue::Real(72.5)), "72.5");
    assert_eq!(
        logged(event_state, &status_flags(0b1000)),
        "bits(1000)",
        "Event_State has no named bits"
    );
    let flags = Some(LoggedProperty {
        property: PropertyIdentifier::STATUS_FLAGS,
        ..state
    });
    assert_eq!(logged(flags, &offnormal), "enum 2");
    assert_eq!(
        logged_data(flags, &offnormal),
        Datum::Enumerated {
            value: 2,
            name: None,
        }
    );
}

/// A Trend Log Multiple's array names each member's column; an empty
/// element names nothing, and octets that don't decode name no column.
#[test]
fn columns_follow_the_members_and_fall_back_on_bad_octets() {
    let empty = reference(
        ObjectType::ANALOG_INPUT,
        ObjectIdentifier::WILDCARD_INSTANCE,
        PropertyIdentifier::PRESENT_VALUE,
    );
    let columns = LogColumns::decode(&encoded(&[
        reference(
            ObjectType::ANALOG_INPUT,
            1,
            PropertyIdentifier::PRESENT_VALUE,
        ),
        empty.clone(),
        reference(
            ObjectType::BINARY_INPUT,
            2,
            PropertyIdentifier::PRESENT_VALUE,
        ),
    ]));
    assert!(!columns.is_empty());
    assert_eq!(
        columns.column(0).map(|column| column.object.object_type()),
        Some(ObjectType::ANALOG_INPUT)
    );
    assert_eq!(columns.column(1), None);
    assert_eq!(
        logged(columns.column(2), &PropertyValue::Enumerated(1)),
        "active"
    );
    assert_eq!(columns.column(3), None, "past the last member");

    assert!(LogColumns::decode(&encoded(&[empty])).is_empty());
    assert!(LogColumns::decode(&[]).is_empty());
    let mut cut = encoded(&[reference(
        ObjectType::ANALOG_INPUT,
        1,
        PropertyIdentifier::EVENT_STATE,
    )]);
    cut.pop();
    assert_eq!(LogColumns::decode(&cut), LogColumns::default());
    // A good reference before the cut one names nothing either: the
    // columns would no longer line up with the members.
    let mut tail = encoded(&[reference(
        ObjectType::ANALOG_INPUT,
        1,
        PropertyIdentifier::STATUS_FLAGS,
    )]);
    tail.extend_from_slice(&cut);
    assert_eq!(LogColumns::decode(&tail), LogColumns::default());
    // An application-tagged value is no reference.
    assert_eq!(LogColumns::decode(&[0x91, 0x02]), LogColumns::default());
}

/// A reference to another device names its column as a local one does,
/// with its array index; one whose Device member is at the reserved
/// instance is empty.
#[test]
fn device_qualified_references_name_their_column() {
    let remote = BACnetDeviceObjectPropertyReference {
        property_array_index: Some(8),
        device_identifier: Some(ObjectIdentifier::new(ObjectType::DEVICE, 77).unwrap()),
        ..reference(
            ObjectType::BINARY_OUTPUT,
            2,
            PropertyIdentifier::PRIORITY_ARRAY,
        )
    };
    let unset_device = BACnetDeviceObjectPropertyReference {
        device_identifier: Some(
            ObjectIdentifier::new(ObjectType::DEVICE, ObjectIdentifier::WILDCARD_INSTANCE).unwrap(),
        ),
        ..reference(ObjectType::ANALOG_INPUT, 1, PropertyIdentifier::EVENT_STATE)
    };
    let columns = LogColumns::decode(&encoded(&[remote, unset_device]));
    let column = columns.column(0);
    assert_eq!(
        column,
        Some(LoggedProperty {
            object: ObjectIdentifier::new(ObjectType::BINARY_OUTPUT, 2).unwrap(),
            property: PropertyIdentifier::PRIORITY_ARRAY,
            index: Some(8),
            device: Some(77),
        })
    );
    // A Priority_Array slot of a binary output names its state.
    assert_eq!(logged(column, &PropertyValue::Enumerated(1)), "active");
    assert_eq!(columns.column(1), None, "the Device member is unset");
}

/// Which logs have a Log_DeviceObjectProperty to read.
#[test]
fn only_trend_logs_name_their_columns() {
    assert!(LogColumns::applies(ObjectType::TREND_LOG));
    assert!(LogColumns::applies(ObjectType::TREND_LOG_MULTIPLE));
    assert!(!LogColumns::applies(ObjectType::EVENT_LOG));
    assert!(!LogColumns::applies(ObjectType::AUDIT_LOG));
}

fn state_column(object_type: ObjectType, instance: u32) -> Option<LoggedProperty> {
    Some(LoggedProperty {
        object: ObjectIdentifier::new(object_type, instance).unwrap(),
        property: PropertyIdentifier::PRESENT_VALUE,
        index: None,
        device: None,
    })
}

/// Column `index` of `columns`, as text, named with its state texts.
fn named(columns: &LogColumns, index: usize, value: &PropertyValue) -> String {
    ValueFormat::new(
        ObjectType::TREND_LOG,
        columns.state_texts(index),
        Floats::Exact,
    )
    .logged(columns.column(index), value)
}

/// A binary and a multi-state column are named with their objects' state
/// texts; a state outside a short State_Text, an empty text and a column
/// without texts keep today's names (#2122).
#[tokio::test]
async fn state_texts_name_binary_and_multi_state_columns() {
    let mut columns: LogColumns = [
        state_column(ObjectType::BINARY_VALUE, 1),
        state_column(ObjectType::MULTI_STATE_VALUE, 1),
        state_column(ObjectType::MULTI_STATE_INPUT, 2),
    ]
    .into_iter()
    .collect();
    columns
        .read_state_texts(Some(5), |object| async move {
            ControlFlow::Continue(match object.object_type() {
                ObjectType::BINARY_VALUE => StateTexts {
                    active: Some("Running".into()),
                    inactive: Some(String::new()),
                    states: Vec::new(),
                },
                ObjectType::MULTI_STATE_VALUE => StateTexts {
                    states: vec!["Off".into(), "Lo\nw".into(), "x".repeat(200)],
                    ..StateTexts::default()
                },
                // A refused read.
                _ => StateTexts::default(),
            })
        })
        .await;
    let enumerated = PropertyValue::Enumerated;
    assert_eq!(named(&columns, 0, &enumerated(1)), "active (Running)");
    assert_eq!(named(&columns, 0, &enumerated(0)), "inactive", "empty text");
    // Control characters in a device's text show as spaces.
    assert_eq!(named(&columns, 1, &PropertyValue::Unsigned(2)), "2 (Lo w)");
    assert_eq!(named(&columns, 1, &PropertyValue::Unsigned(1)), "1 (Off)");
    // Past the end of State_Text, and state 0, which no text names.
    assert!(named(&columns, 1, &PropertyValue::Unsigned(3)).starts_with("3 (xxx"));
    assert_eq!(named(&columns, 1, &PropertyValue::Unsigned(4)), "4");
    assert_eq!(named(&columns, 1, &PropertyValue::Unsigned(0)), "0");
    assert_eq!(named(&columns, 2, &PropertyValue::Unsigned(2)), "2");
    // The typed form keeps the formatter's own names: no device text.
    let typed = ValueFormat::new(ObjectType::TREND_LOG, columns.state_texts(0), Floats::Exact)
        .logged_data(columns.column(0), &enumerated(1));
    assert!(!typed.may_hold_device_text(), "{typed:?}");
    // Shown cleaned and cut; kept raw for a front end that cleans its own.
    let cut = "x".repeat(crate::format::MAX_NAME_CHARS);
    assert_eq!(columns.state_texts(1).states, ["Off", "Lo w", cut.as_str()]);
    assert_eq!(columns.raw_state_texts(1).states[1], "Lo\nw");
    assert_eq!(columns.raw_state_texts(1).states[2], "x".repeat(200));
    assert_eq!(columns.raw_state_texts(2), &StateTexts::default());
    assert_eq!(columns.state_texts(9), &StateTexts::default());
}

/// Texts are read only for state columns, once an object, on the log's own
/// device, for at most MAX_STATE_TEXT_OBJECTS objects, and a break ends
/// the reading (#2122).
#[tokio::test]
async fn state_texts_are_read_once_an_object_within_the_cap() {
    let on = |device| {
        Some(LoggedProperty {
            device,
            ..state_column(ObjectType::BINARY_INPUT, 3).unwrap()
        })
    };
    let mut wanted: Vec<Option<LoggedProperty>> = vec![
        state_column(ObjectType::MULTI_STATE_VALUE, 1),
        // The same object again, a non-state property, a non-state object,
        // an empty member, and a binary input on this device and another.
        state_column(ObjectType::MULTI_STATE_VALUE, 1),
        Some(LoggedProperty {
            property: PropertyIdentifier::STATUS_FLAGS,
            ..state_column(ObjectType::MULTI_STATE_VALUE, 2).unwrap()
        }),
        state_column(ObjectType::ANALOG_VALUE, 1),
        None,
        on(Some(5)),
        on(Some(6)),
    ];
    wanted.extend((10..20).map(|instance| state_column(ObjectType::BINARY_VALUE, instance)));
    let oid = |object_type, instance| ObjectIdentifier::new(object_type, instance).unwrap();

    let mut asked = Vec::new();
    let mut columns: LogColumns = wanted.iter().copied().collect();
    columns
        .read_state_texts(Some(5), |object| {
            asked.push(object);
            async { ControlFlow::Continue(StateTexts::default()) }
        })
        .await;
    assert_eq!(asked.len(), MAX_STATE_TEXT_OBJECTS, "{asked:?}");
    assert_eq!(asked[0], oid(ObjectType::MULTI_STATE_VALUE, 1));
    assert_eq!(asked[1], oid(ObjectType::BINARY_INPUT, 3));
    assert_eq!(asked[2], oid(ObjectType::BINARY_VALUE, 10));

    // A log device the client doesn't know reads only unqualified ones.
    let mut asked = Vec::new();
    let mut columns: LogColumns = wanted.iter().copied().collect();
    columns
        .read_state_texts(None, |object| {
            asked.push(object);
            async { ControlFlow::Continue(StateTexts::default()) }
        })
        .await;
    assert!(
        !asked.contains(&oid(ObjectType::BINARY_INPUT, 3)),
        "{asked:?}"
    );

    // A break ends the reading, and the columns read before it keep their
    // texts.
    let mut columns: LogColumns = wanted.iter().copied().collect();
    let mut calls = 0;
    columns
        .read_state_texts(Some(5), |_| {
            calls += 1;
            let first = calls == 1;
            async move {
                if first {
                    ControlFlow::Continue(StateTexts {
                        states: vec!["Off".into()],
                        ..StateTexts::default()
                    })
                } else {
                    ControlFlow::Break(())
                }
            }
        })
        .await;
    assert_eq!(calls, 2);
    assert_eq!(columns.state_texts(0).states, ["Off"]);
    assert_eq!(columns.state_texts(1).states, ["Off"], "the same object");
    assert_eq!(columns.state_texts(5), &StateTexts::default());

    // No state column: nothing is read.
    let mut plain = LogColumns::decode(&encoded(&[reference(
        ObjectType::ANALOG_INPUT,
        1,
        PropertyIdentifier::PRESENT_VALUE,
    )]));
    plain
        .read_state_texts(Some(5), |_| async {
            panic!("an analog column has no state texts")
        })
        .await;
}