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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
use super::*;
use crate::server::test_transport::{BIP_LOCAL_MAC, SendLog, SendMode, TestTransport};
use bacnet_encoding::constructed::decode_event_notification;
use bacnet_objects::analog::AnalogInputObject;
use bacnet_objects::device::{DeviceConfig, DeviceObject};
use bacnet_objects::event::EventStateChange;
use bacnet_objects::traits::BACnetObject;
use bacnet_types::bitstring::{DaysOfWeek, EventTransitionBits};
use bacnet_types::enums::{EngineeringUnits, EventState, EventType};
use bytes::Bytes;

#[path = "event_notifications_commit_tests.rs"]
mod commit_tests;

#[path = "event_notifications_history_tests.rs"]
mod history_tests;

#[path = "event_message_policy_tests.rs"]
mod message_policy_tests;

#[path = "event_options_tests.rs"]
mod options_tests;

#[path = "event_notifications_priority_tests.rs"]
mod priority_tests;

/// A transport that records every broadcast NPDU it is asked to send and
/// discards unicasts. Used to capture the EventNotification a server
/// actually puts on the wire.
pub(super) fn recording_transport() -> (TestTransport, SendLog) {
    let transport = TestTransport::builder()
        .local_mac(&BIP_LOCAL_MAC)
        .unicast(SendMode::Ignore)
        .build();
    let sent = transport.sent();
    (transport, sent)
}

/// A server under DISABLE_INITIATION suppresses the periodic event
/// send: `build_and_send_event_notification` returns without sending,
/// matching the per-write path's DCC gate. Verified against a recording
/// transport that would otherwise capture the broadcast APDU.
#[tokio::test]
async fn dcc_suppresses_periodic_event_send() {
    let (transport, sent) = recording_transport();
    let network = Arc::new(NetworkLayer::new(transport));
    let comm_state = Arc::new(CommState::default());
    comm_state.set_for_test(DccState::DisableInitiation);
    let learned_routers = Arc::new(Mutex::new(LearnedRouterCache::new()));

    let mut db = clocked_test_database();
    db.add(Box::new(
        AnalogInputObject::new(1, "AI-1", EngineeringUnits::SQUARE_METERS).unwrap(),
    ))
    .unwrap();
    db.add(Box::new(
        DeviceObject::new(DeviceConfig {
            identity: bacnet_objects::DeviceIdentity::default().with_name("Dev"),
            ..DeviceConfig::default()
        })
        .unwrap(),
    ))
    .unwrap();
    // A recipient that WOULD be broadcast to, so the empty assertion below can
    // only be satisfied by the DCC gate. Without this the test passes because
    // no recipient was named, and it stays green even with the gate removed.
    db.add(Box::new(notification_class_0_broadcasting()))
        .unwrap();
    let db = Arc::new(tokio::sync::RwLock::new(db));
    let oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap();

    let change = EventStateChange {
        from: EventState::NORMAL,
        to: EventState::HIGH_LIMIT,
    };
    BACnetServer::<TestTransport>::build_and_send_event_notification_with_bindings(
        &crate::server::event_delivery::EventDelivery {
            db: &db,
            network: &network,
            comm_state: &comm_state,
            learned_routers: &learned_routers,
            notification_transactions: &NotificationTransactions::new(),
            device_bindings: &Arc::new(RwLock::new(
                crate::server::device_bindings::DeviceBindingTable::new(),
            )),
            suppressions: &Default::default(),
            retry_timeout_ms: 1000,
            local_apdu_capacity: 1476,
        },
        &oid,
        (change, EventType::OUT_OF_RANGE),
    )
    .await;

    assert!(
        sent.is_empty(),
        "DCC-disabled server must not send event notifications"
    );
}

