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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! A timestamped COV-multiple report that one notification cannot carry goes
//! out strictly in capture order, each reference's latest change included
//! (135-2020 §13.1, §13.18.1.1; #1008).
//!
//! Each notification fits the subscriber's maximum APDU even when the latest
//! changes alone would not, and carries a contiguous run of the report's
//! changes: every change in one notification is older than every change in
//! the next. The untimestamped values go in the last notification, or, when
//! they alone do not fit one, after every change in as many as they need
//! (#1038). A reference whose latest change went out in an earlier part
//! completes its
//! observation when that part is sent, or, when confirmed, acknowledged. A
//! change too large for any notification goes out one value per
//! notification (`cov_timed_value_split_tests`, #1090), and a value too large
//! even alone is dropped and counted, latest or not, so it never stalls the
//! context. Time is paused.
use super::cov_wire_test_support::*;
use super::*;
use bacnet_objects::analog::AnalogValueObject;
use bacnet_services::cov_multiple::COVNotificationMultipleRequest;
use bacnet_types::enums::EngineeringUnits;
use bacnet_types::primitives::Time;

const SMALL_APDU: u16 = 206;

/// Timestamped references in the many-object context: one PV and
/// Status_Flags change of each takes 33 octets, so the latest changes of all
/// of them exceed one 206-octet notification, which carries five.
const OBJECTS: u32 = 8;

/// One PV row: object, encoded value and Time_Of_Change.
type Row = (ObjectIdentifier, Vec<u8>, Option<Time>);

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

fn timed_objects() -> impl Iterator<Item = ObjectIdentifier> {
    (1..=OBJECTS).map(av)
}

/// Encoded APDU length of `notification`: its service request plus the
/// unconfirmed (2 octets) or unsegmented confirmed (4 octets) header.
fn apdu_len(notification: &COVNotificationMultipleRequest, confirmed: bool) -> usize {
    let mut encoded = BytesMut::new();
    notification.encode(&mut encoded).unwrap();
    encoded.len() + if confirmed { 4 } else { 2 }
}

/// Every PV row of `notification`, ordered by time; within one notification
/// items group by object, not by time.
fn pv_rows_by_time(notification: &COVNotificationMultipleRequest) -> Vec<Row> {
    let mut rows: Vec<Row> = notification
        .list_of_cov_notifications
        .iter()
        .flat_map(|item| {
            item.list_of_values
                .iter()
                .filter(|value| value.property_identifier == PV)
                .map(|value| {
                    (
                        item.monitored_object_identifier,
                        value.value.clone(),
                        value.time_of_change,
                    )
                })
        })
        .collect();
    rows.sort_by_key(|(_, _, time)| time.map(|time| time.second));
    rows
}

/// `notification` fits the small APDU, and its header names the newest
/// timestamped PV change it carries.
fn check(notification: &COVNotificationMultipleRequest, confirmed: bool) {
    let len = apdu_len(notification, confirmed);
    assert!(
        len <= usize::from(SMALL_APDU),
        "{len}-octet APDU exceeds {SMALL_APDU}"
    );
    let newest = pv_rows_by_time(notification)
        .into_iter()
        .filter_map(|(_, _, time)| time)
        .next_back();
    assert_eq!(
        notification.timestamp.map(|(_, time)| time),
        newest,
        "the header names the last change carried"
    );
}

/// A server with AV-1 to AV-8 and, when `untimed` is given, that many more
/// AVs after them, subscribed by one context of the subscriber's 206-octet
/// APDU: the first eight with timestamps, the rest without. The initial
/// report is left untaken.
async fn many_object_harness(confirmed: bool, untimed: u32, delay: u32) -> Harness {
    let mut h = Harness::start_with(ServerConfig::default(), |db| {
        for instance in 2..=OBJECTS + untimed {
            let name = format!("AV-{instance}");
            db.add(Box::new(
                AnalogValueObject::new(instance, &name, EngineeringUnits::DEGREES_CELSIUS).unwrap(),
            ))
            .unwrap();
        }
    })
    .await;
    h.request_max_apdu = SMALL_APDU;
    let specs = (1..=OBJECTS + untimed)
        .map(|instance| (av(instance), vec![(PV, instance <= OBJECTS)]))
        .collect();
    h.subscribe_with_delay(confirmed, specs, delay).await;
    h
}

