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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
//! BACnetServer: builder, APDU dispatch, and lifecycle management.
//!
//! The server wraps a NetworkLayer behind Arc (shared with the dispatch task),
//! owns an ObjectDatabase via `Arc<RwLock>`, and spawns a dispatch task that
//! routes incoming APDUs to service handlers.

use std::collections::HashMap;
use std::net::Ipv4Addr;
use std::sync::Arc;
#[cfg(test)]
pub(crate) use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Instant;

use bytes::{Bytes, BytesMut};
use tokio::sync::{Mutex, RwLock, Semaphore, mpsc, oneshot, watch};
use tokio::task::JoinHandle;
use tokio::time::Duration;
use tracing::{debug, warn};

use event_delivery::EventDelivery;

use bacnet_encoding::apdu::{
    self, AbortPdu, Apdu, ComplexAck, ConfirmedRequest as ConfirmedRequestPdu, ErrorPdu, RejectPdu,
    SegmentAck as SegmentAckPdu, SimpleAck, UnconfirmedRequest as UnconfirmedRequestPdu,
    encode_apdu,
};
use bacnet_encoding::npdu::NpduAddress;
use bacnet_encoding::primitives::encode_property_value;
use bacnet_encoding::segmentation::{
    SegmentReceiver, SegmentedPduType, duplicate_in_window, max_segment_payload, split_payload,
};
use bacnet_network::layer::NetworkLayer;
use bacnet_npdu::NetworkPriority;
use bacnet_objects::database::ObjectDatabase;
use bacnet_objects::notification_class::{
    lookup_notification_recipients, resolve_transition_priority_ack,
};
use bacnet_services::alarm_event::EventNotificationRequest;
use bacnet_services::common::BACnetPropertyValue;
use bacnet_services::cov::COVNotificationRequest;
use bacnet_services::cov_multiple::COVNotificationMultipleRequest;
use bacnet_services::who_is::{DeviceInstanceRange, IAmRequest, WhoIsRequest};
use bacnet_transport::any::AnyTransport;
use bacnet_transport::bip::{BipMode, BipMulticast, BipTransport};
use bacnet_transport::port::TransportPort;
use bacnet_transport::warn_throttle::WarnThrottle;
use bacnet_transport::warn_throttled;
use bacnet_types::MacAddr;
use bacnet_types::enums::{
    AbortReason, ConfirmedServiceChoice, ErrorClass, ErrorCode, LifeSafetyOperation, NotifyType,
    ObjectType, PropertyIdentifier, RejectReason, Segmentation, UnconfirmedServiceChoice,
};
use bacnet_types::error::{Error, RemoteError};
use bacnet_types::primitives::{ObjectIdentifier, PropertyValue};

use crate::audit_notification::{
    AuditNotificationAuthorizationContext, MAX_AUDIT_NOTIFICATION_BYTES, MAX_AUDIT_NOTIFICATIONS,
    UnconfirmedAuditNotificationAuthorizationContext,
};
pub use crate::cov::{CovCounters, CovPolicy};
use crate::cov::{
    CovNotificationKind, CovSubscription, CovSubscriptionSnapshot, CovSubscriptionTable,
};
use crate::life_safety::LifeSafetyOperationAuthorizationContext;
use crate::{handlers, runtime_clock};
use confirmed_request_tracker::{
    ConfirmedRequestAdmission, ConfirmedRequestTracker, PendingConfirmedRequest,
};
pub use device_bindings::DeviceBinding;
use device_bindings::{DeviceBindingTable, register_configured_binding};
use learned_router_cache::LearnedRouterCache;
use lso_replay::{LsoAdmission, PendingLsoReplay};
use notification_transactions::{
    InitiationRestricted, NotificationTransactions, NotificationWorkerResult,
    canonical_direct_peer, canonical_routed_peer, run_notification_under_dcc,
    run_notification_worker,
};
use request_services::{DispatchContext, RequestOrigin, RequestServices, UnconfirmedServices};
use requests::ConfirmedRequestOwnership;
use requests::confirmed_response::ResponseTarget;
use segmentation::{ComplexAckParams, SegmentedSendResources};