/// Decode the single broadcast EventNotification captured by a
/// [`recording_transport`] into its [`EventNotificationRequest`].
///
/// Panics with a useful message if no notification was sent (so a regression
/// that silently drops the notification is caught rather than masking as
/// "no broadcast = pass").
pub(super) fn decode_broadcast_notification(sent: &[Bytes]) -> EventNotificationRequest {
    use bacnet_encoding::apdu::decode_apdu;
    use bacnet_encoding::npdu::decode_npdu;

    assert_eq!(
        sent.len(),
        1,
        "expected exactly one broadcast EventNotification, got {}",
        sent.len()
    );
    let npdu = decode_npdu(sent[0].clone()).expect("decode NPDU");
    match decode_apdu(npdu.payload).expect("decode APDU") {
        Apdu::UnconfirmedRequest(req) => {
            assert_eq!(
                req.service_choice,
                UnconfirmedServiceChoice::UNCONFIRMED_EVENT_NOTIFICATION
            );
            decode_event_notification(&req.service_request).expect("decode EventNotification")
        }
        other => panic!("expected UnconfirmedRequest, got {other:?}"),
    }
}

/// The `Recipient_List` entry Clause 12.21 prescribes for a device whose list
/// is not writable: a local broadcast (network 0, zero-length MAC), all days,
/// the full daily window, process identifier 0, unconfirmed, all transitions.
///
/// Notifications reach the recording transport's broadcast wire *because this
/// entry asks them to*. An empty `Recipient_List` names no notification-clients
/// and so distributes nothing (Clause 13.2.5).
pub(super) fn local_broadcast_destination() -> bacnet_types::constructed::BACnetDestination {
    use bacnet_types::constructed::{BACnetAddress, BACnetDestination, BACnetRecipient};
    use bacnet_types::primitives::Time;
    BACnetDestination {
        valid_days: DaysOfWeek::all(),
        from_time: Time {
            hour: 0,
            minute: 0,
            second: 0,
            hundredths: 0,
        },
        to_time: Time {
            hour: 23,
            minute: 59,
            second: 59,
            hundredths: 99,
        },
        recipient: BACnetRecipient::Address(BACnetAddress {
            network_number: 0,
            mac_address: MacAddr::new(),
        }),
        process_identifier: 0,
        issue_confirmed_notifications: false,
        transitions: EventTransitionBits::all(),
    }
}

/// Notification Class instance 0 — the class an object points at when its
/// `Notification_Class` property is left at its default — carrying the single
/// local-broadcast recipient from [`local_broadcast_destination`].
pub(super) fn notification_class_0_broadcasting()
-> bacnet_objects::notification_class::NotificationClass {
    let mut nc = bacnet_objects::notification_class::NotificationClass::new(0, "NC-0").unwrap();
    nc.add_destination(local_broadcast_destination()).unwrap();
    nc
}

/// Build a server fixture: a Device, a NotificationClass (instance `nc`, whose
/// recipient list holds the Clause 12.21 local-broadcast entry) with the given
/// per-transition `priority` and `ack_required` bits, and an AnalogInput whose
/// `Notification_Class` points at it with `Notify_Type = ALARM`.
async fn fixture_with_commanded_nc(
    nc: u32,
    priority: [u8; 3],
    ack_required: EventTransitionBits,
) -> (
    Arc<RwLock<ObjectDatabase>>,
    Arc<NetworkLayer<TestTransport>>,
    Arc<CommState>,
    Arc<Mutex<LearnedRouterCache>>,
    SendLog,
    ObjectIdentifier,
) {
    let (transport, sent) = recording_transport();
    let network = Arc::new(NetworkLayer::new(transport));
    let comm_state = Arc::new(CommState::default()); // DCC enabled
    let learned_routers = Arc::new(Mutex::new(LearnedRouterCache::new()));

    let mut db = clocked_test_database();
    // NotificationClass with the configured per-transition arrays and a single
    // local-broadcast recipient, so the notification lands on the broadcast wire.
    let mut notification_class =
        bacnet_objects::notification_class::NotificationClass::new(nc, "NC").unwrap();
    notification_class.priority = priority;
    notification_class.ack_required = ack_required;
    notification_class
        .add_destination(local_broadcast_destination())
        .unwrap();
    db.add(Box::new(notification_class)).unwrap();

    db.add(Box::new(
        DeviceObject::new(DeviceConfig {
            identity: bacnet_objects::DeviceIdentity::default().with_name("Dev"),
            ..DeviceConfig::default()
        })
        .unwrap(),
    ))
    .unwrap();

    // AnalogInput pointing at the NotificationClass, ALARM notify type.
    let mut ai = AnalogInputObject::new(1, "AI-1", EngineeringUnits::SQUARE_METERS).unwrap();
    ai.write_property(
        PropertyIdentifier::NOTIFICATION_CLASS,
        None,
        PropertyValue::Unsigned(nc as u64),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::NOTIFY_TYPE,
        None,
        PropertyValue::Enumerated(NotifyType::ALARM.to_raw()),
        None,
    )
    .unwrap();
    db.add(Box::new(ai)).unwrap();

    let db = Arc::new(RwLock::new(db));
    let oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap();
    (db, network, comm_state, learned_routers, sent, oid)
}