/// Notifications until `expected` PV rows have arrived, each checked and, when
/// confirmed, acknowledged before the next. Returns the notifications.
async fn take(
    h: &Harness,
    expected: usize,
    confirmed: bool,
) -> Vec<COVNotificationMultipleRequest> {
    take_as(h, expected, confirmed, true).await
}

/// The initial report, unchecked: a test examines its own report.
async fn take_initial(h: &Harness, expected: usize, confirmed: bool) {
    take_as(h, expected, confirmed, false).await;
}

async fn take_as(
    h: &Harness,
    expected: usize,
    confirmed: bool,
    checked: bool,
) -> Vec<COVNotificationMultipleRequest> {
    let mut taken = Vec::new();
    let mut rows = 0;
    while rows < expected {
        if confirmed && !taken.is_empty() {
            // One part outstanding at a time.
            h.no_notification().await;
            h.ack().await;
        }
        let notification = h.notification().await;
        if checked {
            check(&notification, confirmed);
        }
        rows += pv_rows_by_time(&notification).len();
        taken.push(notification);
    }
    if confirmed {
        h.ack().await;
        h.settle().await;
    }
    h.no_notification().await;
    taken
}

/// Queue one PV change of each object, in turn at seconds `first..`, behind
/// DISABLE_INITIATION; the last re-enables and reports them all. Returns
/// every change as its expected row, in capture order.
async fn hold_one_change_each(h: &Harness, objects: &[ObjectIdentifier], first: u8) -> Vec<Row> {
    h.server
        .comm_state
        .set_for_test(DccState::DisableInitiation);
    let mut expected = Vec::new();
    for (index, (at_second, &object)) in (first..).zip(objects).enumerate() {
        if index + 1 == objects.len() {
            h.server.comm_state.set_for_test(DccState::Enable);
        }
        h.set_clock(at_second);
        let value = f32::from(at_second);
        h.write_local_to(object, value).await;
        expected.push((object, real(value), Some(time(at_second))));
    }
    expected
}

/// Each notification carries a contiguous run of the changes, in capture
/// order, and every change arrives once.
fn assert_capture_order(taken: &[COVNotificationMultipleRequest], expected: &[Row]) {
    let conveyed: Vec<Row> = taken.iter().flat_map(pv_rows_by_time).collect();
    assert_eq!(
        conveyed, expected,
        "every change once, strictly oldest first"
    );
}

/// The PV value each timestamped reference of `object` last completed.
async fn completed(h: &Harness, object: ObjectIdentifier) -> Option<PropertyValue> {
    let mut table = h.server.cov_table.write().await;
    table
        .subscriptions_for(&object)
        .into_iter()
        .find(|sub| sub.timestamped)
        .and_then(|sub| sub.last_notified_observation.as_ref())
        .map(|observation| observation.sample().value().clone())
}