/// Maximum number of concurrent segmented reassembly sessions.
const MAX_SEG_RECEIVERS: usize = 128;

/// Hard per-request reassembly ceiling: the sequence-number space (#364).
///
/// A local storage bound, not a protocol one. Clause 20.1.2.7 makes the
/// request sequence number modulo 256, so a longer request is entirely
/// representable on the wire — this server simply keys its segment store by
/// that `u8` and cannot tell segment 256 from segment 0. A Device object
/// configured to receive segments does publish a tighter advertisement — its
/// `Max_Segments_Accepted` (Clause 12.11) defaults to `Unsigned(65)` — but
/// enforcing it here is deliberately not done: accepting more segments than
/// advertised is permissive, not a violation, while the sequence space is the
/// line past which acceptance silently corrupts. Exactly 256 segments
/// reassemble correctly and must keep working; 257 is the first that would
/// corrupt the payload.
const MAX_REQUEST_SEGMENTS: usize = 256;

/// Maximum number of concurrent segmented response send sessions.
const MAX_SEG_SENDERS: usize = 128;

/// Maximum negative SegmentAck retries during segmented response send.
const MAX_NEG_SEGMENT_ACK_RETRIES: u8 = 3;

/// Default timeout while waiting for SegmentACK during segmented response send.
const DEFAULT_APDU_SEGMENT_TIMEOUT: Duration = Duration::from_secs(5);

/// Default retransmission budget for segmented response segments.
const DEFAULT_APDU_SEGMENT_RETRIES: u8 = MAX_NEG_SEGMENT_ACK_RETRIES;

/// Default number of APDU retries for confirmed notifications and the
/// WriteProperty requests Command actions send to other devices.
const DEFAULT_APDU_RETRIES: u8 = 3;

/// Data from a TimeSynchronization request.
#[derive(Debug, Clone)]
pub struct TimeSyncData {
    /// Raw service request bytes (caller can decode if needed).
    pub raw_service_data: Bytes,
    /// Whether this was a UTC time sync (vs. local).
    pub is_utc: bool,
    /// Transport-native source MAC; on SC this is the source VMAC, not a
    /// certificate principal. This metadata is a claimed identity only.
    pub source_mac: MacAddr,
    /// Claimed routed NPDU source, if present; takes precedence for policy matching.
    pub source_network: Option<NpduAddress>,
    /// Honest transport + origin provenance, threaded for diagnostics only
    /// (RB-07 compat mode: decisions unchanged; RB-09 consumes it later).
    pub provenance: bacnet_transport::port::TransportProvenance,
}

mod config;
pub use config::ServerConfig;
mod audit_batch_queue;
mod audit_batch_runtime;
mod audit_context_preparation;
mod audit_forwarder;
#[cfg(test)]
mod audit_forwarder_tests;
mod audit_recipient;
mod audit_recipient_routes;
mod audit_reporter;
mod audit_reporter_changes;
mod audit_send_now;
mod heap_futures;
pub use audit_reporter::{AuditReportersConfig, valid_bip_audit_address};
#[cfg(test)]
mod audit_reporter_tests;

/// Generic builder for BACnetServer with a pre-built transport. The
/// transport type defaults to [`AnyTransport`], as [`BACnetServer`]'s does.
pub struct ServerBuilder<T: TransportPort = AnyTransport> {
    config: ServerConfig,
    db: Option<ObjectDatabase>,
    transport: Option<T>,
    configured_device_bindings: Vec<DeviceBinding>,
}

