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
//! Max_Notification_Delay bounds how long timestamped changes stay queued
//! (135-2020 §13.1, §13.16.1.1.4; #856, part 2).
//!
//! Changes are reported as soon as they happen. These tests cover changes
//! that are still queued afterwards, because a send failed, a confirmed report
//! went unanswered or DISABLE_INITIATION held them, and nothing else changes:
//! they go out once the delay, measured from the earliest of them, has passed.
//! Time is paused, so Tokio jumps to each deadline while the test waits.
use super::cov_wire_test_support::*;
use super::*;
use tokio::time::Instant as TokioInstant;

const DELAY: u32 = 10;

/// Wait until just before `DELAY` seconds after `since`; nothing goes out yet.
async fn nothing_before_the_deadline(h: &Harness, since: TokioInstant) {
    tokio::time::sleep_until(
        since + Duration::from_secs(u64::from(DELAY)) - Duration::from_millis(100),
    )
    .await;
    h.no_notification().await;
}

/// The next notification, and that it went out at the deadline.
async fn notification_at_the_deadline(
    h: &Harness,
    since: TokioInstant,
) -> bacnet_services::cov_multiple::COVNotificationMultipleRequest {
    let report = h.notification().await;
    let elapsed = since.elapsed();
    let delay = Duration::from_secs(u64::from(DELAY));
    assert!(
        elapsed >= delay && elapsed < delay + Duration::from_secs(1),
        "sent {elapsed:?} after the earliest change, not at the {delay:?} deadline"
    );
    report
}