/// A `Notification_Class` naming an object that does not exist distributes
/// nothing.
///
/// The Standard does not say what a `Notification_Class` naming a nonexistent
/// object should do — Clause 13.2.5 governs distribution given a Recipient_List,
/// and Clause 12 defines the property without a missing-object rule. So this is
/// a deliberate choice for an undefined configuration, not a mandate.
///
/// Silence is the defensible reading: a class that does not exist supplies no
/// Recipient_List, and 13.2.5 restricts distribution to that input's
/// notification-clients. The alternative was the previous
/// behavior, where a misconfigured `Notification_Class` broadcast the alarm to
/// every device on the link.
#[tokio::test]
async fn event_notification_missing_class_distributes_nothing() {
    let (transport, sent) = recording_transport();
    let network = Arc::new(NetworkLayer::new(transport));
    let comm_state = Arc::new(CommState::default());
    let learned_routers = Arc::new(Mutex::new(LearnedRouterCache::new()));

    let mut db = clocked_test_database();
    db.add(Box::new(
        DeviceObject::new(DeviceConfig {
            identity: bacnet_objects::DeviceIdentity::default().with_name("Dev"),
            ..DeviceConfig::default()
        })
        .unwrap(),
    ))
    .unwrap();
    // AI points at NotificationClass 999, which does not exist.
    let mut ai = AnalogInputObject::new(1, "AI-1", EngineeringUnits::SQUARE_METERS).unwrap();
    ai.write_property(
        PropertyIdentifier::NOTIFICATION_CLASS,
        None,
        PropertyValue::Unsigned(999),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::NOTIFY_TYPE,
        None,
        PropertyValue::Enumerated(NotifyType::ALARM.to_raw()),
        None,
    )
    .unwrap();
    db.add(Box::new(ai)).unwrap();
    let db = Arc::new(RwLock::new(db));
    let oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap();

    let change = EventStateChange {
        from: EventState::NORMAL,
        to: EventState::HIGH_LIMIT,
    };
    BACnetServer::<TestTransport>::build_and_send_event_notification_with_bindings(
        &crate::server::event_delivery::EventDelivery {
            db: &db,
            network: &network,
            comm_state: &comm_state,
            learned_routers: &learned_routers,
            notification_transactions: &NotificationTransactions::new(),
            device_bindings: &Arc::new(RwLock::new(
                crate::server::device_bindings::DeviceBindingTable::new(),
            )),
            suppressions: &Default::default(),
            retry_timeout_ms: 1000,
            local_apdu_capacity: 1476,
        },
        &oid,
        (change, EventType::OUT_OF_RANGE),
    )
    .await;

    assert!(
        sent.is_empty(),
        "a Notification_Class that does not exist names no recipients, so \
         nothing may be distributed"
    );
}