impl<T: TransportPort + 'static> ServerBuilder<T> {
    /// Set the one Device identity. With no supplied database, startup creates
    /// a stock Device after resolving the owned transport's receive capacity.
    pub fn device(mut self, identity: bacnet_objects::DeviceIdentity) -> Self {
        self.config.device = identity;
        self
    }

    /// Set the object database (transfers ownership).
    pub fn database(mut self, db: ObjectDatabase) -> Self {
        self.db = Some(db);
        self
    }

    /// Set the pre-built transport.
    pub fn transport(mut self, transport: T) -> Self {
        self.transport = Some(transport);
        self
    }

    /// Register one explicit unicast route for a Device recipient.
    pub fn device_binding(mut self, binding: DeviceBinding) -> Result<Self, Error> {
        register_configured_binding(&mut self.configured_device_bindings, binding)?;
        Ok(self)
    }

    /// Select the only local Audit Log that receives authorized notifications.
    pub fn audit_notification_sink(mut self, sink: ObjectIdentifier) -> Self {
        self.config.audit_notification_sink = Some(sink);
        self
    }

    /// Enable periodic fault detection / reliability evaluation.
    ///
    /// When enabled, every object's opt-in reliability hook runs every 10
    /// seconds; the default hook is a no-op.
    ///
    /// Reliability evaluation only; Event Enrollment evaluation is configured
    /// by [`enable_event_enrollment`](Self::enable_event_enrollment).
    pub fn enable_fault_detection(mut self, enabled: bool) -> Self {
        self.config.enable_fault_detection = enabled;
        self
    }

    /// Enable periodic Event Enrollment evaluation (default `true`).
    pub fn enable_event_enrollment(mut self, enabled: bool) -> Self {
        self.config.enable_event_enrollment = enabled;
        self
    }

    /// Set the interval in seconds between Event Enrollment evaluation passes
    /// (default 10).
    pub fn event_enrollment_interval_secs(mut self, secs: u64) -> Self {
        self.config.event_enrollment_interval_secs = secs;
        self
    }

    /// Set the discovery rate-limiting and duplicate suppression policy.
    pub fn discovery_policy(mut self, policy: DiscoveryPolicy) -> Self {
        self.config.discovery_policy = policy;
        self
    }

    /// Set the COV quota and notification work budget policy, checked by
    /// [`CovPolicy::validate`] before transport startup.
    pub fn cov_policy(mut self, policy: CovPolicy) -> Self {
        self.config.cov_policy = policy;
        self
    }

    /// Build and start the server.
    pub async fn build(mut self) -> Result<BACnetServer<T>, Error> {
        let transport = self
            .transport
            .ok_or_else(|| Error::Encoding("transport not set on ServerBuilder".into()))?;
        let db = crate::local_device::prepare_database(
            &mut self.config,
            self.db,
            transport.local_receive_apdu_capacity(),
        )?;
        BACnetServer::start_with_clock_mode_and_bindings(
            self.config,
            db,
            transport,
            Some(ClockConfig::default()),
            self.configured_device_bindings,
        )
        .await
    }
}

/// BIP-specific builder that constructs `BipTransport` from interface/port/broadcast fields.
pub struct BipServerBuilder {
    config: ServerConfig,
    db: Option<ObjectDatabase>,
    configured_device_bindings: Vec<DeviceBinding>,
    mode: BipMode,
    multicast: Option<BipMulticast>,
}

impl BipServerBuilder {
    /// Set the one Device identity. With no supplied database, startup creates
    /// a stock Device after resolving the owned transport's receive capacity.
    pub fn device(mut self, identity: bacnet_objects::DeviceIdentity) -> Self {
        self.config.device = identity;
        self
    }

    /// Set the object database (transfers ownership).
    pub fn database(mut self, db: ObjectDatabase) -> Self {
        self.db = Some(db);
        self
    }

    /// Register one explicit unicast route for a Device recipient.
    pub fn device_binding(mut self, binding: DeviceBinding) -> Result<Self, Error> {
        register_configured_binding(&mut self.configured_device_bindings, binding)?;
        Ok(self)
    }

    /// Select the only local Audit Log that receives authorized notifications.
    pub fn audit_notification_sink(mut self, sink: ObjectIdentifier) -> Self {
        self.config.audit_notification_sink = Some(sink);
        self
    }

    /// Enable periodic fault detection / reliability evaluation.
    ///
    /// When enabled, every object's opt-in reliability hook runs every 10
    /// seconds; the default hook is a no-op.
    ///
    /// Reliability evaluation only; Event Enrollment evaluation is configured
    /// by [`enable_event_enrollment`](Self::enable_event_enrollment).
    pub fn enable_fault_detection(mut self, enabled: bool) -> Self {
        self.config.enable_fault_detection = enabled;
        self
    }