#[tokio::test(start_paused = true)]
async fn an_initial_report_whose_latest_changes_exceed_the_apdu_goes_out_in_several() {
    let mut h = many_object_harness(false, 0, 10).await;
    let taken = take(&h, OBJECTS as usize, false).await;
    assert!(taken.len() >= 2, "{} notifications", taken.len());
    let mut objects: Vec<_> = taken
        .iter()
        .flat_map(pv_rows_by_time)
        .map(|(object, value, time_of_change)| {
            assert_eq!((value, time_of_change), (real(0.0), Some(time(0))));
            object
        })
        .collect();
    objects.sort_by_key(|object| object.instance_number());
    assert_eq!(objects, timed_objects().collect::<Vec<_>>(), "each once");
    assert_eq!(h.server.cov_counters().timed_changes_dropped, 0);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn unconfirmed_latest_changes_beyond_one_apdu_go_out_in_capture_order() {
    let mut h = many_object_harness(false, 0, 10).await;
    take_initial(&h, OBJECTS as usize, false).await;
    let objects: Vec<_> = timed_objects().collect();
    let expected = hold_one_change_each(&h, &objects, 1).await;
    let taken = take(&h, expected.len(), false).await;
    assert_eq!(taken.len(), 2);
    assert_capture_order(&taken, &expected);
    // The last notification takes as many of the newest changes as fit.
    assert_eq!(pv_rows_by_time(&taken[1]).len(), 5);
    for (object, value, _) in &expected {
        assert_eq!(
            completed(&h, *object).await.map(|pv| real_of(&pv)),
            Some(value.clone())
        );
    }
    assert_eq!(h.server.cov_counters().timed_changes_dropped, 0);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn confirmed_latest_changes_beyond_one_apdu_go_out_part_by_part_in_capture_order() {
    let mut h = many_object_harness(true, 0, 10).await;
    take_initial(&h, OBJECTS as usize, true).await;
    let objects: Vec<_> = timed_objects().collect();
    let expected = hold_one_change_each(&h, &objects, 1).await;
    let mut taken = Vec::new();
    let mut conveyed = 0;
    while conveyed < expected.len() {
        let notification = h.notification().await;
        check(&notification, true);
        let rows = pv_rows_by_time(&notification);
        // Nothing more until this part's Ack, which completes the references
        // it carried and only those (#1008).
        h.no_notification().await;
        for (object, _, _) in &rows {
            assert_eq!(completed(&h, *object).await, Some(PropertyValue::Real(0.0)));
        }
        h.ack().await;
        h.settle().await;
        for (object, value, _) in &rows {
            assert_eq!(
                completed(&h, *object).await.map(|pv| real_of(&pv)),
                Some(value.clone())
            );
        }
        conveyed += rows.len();
        taken.push(notification);
    }
    assert!(taken.len() >= 2, "{} notifications", taken.len());
    assert_capture_order(&taken, &expected);
    h.no_notification().await;
    assert_eq!(h.server.cov_counters().timed_changes_dropped, 0);
    h.server.stop().await.unwrap();
}

fn real_of(value: &PropertyValue) -> Vec<u8> {
    let PropertyValue::Real(value) = value else {
        panic!("a REAL Present_Value, got {value:?}");
    };
    real(*value)
}

#[tokio::test(start_paused = true)]
async fn a_latest_change_older_than_another_references_history_goes_out_first() {
    let mut h = many_object_harness(false, 0, 10).await;
    take_initial(&h, OBJECTS as usize, false).await;
    // AV-2 changes once, then AV-1 nine times: AV-2's only change, its latest,
    // is the oldest of the report and leads it.
    let mut objects = vec![av(2)];
    objects.extend(std::iter::repeat_n(av(1), 9));
    let expected = hold_one_change_each(&h, &objects, 1).await;
    let taken = take(&h, expected.len(), false).await;
    assert!(taken.len() >= 2, "{} notifications", taken.len());
    assert_capture_order(&taken, &expected);
    assert_eq!(h.server.cov_counters().timed_changes_dropped, 0);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn untimestamped_values_go_last_with_the_newest_changes_that_still_fit() {
    let mut h = many_object_harness(false, 1, 10).await;
    let untimed = av(OBJECTS + 1);
    take_initial(&h, OBJECTS as usize + 1, false).await;
    // Every timestamped object changes behind DISABLE_INITIATION, then the
    // untimestamped one, which reports them all at once (§13.1).
    h.server
        .comm_state
        .set_for_test(DccState::DisableInitiation);
    let mut expected = Vec::new();
    for (at_second, object) in (1..).zip(timed_objects()) {
        h.set_clock(at_second);
        h.write_local_to(object, f32::from(at_second)).await;
        expected.push((object, real(f32::from(at_second)), Some(time(at_second))));
    }
    h.server.comm_state.set_for_test(DccState::Enable);
    h.set_clock(20);
    h.write_local_to(untimed, 7.0).await;
    expected.push((untimed, real(7.0), None));
    let taken = take(&h, expected.len(), false).await;
    let carries_untimed = |notification: &COVNotificationMultipleRequest| {
        notification
            .list_of_cov_notifications
            .iter()
            .any(|item| item.monitored_object_identifier == untimed)
    };
    let (last, earlier) = taken.split_last().unwrap();
    assert!(
        !earlier.is_empty(),
        "the timestamped changes need a part of their own"
    );
    assert!(earlier.iter().all(|n| !carries_untimed(n)));
    assert!(carries_untimed(last));
    assert!(
        pv_rows_by_time(last).len() > 1,
        "the newest changes that fit share the last notification"
    );
    // Untimed rows sort first in a notification; compare the timed ones.
    let conveyed: Vec<Row> = taken
        .iter()
        .flat_map(pv_rows_by_time)
        .filter(|(_, _, time)| time.is_some())
        .collect();
    assert_eq!(conveyed, expected[..expected.len() - 1]);
    assert_eq!(h.server.cov_counters().timed_changes_dropped, 0);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn untimestamped_values_too_large_for_one_apdu_follow_every_change_in_parts_that_fit() {
    // Nine untimestamped references take more than one notification on their
    // own. The initial report carries them all with the timestamped ones:
    // the timestamped changes go first, in parts that fit, and the
    // untimestamped values follow, split by object item into parts that fit
    // too (#1038).
    let untimed = 9;
    let mut h = many_object_harness(false, untimed, 10).await;
    let mut taken = Vec::new();
    let mut untimed_rows = 0;
    while untimed_rows < untimed as usize {
        let notification = h.notification().await;
        check(&notification, false);
        untimed_rows += pv_rows_by_time(&notification)
            .iter()
            .filter(|(object, _, _)| object.instance_number() > OBJECTS)
            .count();
        taken.push(notification);
    }
    h.no_notification().await;
    let carries_untimed = |notification: &COVNotificationMultipleRequest| {
        notification
            .list_of_cov_notifications
            .iter()
            .any(|item| item.monitored_object_identifier.instance_number() > OBJECTS)
    };
    let first_untimed = taken.iter().position(carries_untimed).unwrap();
    let (timed, untimed_parts) = taken.split_at(first_untimed);
    let timed_rows: usize = timed.iter().map(|n| pv_rows_by_time(n).len()).sum();
    assert_eq!(
        timed_rows, OBJECTS as usize,
        "every timestamped change first"
    );
    assert!(
        untimed_parts.len() >= 2,
        "{} untimestamped parts",
        untimed_parts.len()
    );
    let mut objects = Vec::new();
    for notification in untimed_parts {
        let rows = pv_rows_by_time(notification);
        assert!(
            rows.iter()
                .all(|(object, _, time)| { object.instance_number() > OBJECTS && time.is_none() })
        );
        assert_eq!(notification.timestamp, None, "nothing in it is timestamped");
        objects.extend(rows.into_iter().map(|(object, _, _)| object));
    }
    objects.sort_by_key(|object| object.instance_number());
    let expected: Vec<_> = (OBJECTS + 1..=OBJECTS + untimed).map(av).collect();
    assert_eq!(objects, expected, "each untimestamped value once");
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn a_reference_completes_when_the_part_carrying_its_latest_change_is_sent() {
    let mut h = many_object_harness(false, 0, 1).await;
    take_initial(&h, OBJECTS as usize, false).await;
    // The second part fails; the first was sent, and its references are done.
    h.fail_notification(1);
    let objects: Vec<_> = timed_objects().collect();
    let expected = hold_one_change_each(&h, &objects, 1).await;
    let first = h.notification().await;
    check(&first, false);
    h.no_notification().await;
    let sent = pv_rows_by_time(&first);
    assert!(sent.len() < expected.len(), "a first part");
    for (object, value, _) in &expected {
        let done = sent.iter().any(|(sent, _, _)| sent == object);
        let want = if done { value.clone() } else { real(0.0) };
        assert_eq!(
            completed(&h, *object).await.map(|pv| real_of(&pv)),
            Some(want)
        );
    }
    // The failed part goes out again at the Max_Notification_Delay deadline.
    let mut taken = vec![first];
    taken.extend(take(&h, expected.len() - sent.len(), false).await);
    assert_capture_order(&taken, &expected);
    for (object, value, _) in &expected {
        assert_eq!(
            completed(&h, *object).await.map(|pv| real_of(&pv)),
            Some(value.clone())
        );
    }
    assert_eq!(h.server.cov_counters().timed_changes_dropped, 0);
    h.server.stop().await.unwrap();
}

/// A harness whose one context subscribes CSV-1's Present_Value with
/// timestamps from a 206-octet subscriber, with the initial report taken
/// (and acknowledged, when confirmed), and a writer of CSV-1's value.
async fn string_harness(confirmed: bool) -> (Harness, ObjectIdentifier) {
    use bacnet_objects::value_types::CharacterStringValueObject;
    let csv = ObjectIdentifier::new(ObjectType::CHARACTERSTRING_VALUE, 1).unwrap();
    let mut h = Harness::start_with(ServerConfig::default(), |db| {
        db.add(Box::new(
            CharacterStringValueObject::new(1, "CSV-1").unwrap(),
        ))
        .unwrap();
    })
    .await;
    h.request_max_apdu = SMALL_APDU;
    h.subscribe_specs(confirmed, vec![(csv, vec![(PV, true)])])
        .await;
    h.notification().await;
    if confirmed {
        h.ack().await;
        h.settle().await;
    }
    (h, csv)
}

async fn write_string(h: &Harness, csv: ObjectIdentifier, text: String) {
    h.server
        .write_local(
            &csv,
            PV,
            None,
            PropertyValue::CharacterString(text),
            Some(8),
            crate::LocalCommandSource::ServerDevice,
        )
        .await
        .unwrap();
}

#[tokio::test(start_paused = true)]
async fn a_confirmed_latest_change_too_large_for_any_notification_never_stalls_the_context() {
    let (mut h, csv) = string_harness(true).await;
    // A 200-character value alone exceeds a 206-octet notification: no
    // report could ever carry it, so it is dropped and counted rather than
    // sent over the subscriber's limit and retried without end. The rest of
    // its change, the Status_Flags, goes out on its own (#1090).
    h.set_clock(1);
    write_string(&h, csv, "a".repeat(200)).await;
    let flags = h.notification().await;
    let carried: Vec<_> = flags.list_of_cov_notifications[0]
        .list_of_values
        .iter()
        .map(|value| (value.property_identifier, value.time_of_change))
        .collect();
    assert_eq!(carried, vec![(SF, Some(time(1)))]);
    assert_eq!(h.server.cov_counters().timed_changes_dropped, 1);
    h.ack().await;
    h.settle().await;
    h.no_notification().await;
    // The context is idle, so the next change reports at once.
    h.set_clock(2);
    write_string(&h, csv, "short".into()).await;
    let report = h.notification().await;
    assert!(apdu_len(&report, true) <= usize::from(SMALL_APDU));
    let rows: Vec<_> = report.list_of_cov_notifications[0]
        .list_of_values
        .iter()
        .filter(|value| value.property_identifier == PV)
        .map(|value| value.time_of_change)
        .collect();
    assert_eq!(rows, vec![Some(time(2))]);
    h.ack().await;
    h.settle().await;
    h.no_notification().await;
    assert_eq!(h.server.cov_counters().timed_changes_dropped, 1);
    h.server.stop().await.unwrap();
}