/// Notify_Type = EVENT still honors the per-transition Ack_Required (it is
/// not ALARM-specific); the legacy code derived ack_required purely from
/// `Notify_Type == ALARM`, so an EVENT notification would wrongly clear it.
#[tokio::test]
async fn event_notification_event_notify_type_honors_class_ack_required() {
    let (db, network, comm_state, learned_routers, sent, oid) = fixture_with_commanded_nc(
        5,
        [50, 150, 250],
        EventTransitionBits::TO_OFFNORMAL | EventTransitionBits::TO_NORMAL,
    )
    .await;
    // Reconfigure the AI to Notify_Type = EVENT.
    {
        let mut guard = db.write().await;
        let ai = guard.get_mut(&oid).expect("AI present");
        ai.write_property(
            PropertyIdentifier::NOTIFY_TYPE,
            None,
            PropertyValue::Enumerated(NotifyType::EVENT.to_raw()),
            None,
        )
        .unwrap();
    }

    let change = EventStateChange {
        from: EventState::NORMAL,
        to: EventState::HIGH_LIMIT,
    };
    BACnetServer::<TestTransport>::build_and_send_event_notification_with_bindings(
        &crate::server::event_delivery::EventDelivery {
            db: &db,
            network: &network,
            comm_state: &comm_state,
            learned_routers: &learned_routers,
            notification_transactions: &NotificationTransactions::new(),
            device_bindings: &Arc::new(RwLock::new(
                crate::server::device_bindings::DeviceBindingTable::new(),
            )),
            suppressions: &Default::default(),
            retry_timeout_ms: 1000,
            local_apdu_capacity: 1476,
        },
        &oid,
        (change, EventType::OUT_OF_RANGE),
    )
    .await;

    let notif = decode_broadcast_notification(&sent.npdus());
    // ack_required is encoded for both ALARM and EVENT notify types; the
    // per-transition ACK_REQUIRED bit 0 (TO_OFFNORMAL) is true here.
    assert!(
        notif.ack_required,
        "EVENT notify type honors per-transition ACK_REQUIRED"
    );
    assert_eq!(notif.priority, 50);
}

/// Build a one-object database whose AnalogInput will transition
/// NORMAL -> HIGH_LIMIT on the next intrinsic evaluation, with `Event_Enable`
/// set from `event_enable_byte`.
///
/// `Event_Enable` is written through `write_property` rather than an internal
/// setter, so these tests cover the same path a network client takes. Bytes
/// are the Clause 20.2.10 wire encoding: MSB-first, TO_OFFNORMAL at `0x80`.
pub(super) fn db_with_high_limit_transition(
    event_enable_byte: u8,
) -> Arc<tokio::sync::RwLock<ObjectDatabase>> {
    let mut ai = AnalogInputObject::new(1, "AI-1", EngineeringUnits::DEGREES_CELSIUS).unwrap();
    for (p, v) in [
        (PropertyIdentifier::HIGH_LIMIT, 80.0f32),
        (PropertyIdentifier::LOW_LIMIT, 20.0),
        (PropertyIdentifier::DEADBAND, 2.0),
    ] {
        ai.write_property(p, None, PropertyValue::Real(v), None)
            .unwrap();
    }
    ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![0xC0], // low + high limit checking enabled
        },
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::EVENT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![event_enable_byte],
        },
        None,
    )
    .unwrap();
    ai.set_present_value(81.0); // above high_limit -> NORMAL -> HIGH_LIMIT

    let mut db = clocked_test_database();
    db.add(Box::new(ai)).unwrap();
    db.add(Box::new(
        DeviceObject::new(DeviceConfig {
            identity: bacnet_objects::DeviceIdentity::default().with_name("Dev"),
            ..DeviceConfig::default()
        })
        .unwrap(),
    ))
    .unwrap();
    // The AnalogInput's Notification_Class defaults to 0, so class 0 has to
    // exist and name a recipient for anything to be distributed.
    db.add(Box::new(notification_class_0_broadcasting()))
        .unwrap();
    Arc::new(tokio::sync::RwLock::new(db))
}