    /// Enable periodic Event Enrollment evaluation (default `true`).
    pub fn enable_event_enrollment(mut self, enabled: bool) -> Self {
        self.config.enable_event_enrollment = enabled;
        self
    }

    /// Set the interval in seconds between Event Enrollment evaluation passes
    /// (default 10).
    pub fn event_enrollment_interval_secs(mut self, secs: u64) -> Self {
        self.config.event_enrollment_interval_secs = secs;
        self
    }

    /// Set the discovery rate-limiting and duplicate suppression policy.
    pub fn discovery_policy(mut self, policy: DiscoveryPolicy) -> Self {
        self.config.discovery_policy = policy;
        self
    }

    /// Set the COV quota and notification work budget policy, checked by
    /// [`CovPolicy::validate`] before transport startup.
    pub fn cov_policy(mut self, policy: CovPolicy) -> Self {
        self.config.cov_policy = policy;
        self
    }

    /// Build and start the server, constructing a BipTransport from the config.
    pub async fn build(mut self) -> Result<BACnetServer<BipTransport>, Error> {
        let config = &self.config;
        let mut transport =
            BipTransport::new(config.interface, config.port, config.broadcast_address);
        transport.set_share_port_by_address(config.share_port_by_address);
        transport.set_mode(self.mode);
        transport.set_multicast(self.multicast)?;
        let db = crate::local_device::prepare_database(
            &mut self.config,
            self.db,
            transport.local_receive_apdu_capacity(),
        )?;
        BACnetServer::start_with_clock_mode_and_bindings(
            self.config,
            db,
            transport,
            Some(ClockConfig::default()),
            self.configured_device_bindings,
        )
        .await
    }
}

type TransportCleanup<T> = JoinHandle<(NetworkLayer<T>, Result<(), Error>)>;