#[tokio::test(start_paused = true)]
async fn failed_sends_are_retried_at_the_delay_from_the_earliest_change() {
    let mut h = Harness::start(ServerConfig::default()).await;
    h.subscribe_with_delay(false, vec![(av1(), vec![(PV, true)])], DELAY)
        .await;
    h.notification().await;
    h.fail_notifications.store(true, Ordering::Release);
    h.set_clock(21);
    let earliest = TokioInstant::now();
    h.write_local(5.0).await;
    tokio::time::sleep(Duration::from_secs(4)).await;
    h.set_clock(25);
    h.write_local(6.0).await;
    h.fail_notifications.store(false, Ordering::Release);
    nothing_before_the_deadline(&h, earliest).await;
    let report = notification_at_the_deadline(&h, earliest).await;
    assert_eq!(
        pv_rows(&report),
        vec![(real(5.0), Some(time(21))), (real(6.0), Some(time(25)))]
    );
    // Delivered: nothing is left for a later deadline.
    tokio::time::sleep(Duration::from_secs(u64::from(DELAY) * 3)).await;
    h.no_notification().await;
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn changes_held_by_disable_initiation_go_out_at_the_deadline() {
    let mut h = Harness::start(ServerConfig::default()).await;
    h.subscribe_with_delay(false, vec![(av1(), vec![(PV, true)])], DELAY)
        .await;
    h.notification().await;
    h.server
        .comm_state
        .set_for_test(DccState::DisableInitiation);
    h.set_clock(11);
    let earliest = TokioInstant::now();
    h.write_local(10.0).await;
    tokio::time::sleep(Duration::from_secs(2)).await;
    h.server.comm_state.set_for_test(DccState::Enable);
    nothing_before_the_deadline(&h, earliest).await;
    let report = notification_at_the_deadline(&h, earliest).await;
    assert_eq!(pv_rows(&report), vec![(real(10.0), Some(time(11)))]);
    h.server.stop().await.unwrap();
}

/// Hold-off after a failed confirmed report with a 10 ms retry timeout.
const HOLD_OFF: Duration = Duration::from_millis(10 * (DEFAULT_APDU_RETRIES as u64 + 1));

#[tokio::test(start_paused = true)]
async fn an_unacknowledged_confirmed_report_is_resent_at_the_deadline() {
    let mut h = Harness::start(ServerConfig {
        cov_retry_timeout_ms: 10,
        ..ServerConfig::default()
    })
    .await;
    h.subscribe_with_delay(true, vec![(av1(), vec![(PV, true)])], DELAY)
        .await;
    h.notification().await;
    h.ack().await;
    h.settle().await;
    h.set_clock(45);
    let earliest = TokioInstant::now();
    h.write_local(5.0).await;
    h.notification().await;
    // Never acknowledged: the retries run out and the context holds off.
    h.workers_idle().await;
    tokio::time::sleep(HOLD_OFF).await;
    nothing_before_the_deadline(&h, earliest).await;
    let report = notification_at_the_deadline(&h, earliest).await;
    assert_eq!(pv_rows(&report), vec![(real(5.0), Some(time(45)))]);
    h.ack().await;
    h.settle().await;
    tokio::time::sleep(Duration::from_secs(u64::from(DELAY) * 3)).await;
    h.no_notification().await;
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn a_delivered_change_owes_no_deadline_notification() {
    let mut h = Harness::start(ServerConfig::default()).await;
    h.subscribe_with_delay(false, vec![(av1(), vec![(PV, true)])], DELAY)
        .await;
    h.notification().await;
    h.set_clock(30);
    h.write_local(3.0).await;
    assert_eq!(
        pv_rows(&h.notification().await),
        vec![(real(3.0), Some(time(30)))]
    );
    tokio::time::sleep(Duration::from_secs(u64::from(DELAY) * 3)).await;
    h.no_notification().await;
    h.server.stop().await.unwrap();
}

fn av1_timed() -> Vec<(ObjectIdentifier, Vec<(PropertyIdentifier, bool)>)> {
    vec![(av1(), vec![(PV, true)])]
}

/// Timestamped histories and backstop waits the server still holds.
async fn held(h: &Harness) -> (usize, usize) {
    h.server.cov_table.read().await.timed().lock().held()
}

/// Assert that a notification taken now went out less than a second after
/// `since`.
fn promptly(since: TokioInstant, what: &str) {
    let elapsed = since.elapsed();
    assert!(
        elapsed < Duration::from_secs(1),
        "{what}: sent {elapsed:?} later"
    );
}

#[tokio::test(start_paused = true)]
async fn cancelling_with_a_deadline_pending_sends_nothing_and_holds_nothing() {
    let mut h = Harness::start(ServerConfig::default()).await;
    h.subscribe_with_delay(false, av1_timed(), DELAY).await;
    h.notification().await;
    h.server
        .comm_state
        .set_for_test(DccState::DisableInitiation);
    h.set_clock(12);
    h.write_local(10.0).await;
    h.cancel_specs(false, av1_timed()).await;
    h.server.comm_state.set_for_test(DccState::Enable);
    tokio::time::sleep(Duration::from_secs(u64::from(DELAY) * 3)).await;
    h.no_notification().await;
    assert_eq!(held(&h).await, (0, 0));
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn expiry_with_a_deadline_pending_sends_nothing_and_holds_nothing() {
    let mut h = Harness::start(ServerConfig::default()).await;
    h.subscribe_with_delay(false, av1_timed(), DELAY).await;
    h.notification().await;
    h.server
        .comm_state
        .set_for_test(DccState::DisableInitiation);
    h.set_clock(13);
    h.write_local(10.0).await;
    h.server.cov_table.write().await.expire_all_for_test();
    h.server.comm_state.set_for_test(DccState::Enable);
    tokio::time::sleep(Duration::from_secs(u64::from(DELAY) * 3)).await;
    h.no_notification().await;
    h.server.cov_table.write().await.purge_expired();
    assert_eq!(held(&h).await, (0, 0));
    h.server.stop().await.unwrap();
}

fn permissive_dcc() -> ServerConfig {
    ServerConfig {
        dcc_policy: crate::server::DccPolicy::LegacyPermissive,
        ..ServerConfig::default()
    }
}

#[tokio::test(start_paused = true)]
async fn enabling_communication_after_the_deadline_sends_at_once() {
    use bacnet_types::enums::EnableDisable;
    let mut h = Harness::start(permissive_dcc()).await;
    h.subscribe_with_delay(false, av1_timed(), DELAY).await;
    h.notification().await;
    h.dcc(EnableDisable::DISABLE_INITIATION, None).await;
    h.settle().await;
    h.set_clock(14);
    let earliest = TokioInstant::now();
    h.write_local(10.0).await;
    // The deadline passes while initiation is disabled; that attempt is
    // blocked and the next would wait another delay.
    tokio::time::sleep_until(earliest + Duration::from_secs(u64::from(DELAY) + 2)).await;
    h.no_notification().await;
    let enabled = TokioInstant::now();
    h.dcc(EnableDisable::ENABLE, None).await;
    let report = h.notification().await;
    promptly(enabled, "after ENABLE");
    assert_eq!(pv_rows(&report), vec![(real(10.0), Some(time(14)))]);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn the_dcc_timer_reenabling_communication_sends_at_once() {
    use bacnet_types::enums::EnableDisable;
    const SLOW: u32 = 25;
    let mut h = Harness::start(permissive_dcc()).await;
    h.subscribe_with_delay(false, av1_timed(), SLOW).await;
    h.notification().await;
    let disabled = TokioInstant::now();
    h.dcc(EnableDisable::DISABLE_INITIATION, Some(1)).await;
    h.settle().await;
    h.set_clock(15);
    h.write_local(10.0).await;
    // Blocked attempts at 25 s and 50 s; the next would come at 75 s.
    let expiry = disabled + Duration::from_secs(60);
    tokio::time::sleep_until(expiry - Duration::from_millis(100)).await;
    h.no_notification().await;
    let report = h.notification().await;
    let elapsed = disabled.elapsed();
    assert!(
        elapsed >= Duration::from_secs(60) && elapsed < Duration::from_secs(61),
        "sent {elapsed:?} after DISABLE_INITIATION for one minute"
    );
    assert_eq!(pv_rows(&report), vec![(real(10.0), Some(time(15)))]);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn reinitialize_restart_resumes_overdue_cov_without_another_write() {
    use bacnet_services::device_mgmt::ReinitializeDeviceRequest;
    use bacnet_types::enums::{EnableDisable, ReinitializedState};
    for state in [ReinitializedState::WARMSTART, ReinitializedState::COLDSTART] {
        let mut config = permissive_dcc();
        config.on_reinitialize = Some(Arc::new(|_, _| Ok(())));
        let mut h = Harness::start(config).await;
        h.subscribe_with_delay(false, av1_timed(), DELAY).await;
        h.notification().await;
        h.dcc(EnableDisable::DISABLE_INITIATION, None).await;
        h.settle().await;
        h.set_clock(14);
        let earliest = TokioInstant::now();
        h.write_local(10.0).await;
        tokio::time::sleep_until(earliest + Duration::from_secs(u64::from(DELAY) + 2)).await;
        h.no_notification().await;
        let mut data = BytesMut::new();
        ReinitializeDeviceRequest {
            reinitialized_state: state,
            password: None,
        }
        .encode(&mut data)
        .unwrap();
        let accepted = TokioInstant::now();
        h.request(ConfirmedServiceChoice::REINITIALIZE_DEVICE, data)
            .await;
        let report = h.notification().await;
        promptly(accepted, "after accepted restart");
        assert_eq!(pv_rows(&report), vec![(real(10.0), Some(time(14)))]);
        h.server.stop().await.unwrap();
    }
}

/// A confirmed report that DISABLE_INITIATION ends at its first retry (#1327)
/// returns its history to the queue with no hold-off: once communication is
/// enabled again the overdue change goes out at once, its time kept.
#[tokio::test(start_paused = true)]
async fn history_of_a_report_dcc_ends_at_a_retry_goes_out_once_enabled() {
    use bacnet_types::enums::EnableDisable;
    let mut h = Harness::start(permissive_dcc()).await;
    h.subscribe_with_delay(true, av1_timed(), DELAY).await;
    h.notification().await;
    h.ack().await;
    h.settle().await;
    h.set_clock(19);
    let changed = TokioInstant::now();
    h.write_local(1.0).await;
    h.notification().await;
    let (invoke_id, _) = h.take_confirmed();
    h.dcc(EnableDisable::DISABLE_INITIATION, None).await;
    h.workers_idle().await;
    let ended = changed.elapsed();
    assert!(
        (Duration::from_millis(2_900)..Duration::from_millis(3_050)).contains(&ended),
        "lease freed {ended:?} after the report"
    );
    // The deadline passes while initiation is disabled; nothing goes out,
    // not even a retry of the withdrawn report.
    tokio::time::sleep_until(changed + Duration::from_secs(u64::from(DELAY) + 2)).await;
    h.no_notification().await;
    assert!(!h.frames.lock().unwrap().iter().any(
        |apdu| matches!(apdu, Apdu::ConfirmedRequest(request) if request.invoke_id == invoke_id)
    ));
    let enabled = TokioInstant::now();
    h.dcc(EnableDisable::ENABLE, None).await;
    let report = h.notification().await;
    promptly(enabled, "after ENABLE");
    assert_eq!(pv_rows(&report), vec![(real(1.0), Some(time(19)))]);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn a_report_outstanding_at_the_deadline_leaves_the_follow_up_to_its_ack() {
    let mut h = Harness::start(ServerConfig {
        cov_retry_timeout_ms: 30_000,
        ..ServerConfig::default()
    })
    .await;
    h.subscribe_with_delay(true, av1_timed(), DELAY).await;
    h.notification().await;
    h.ack().await;
    h.settle().await;
    h.set_clock(16);
    h.write_local(1.0).await;
    h.notification().await; // outstanding, not yet acknowledged
    tokio::time::sleep(Duration::from_secs(1)).await;
    h.set_clock(17);
    let held_change = TokioInstant::now();
    h.write_local(2.0).await;
    // Its deadline passes while the first report is still outstanding.
    tokio::time::sleep_until(held_change + Duration::from_secs(u64::from(DELAY) + 4)).await;
    h.no_notification().await;
    let acked = TokioInstant::now();
    h.ack().await;
    let report = h.notification().await;
    promptly(acked, "after the Ack");
    assert_eq!(pv_rows(&report), vec![(real(2.0), Some(time(17)))]);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn a_confirmed_hold_off_moves_the_retry_to_its_end() {
    const SLOW: u32 = 30;
    let mut h = Harness::start(ServerConfig {
        cov_retry_timeout_ms: 5_000,
        ..ServerConfig::default()
    })
    .await;
    h.subscribe_with_delay(true, av1_timed(), SLOW).await;
    h.notification().await;
    h.ack().await;
    h.settle().await;
    h.set_clock(18);
    let earliest = TokioInstant::now();
    h.write_local(1.0).await;
    h.notification().await;
    // Four unanswered attempts end at 20 s, and the hold-off lasts another
    // 20 s. The deadline (30 s) falls inside it, so the retry waits for its
    // end, not for another delay.
    let hold_off = Duration::from_millis(5_000 * (DEFAULT_APDU_RETRIES as u64 + 1));
    tokio::time::sleep_until(earliest + hold_off * 2 - Duration::from_millis(100)).await;
    h.no_notification().await;
    let report = h.notification().await;
    let elapsed = earliest.elapsed();
    assert!(
        elapsed >= hold_off * 2 && elapsed < hold_off * 2 + Duration::from_secs(1),
        "resent {elapsed:?} after the change, not when the hold-off ended"
    );
    assert_eq!(pv_rows(&report), vec![(real(1.0), Some(time(18)))]);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn a_failing_context_is_retried_once_per_delay() {
    const FAST: u32 = 2;
    let mut h = Harness::start(ServerConfig::default()).await;
    h.subscribe_with_delay(false, av1_timed(), FAST).await;
    h.notification().await;
    let attempts = |h: &Harness| h.server.cov_counters().notifications_sent;
    let before = attempts(&h);
    h.fail_notifications.store(true, Ordering::Release);
    h.set_clock(19);
    let earliest = TokioInstant::now();
    h.write_local(5.0).await;
    for (at_ms, expected) in [(1_900, 1), (2_100, 2), (3_900, 2), (4_100, 3), (6_100, 4)] {
        tokio::time::sleep_until(earliest + Duration::from_millis(at_ms)).await;
        assert_eq!(attempts(&h) - before, expected, "attempts by {at_ms} ms");
    }
    h.fail_notifications.store(false, Ordering::Release);
    let report = h.notification().await;
    assert_eq!(attempts(&h) - before, 5);
    assert_eq!(pv_rows(&report), vec![(real(5.0), Some(time(19)))]);
    h.server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn a_renewal_with_a_shorter_delay_brings_the_deadline_forward() {
    let mut h = Harness::start(ServerConfig::default()).await;
    h.subscribe_with_delay(false, av1_timed(), 30).await;
    h.notification().await;
    h.server
        .comm_state
        .set_for_test(DccState::DisableInitiation);
    h.set_clock(20);
    let earliest = TokioInstant::now();
    h.write_local(10.0).await;
    tokio::time::sleep(Duration::from_secs(1)).await;
    h.server.comm_state.set_for_test(DccState::Enable);
    // A lifetime renewal listing no references changes the context's delay.
    h.subscribe_with_delay(false, Vec::new(), 5).await;
    let report = h.notification().await;
    let elapsed = earliest.elapsed();
    assert!(
        elapsed >= Duration::from_secs(5) && elapsed < Duration::from_secs(6),
        "sent {elapsed:?} after the change, not at the renewed 5 s deadline"
    );
    assert_eq!(pv_rows(&report), vec![(real(10.0), Some(time(20)))]);
    h.server.stop().await.unwrap();
}