/// Drive the per-write path once and return the broadcasts it produced.
pub(super) async fn broadcasts_from_per_write_path(
    db: &Arc<tokio::sync::RwLock<ObjectDatabase>>,
    state: DccState,
) -> Vec<Bytes> {
    let (transport, sent) = recording_transport();
    let network = Arc::new(NetworkLayer::new(transport));
    let comm_state = Arc::new(CommState::default());
    comm_state.set_for_test(state);
    let learned_routers = Arc::new(Mutex::new(LearnedRouterCache::new()));
    let oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap();

    BACnetServer::<TestTransport>::fire_event_notifications_with_bindings(
        &crate::server::event_delivery::EventDelivery {
            db,
            network: &network,
            comm_state: &comm_state,
            learned_routers: &learned_routers,
            notification_transactions: &NotificationTransactions::new(),
            device_bindings: &Arc::new(RwLock::new(
                crate::server::device_bindings::DeviceBindingTable::new(),
            )),
            suppressions: &Default::default(),
            retry_timeout_ms: 1000,
            local_apdu_capacity: 1476,
        },
        &Arc::new(RwLock::new(crate::cov::CovSubscriptionTable::new())),
        &oid,
    )
    .await;

    sent.npdus()
}

/// A cleared `Event_Enable` bit must suppress the outbound notification.
///
/// This is the gate this whole change moved. Before #136 the detector returned
/// `None` for a suppressed transition, so nothing downstream *could* send;
/// now the detector reports the transition and only this send site declines to
/// distribute it (Clause 13.2.5). Without this test, deleting or inverting that
/// check is invisible to the suite.
#[tokio::test]
async fn event_enable_cleared_suppresses_per_write_send() {
    let db = db_with_high_limit_transition(0x00); // no transition distributable
    let sent = broadcasts_from_per_write_path(&db, DccState::Enable).await;

    assert!(
        sent.is_empty(),
        "Event_Enable with TO_OFFNORMAL clear must suppress the send, got {} broadcast(s)",
        sent.len()
    );

    // The transition itself still happened — suppression is distribution-only.
    let db = db.read().await;
    let oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap();
    assert_eq!(
        db.get(&oid)
            .unwrap()
            .read_property(PropertyIdentifier::EVENT_STATE, None)
            .unwrap(),
        PropertyValue::Enumerated(EventState::HIGH_LIMIT.to_raw()),
        "Event_State advances even though the notification was suppressed"
    );
}