/// BACnet server with APDU dispatch and service handling.
///
/// `T` is the transport the server runs over. It defaults to
/// [`AnyTransport`], so `BACnetServer` alone names a server over any
/// built-in transport; `BACnetServer<BipTransport>` and the like name one
/// over a concrete transport.
pub struct BACnetServer<T: TransportPort = AnyTransport> {
    target_audit: Option<Arc<audit_recipient::TargetAudit<T>>>,
    /// Shared with dispatch and every task the server starts (#1521).
    config: Arc<ServerConfig>,
    discovery_limiter: Arc<DiscoveryLimiter>,
    #[allow(dead_code)] // Retained with the server, including direct dispatch tests.
    time_sync_limiter: Arc<TimeSyncLimiter>,
    /// Server-owned clock controller; absent in explicit clockless mode.
    _clock: Option<Arc<ServerClock>>,
    /// Shared network layer (also held by dispatch task; read by
    /// [`write_local`](Self::write_local) for post-write COV/event sends).
    network: Option<Arc<NetworkLayer<T>>>,
    broadcaster: Arc<broadcaster::BroadcasterState<T>>,
    transport_cleanup: Option<TransportCleanup<T>>,
    transport_cleanup_error: Option<String>,
    network_number_task: Option<JoinHandle<()>>,
    /// Shared object database.
    db: Arc<RwLock<ObjectDatabase>>,
    /// COV subscription table (also held by dispatch task; read by
    /// [`write_local`](Self::write_local) to fire post-write notifications).
    cov_table: Arc<RwLock<CovSubscriptionTable>>,
    cov_counters: Arc<crate::cov::AtomicCovCounters>,
    /// Channels for routing segmented-send events to in-progress segmented sends.
    #[allow(dead_code)]
    seg_ack_senders: Arc<segmented_send::SegmentedSendRegistry>,
    /// Permits that cap live segmented response sender tasks, including
    /// cancelled senders that have not yet exited a transport send.
    #[allow(dead_code)]
    seg_send_permits: Arc<Semaphore>,
    /// Operational cap of 255 concurrent confirmed COV notification workers.
    /// Invoke-ID ownership is handled by `notification_transactions`.
    cov_in_flight: Arc<Semaphore>,
    /// Bounded DNET-to-router cache learned from admitted notification terminals.
    learned_routers: Arc<Mutex<LearnedRouterCache>>,
    /// Invoke-ID ownership and terminal admission for confirmed notifications.
    notification_transactions: Arc<NotificationTransactions>,
    /// Server-lifetime exact inbound ConfirmedRequest duplicate state.
    confirmed_request_tracker: Arc<ConfirmedRequestTracker>,
    /// Shared configured and passively observed Device recipient authority.
    device_bindings: Arc<RwLock<DeviceBindingTable>>,
    /// DeviceCommunicationControl state; only `dcc_timer` changes it.
    comm_state: Arc<CommState>,
    /// DCC timer owner and replacement/expiry serialization boundary.
    /// Valid replacement and explicit stop abort and join before clearing it.
    dcc_timer: Arc<Mutex<crate::server::dcc_timer::TimerSlot>>,
    dcc_outcomes: Arc<dcc_outcomes::DccOutcomes>,
    /// Lifetime totals of undelivered event notifications (#1142).
    event_suppressions: Arc<event_suppression::EventSuppressions>,
    mutation_decisions: Arc<crate::mutation::MutationDecisions>,
    dispatch_task: Option<JoinHandle<()>>,
    request_tasks: Arc<request_tasks::RequestTasks>,
    cov_purge_task: Option<JoinHandle<()>>,
    fault_detection_task: Option<JoinHandle<()>>,
    event_enrollment_task: Option<JoinHandle<()>>,
    trend_log_task: Option<JoinHandle<()>>,
    schedule_tick_task: Option<JoinHandle<()>>,
    /// One-second `Time_Delay` confirmation task for intrinsic reporting.
    intrinsic_reporting_task: Option<JoinHandle<()>>,
    /// Monotonic object-operation task: Binary Lighting Output
    /// WARN_OFF/WARN_RELINQUISH egress and Access Door pulse relock (#1073).
    binary_lighting_operation_task: Option<JoinHandle<()>>,
    /// Follow-up fanout after acknowledged confirmed COV reports (#896).
    cov_revisit_task: Option<JoinHandle<()>>,
    local_mac: MacAddr,
}

/// Cloneable handle for unsolicited I-Am announcements while its server runs.
///
/// At most 32 local broadcasts may be in flight. Admission fails immediately
/// at capacity or after shutdown begins. Admitted sends belong to the server:
/// cancelling their caller only drops its waiter; stop cancels and joins them.
/// Retaining this handle does not retain the transport after shutdown.
pub struct IAmBroadcaster<T: TransportPort> {
    state: std::sync::Weak<broadcaster::BroadcasterState<T>>,
}

impl<T: TransportPort> Clone for IAmBroadcaster<T> {
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
        }
    }
}

impl BACnetServer<BipTransport> {
    /// Create a BIP-specific builder with interface/port/broadcast fields.
    pub fn bip_builder() -> BipServerBuilder {
        BipServerBuilder {
            config: ServerConfig::default(),
            db: None,
            configured_device_bindings: Vec::new(),
            mode: BipMode::Normal,
            multicast: None,
        }
    }
}

mod clock;
mod command_runs;
mod reinitialize;
mod time_sync_policy;
#[doc(hidden)]
pub use reinitialize::EndpointReinitializeAudit as __endpoint_ReinitializeAudit;
pub use reinitialize::{ReinitializeContext, ReinitializeHandler};
mod write_group;
pub use clock::ClockConfig;
use clock::ServerClock;
#[cfg(test)]
pub(crate) use clock::clocked_test_database;
use time_sync_policy::{TimeSyncLimiter, request_limiters};
pub use time_sync_policy::{
    TimeSyncPolicy, TimeSyncRateLimit, TimeSyncSource, TimeSyncSourceRestriction,
};
mod binary_lighting_lifecycle;
#[cfg(test)]
mod confirmed_issuance_tests;
mod confirmed_request_tracker;
mod cov_clock;
mod cov_encoding;
mod cov_fanout;
mod cov_notifications;
mod cov_notify_context;
mod cov_snapshot;
mod dcc_disable_rate;
pub(crate) mod dcc_outcomes;
mod dcc_policy;
mod lso_replay;
pub use dcc_disable_rate::DccDisableRateLimit;
mod dcc_timer;
pub use dcc_outcomes::DccOutcomeCounters;
pub use dcc_policy::{DccPolicy, DccSource, DccSourceRestriction};
use dcc_timer::CommState;
pub use dcc_timer::DccState;
mod binding_probes;
mod device_bindings;
mod discovery;
#[doc(hidden)]
pub use discovery::iam_request_for as discovery_iam_for_test;
pub use discovery::{DiscoveryCounters, DiscoveryPolicy};
pub(crate) use discovery::{DiscoveryLimiter, PreCheckDecision, WhoHasTarget};
mod audit_log_purge;
mod dispatch;
mod durable_writes;
#[doc(hidden)]
pub use durable_writes::settle_endpoint as __endpoint_settle_durable_writes;
mod event_delivery;
mod event_enrollment_lifecycle;
mod event_forwarding;
pub use event_forwarding::MAX_FORWARDED_DESTINATIONS;
mod event_forwarding_repeats;
mod event_message_policy;
pub(crate) mod event_notification_payload;
mod event_notifications;
mod event_recipient_route;
mod event_send;
mod event_suppression;
pub use event_suppression::EventNotificationCounters;
mod received_event_log;
pub use received_event_log::{RECEIVED_EVENT_LOG_RATE, RECEIVED_EVENT_LOG_SOURCES};
mod confirmed_answer;
pub(crate) mod event_timestamp;
mod handles;
mod learned_router_cache;
mod lifecycle;
mod local_authorization;
pub use local_authorization::LocalAuthorizationResult;
mod local_writes;
mod network_port;
#[cfg(test)]
mod network_port_tests;
/// Reruns a test whose closing rebind loses the freed port to another
/// process (#1419).
#[cfg(test)]
#[path = "../../../bacnet-endpoint/tests/support/port_retry.rs"]
mod port_retry;
pub use confirmed_answer::CovAckResult;
mod remote_writes;
pub(crate) use remote_writes::RemoteRequestError;
mod notification_transactions;
#[doc(hidden)]
pub use notification_transactions::{
    AuditFailureContext as __endpoint_AuditFailureContext,
    AuditFailureQueue as __endpoint_AuditFailureQueue,
    AuditFailureTicket as __endpoint_AuditFailureTicket,
    NotificationOperation as __endpoint_NotificationOperation,
    NotificationReserveError as __endpoint_NotificationReserveError,
    NotificationTransactions as __endpoint_NotificationTransactions,
    NotificationWorkerResult as __endpoint_NotificationWorkerResult,
    canonical_direct_peer as __endpoint_canonical_direct_peer,
    canonical_routed_peer as __endpoint_canonical_routed_peer,
    run_notification_worker as __endpoint_run_notification_worker,
};
mod requests;
#[doc(hidden)]
pub use requests::endpoint_responder::EndpointResponder as __endpoint_EndpointResponder;
#[cfg(feature = "sc-tls")]
mod sc_builder;
#[cfg(test)]
pub(crate) use requests::{EXECUTED_CONFIRMED, EXECUTED_UNCONFIRMED};
#[cfg(test)]
mod audit_notification_tests;
#[cfg(test)]
mod unconfirmed_audit_notification_tests;
#[cfg(feature = "sc-tls")]
pub use sc_builder::ScServerBuilder;
mod responses;
mod segmentation;
mod segmented_receive;
mod segmented_send;
pub(crate) use segmented_send::*;
mod request_admission;
mod rpm_budget;
pub use rpm_budget::ReadPropertyMultipleBudget;
mod alarm_summary_budget;
pub use alarm_summary_budget::GetAlarmSummaryBudget;
mod atomic_read_file_budget;
mod atomic_write_file_budget;
mod read_range_budget;
pub use read_range_budget::ReadRangeBudget;
mod event_information_budget;
pub use event_information_budget::GetEventInformationBudget;
mod enrollment_summary_budget;
pub use atomic_read_file_budget::AtomicReadFileBudget;
pub use atomic_write_file_budget::AtomicWriteFileBudget;
pub use enrollment_summary_budget::GetEnrollmentSummaryBudget;
#[cfg(test)]
mod atomic_read_file_tests;
#[cfg(test)]
mod atomic_write_file_tests;
#[cfg(test)]
mod enrollment_summary_tests;
mod request_peer;
mod request_services;
mod request_tasks;
pub use request_admission::{RequestAdmissionCounters, RequestAdmissionPolicy};
mod shutdown;
pub use shutdown::drop_database_off_runtime;