/// The periodic `Time_Delay` path has its own `Event_Enable` gate, and it needs
/// its own test: the per-write tests above cannot reach it, because a nonzero
/// `Time_Delay` makes the per-write probe return `None` by design.
///
/// Drives the real spawned `intrinsic_reporting_task` on a paused clock: a
/// local write seeds a pending transition, the clock advances past the delay,
/// the task ticks and fires it — with TO_OFFNORMAL cleared, so nothing may go
/// out. Proven to fail when the `outcome.distribute` check at that site is
/// replaced with `if true`.
#[tokio::test(start_paused = true)]
async fn event_enable_cleared_suppresses_periodic_time_delay_send() {
    let (transport, sent) = recording_transport();

    let mut ai = AnalogInputObject::new(1, "AI-1", EngineeringUnits::DEGREES_CELSIUS).unwrap();
    for (p, v) in [
        (PropertyIdentifier::HIGH_LIMIT, 80.0f32),
        (PropertyIdentifier::LOW_LIMIT, 20.0),
        (PropertyIdentifier::DEADBAND, 2.0),
    ] {
        ai.write_property(p, None, PropertyValue::Real(v), None)
            .unwrap();
    }
    ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![0xC0],
        },
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::EVENT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0x00], // nothing distributable
        },
        None,
    )
    .unwrap();
    // Nonzero Time_Delay: the per-write probe only seeds; the periodic task fires.
    ai.write_property(
        PropertyIdentifier::TIME_DELAY,
        None,
        PropertyValue::Unsigned(2),
        None,
    )
    .unwrap();
    ai.set_present_value(81.0); // already above high_limit
    let oid = ai.object_identifier();

    let mut db = clocked_test_database();
    db.add(Box::new(ai)).unwrap();
    db.add(Box::new(
        DeviceObject::new(DeviceConfig {
            identity: bacnet_objects::DeviceIdentity::default().with_name("Dev"),
            ..DeviceConfig::default()
        })
        .unwrap(),
    ))
    .unwrap();
    // A recipient that WOULD be broadcast to, so both assertions below can only
    // be satisfied by the Event_Enable gate rather than by an unnamed recipient.
    db.add(Box::new(notification_class_0_broadcasting()))
        .unwrap();

    let server = BACnetServer::start(ServerConfig::default(), db, transport)
        .await
        .expect("server should start");

    // Any local write runs the post-write trigger path, which probes the
    // detector and seeds the pending transition without sending.
    server
        .write_local(
            &oid,
            PropertyIdentifier::DEADBAND,
            None,
            PropertyValue::Real(2.0),
            None,
            crate::LocalCommandSource::ServerDevice,
        )
        .await
        .expect("local write should succeed");
    assert!(
        sent.is_empty(),
        "a nonzero Time_Delay must not send on the write itself"
    );

    // Past the delay: the periodic task ticks the countdown to zero and fires.
    tokio::time::sleep(Duration::from_secs(5)).await;

    assert!(
        sent.is_empty(),
        "Event_Enable cleared: the periodic Time_Delay path must not send, got {} broadcast(s)",
        sent.len()
    );

    // The transition did fire internally — only distribution was withheld.
    let db_guard = server.database().read().await;
    assert_eq!(
        db_guard
            .get(&oid)
            .unwrap()
            .read_property(PropertyIdentifier::EVENT_STATE, None)
            .unwrap(),
        PropertyValue::Enumerated(EventState::HIGH_LIMIT.to_raw()),
        "the delayed transition must still have been confirmed internally"
    );
}

#[tokio::test(start_paused = true)]
async fn periodic_time_delay_carries_detector_event_type_to_wire() {
    let (transport, sent) = recording_transport();
    let mut ai = AnalogInputObject::new(1, "AI-1", EngineeringUnits::DEGREES_CELSIUS).unwrap();
    for (property, value) in [
        (PropertyIdentifier::HIGH_LIMIT, 80.0),
        (PropertyIdentifier::LOW_LIMIT, 20.0),
        (PropertyIdentifier::DEADBAND, 2.0),
    ] {
        ai.write_property(property, None, PropertyValue::Real(value), None)
            .unwrap();
    }
    ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![0xC0],
        },
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::EVENT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0x80], // TO_OFFNORMAL at wire bit 0
        },
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::TIME_DELAY,
        None,
        PropertyValue::Unsigned(2),
        None,
    )
    .unwrap();
    ai.set_present_value(81.0);
    let oid = ai.object_identifier();

    let mut db = clocked_test_database();
    db.add(Box::new(ai)).unwrap();
    db.add(Box::new(
        DeviceObject::new(DeviceConfig {
            identity: bacnet_objects::DeviceIdentity::default().with_name("Dev"),
            ..DeviceConfig::default()
        })
        .unwrap(),
    ))
    .unwrap();
    db.add(Box::new(notification_class_0_broadcasting()))
        .unwrap();
    let server = BACnetServer::start(ServerConfig::default(), db, transport)
        .await
        .expect("server should start");
    server
        .write_local(
            &oid,
            PropertyIdentifier::DEADBAND,
            None,
            PropertyValue::Real(2.0),
            None,
            crate::LocalCommandSource::ServerDevice,
        )
        .await
        .expect("local write should seed delayed transition");
    tokio::time::sleep(Duration::from_secs(5)).await;

    let notif = decode_broadcast_notification(&sent.npdus());
    assert_eq!(
        notif.event_type,
        EventType::OUT_OF_RANGE,
        "the periodic path must preserve the detector's OUT_OF_RANGE type"
    );
}