#[cfg(test)]
mod access_door_event_tests;
#[cfg(test)]
mod access_door_pulse_task_tests;
#[cfg(test)]
mod access_zone_event_tests;
#[cfg(test)]
mod acknowledge_alarm_tests;
#[cfg(test)]
mod active_cov_subscriptions_tests;
#[cfg(test)]
mod audit_log_query_tests;
#[cfg(test)]
mod averaging_reference_write_tests;
#[cfg(test)]
mod averaging_sample_tests;
#[cfg(test)]
mod binary_lighting_task_tests;
#[cfg(test)]
mod channel_member_concurrency_tests;
#[cfg(test)]
mod channel_reliability_tests;
#[cfg(test)]
mod channel_remote_datatype_tests;
#[cfg(test)]
mod channel_remote_write_tests;
#[cfg(test)]
mod channel_run_tests;
#[cfg(test)]
mod channel_wire_tests;
#[cfg(test)]
mod color_channel_member_tests;
#[cfg(test)]
mod color_engine_task_tests;
#[cfg(test)]
mod command_action_run_tests;
#[cfg(test)]
mod command_action_wire_tests;
#[cfg(test)]
mod command_remote_write_tests;
#[cfg(test)]
mod command_run_cancel_tests;
#[cfg(test)]
mod command_run_stop_tests;
#[cfg(test)]
mod confirmed_broadcast_tests;
#[cfg(test)]
mod cov_background_tests;
#[cfg(test)]
mod cov_budget_tests;
#[cfg(test)]
mod cov_confirmed_baseline_tests;
#[cfg(test)]
mod cov_confirmed_context_tests;
#[cfg(test)]
mod cov_multiple_admission_tests;
#[cfg(test)]
mod cov_notifications_tests;
#[cfg(test)]
mod cov_quota_tests;
#[cfg(test)]
mod cov_timed_capture_order_tests;
#[cfg(test)]
mod cov_timed_chunk_tests;
#[cfg(test)]
mod cov_timed_deadline_tests;
#[cfg(test)]
mod cov_timed_multiple_tests;
#[cfg(test)]
mod cov_timed_producer_tests;
#[cfg(test)]
mod cov_timed_value_split_tests;
#[cfg(test)]
mod cov_untimed_split_tests;
#[cfg(test)]
mod cov_wire_test_support;
#[cfg(test)]
mod dcc_event_detection_tests;
#[cfg(test)]
mod device_bindings_tests;
#[cfg(test)]
mod device_recipient_routing_tests;
#[cfg(all(test, feature = "sc-tls"))]
mod direct_principal_tests;
#[cfg(test)]
mod discovery_dcc_tests;
#[cfg(test)]
mod discovery_tests;
#[cfg(test)]
mod event_confirmed_routing_tests;
#[cfg(test)]
mod event_enable_distribution_tests;
#[cfg(test)]
mod event_enrollment_task_tests;
#[cfg(test)]
mod event_forwarding_bounds_tests;
#[cfg(test)]
mod event_forwarding_cap_tests;
#[cfg(test)]
mod event_forwarding_origin_tests;
#[cfg(test)]
mod event_forwarding_rule_tests;
#[cfg(test)]
mod event_forwarding_tests;
#[cfg(test)]
mod event_log_notification_tests;
#[cfg(test)]
mod event_network_priority_tests;
#[cfg(test)]
mod event_notifications_tests;
#[cfg(test)]
mod event_recipient_routing_tests;
#[cfg(test)]
mod life_safety_application_tests;
#[cfg(test)]
mod life_safety_cov_tests;
#[cfg(test)]
mod life_safety_operation_replay_tests;
#[cfg(test)]
mod life_safety_operation_tests;
#[cfg(test)]
mod lighting_command_member_tests;
#[cfg(test)]
mod lighting_engine_task_tests;
#[cfg(test)]
mod lighting_present_value_tests;
#[cfg(test)]
mod list_element_event_tests;
#[cfg(test)]
mod local_network_number_tests;
#[cfg(test)]
mod local_network_outbound_tests;
#[cfg(test)]
mod log_reference_resize_tests;
#[cfg(test)]
mod loop_controlled_variable_tests;
#[cfg(test)]
mod loop_cov_tests;
#[cfg(test)]
mod mistagged_request_wire_tests;
#[cfg(test)]
mod noncommandable_null_cov_tests;
#[cfg(test)]
mod notification_dcc_tests;
#[cfg(test)]
mod notification_transactions_tests;
#[cfg(test)]
mod pulse_converter_cov_tests;
#[cfg(test)]
mod pulse_converter_reference_tests;
#[cfg(test)]
mod rb07_provenance_tests;
#[cfg(test)]
mod reference_write_wire_tests;
#[cfg(test)]
mod remote_write_answer_tests;
#[cfg(test)]
mod remote_write_discovery_tests;
#[cfg(test)]
mod run_cycle_tests;
#[cfg(test)]
mod schedule_create_retry_tests;
#[cfg(test)]
mod schedule_reference_list_tests;
#[cfg(test)]
mod schedule_reference_write_tests;
#[cfg(test)]
mod schedule_write_tests;
#[cfg(test)]
mod segmentation_tests;
#[cfg(test)]
mod staging_cov_tests;
#[cfg(test)]
mod staging_reference_write_tests;
#[cfg(test)]
mod subscribed_recipients_expiry_tests;
#[cfg(test)]
mod table_13_1_cov_tests;
#[cfg(test)]
pub(crate) mod test_forwarder;
#[cfg(test)]
pub(crate) mod test_transport;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod trailing_octet_wire_tests;
#[cfg(test)]
mod truncated_request_wire_tests;
#[cfg(test)]
mod value_cov_increment_tests;
#[cfg(test)]
mod value_source_local_tests;
#[cfg(test)]
mod warn_spy;

impl<T: TransportPort + 'static> BACnetServer<T> {
    /// Get a snapshot of discovery rate-limiting counters.
    pub fn discovery_counters(&self) -> DiscoveryCounters {
        self.discovery_limiter.counters()
    }

    /// Get a snapshot of COV operational and telemetry counters.
    pub fn cov_counters(&self) -> CovCounters {
        self.cov_counters.snapshot()
    }

    /// Sample lifetime mutation authorization decisions, including after `stop()`.
    /// Counts decisions, not completed handlers or response delivery; see
    /// [`MutationDecisionCounters`](crate::mutation::MutationDecisionCounters).
    pub fn mutation_decision_counters(&self) -> crate::mutation::MutationDecisionCounters {
        self.mutation_decisions.snapshot()
    }

    /// Remove COV subscriptions using this current immediate MAC and routed source.
    /// Cleanup of an obsolete router leaves subscriptions renewed onto another route.
    /// Only removed entries release their contribution to the shared quota group.
    pub async fn remove_peer_subscriptions(
        &self,
        mac: &[u8],
        network: Option<&NpduAddress>,
    ) -> usize {
        let mut table = self.cov_table.write().await;
        table.remove_peer_subscriptions(mac, network)
    }
}

#[cfg(test)]
mod cov_identity_completion_tests;

mod broadcaster;
#[cfg(test)]
mod r7_reply_tests;