bacnet_server/server/comm_state.rs
1//! The server's DeviceCommunicationControl state (Clause 16.1).
2//!
3//! This module sits under `dcc_timer`, so only an accepted DCC request and its
4//! revert timer can change the state. Everything else holding a [`CommState`]
5//! reads it.
6
7use std::sync::atomic::{AtomicBool, Ordering};
8
9use bacnet_types::enums::EnableDisable;
10
11#[cfg(test)]
12#[path = "comm_state_tests.rs"]
13mod tests;
14
15/// The communication state DeviceCommunicationControl has put the server in
16/// (Clause 16.1).
17///
18/// A request for the deprecated DISABLE value is always refused with
19/// `SERVICES` / `SERVICE_REQUEST_DENIED` (Clause 16.1.2), whatever the
20/// [`DccPolicy`](crate::server::DccPolicy), so the server is only ever in one of
21/// these two states.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
23pub enum DccState {
24 /// Communication is enabled: ENABLE. The state at every start.
25 #[default]
26 Enable,
27 /// DISABLE_INITIATION: the server still executes and answers requests, but
28 /// starts nothing of its own except the I-Am that answers a Who-Is and
29 /// audit notifications.
30 DisableInitiation,
31}
32
33impl DccState {
34 /// Whether the server holds back the messages it would start on its own:
35 /// COV and event notifications, I-Have, unsolicited I-Am, remote writes
36 /// and their Who-Is.
37 pub const fn initiation_restricted(self) -> bool {
38 matches!(self, Self::DisableInitiation)
39 }
40}
41
42impl From<DccState> for EnableDisable {
43 fn from(state: DccState) -> Self {
44 match state {
45 DccState::Enable => Self::ENABLE,
46 DccState::DisableInitiation => Self::DISABLE_INITIATION,
47 }
48 }
49}
50
51/// The server's live [`DccState`], read lock-free by every path that starts
52/// a message.
53///
54/// The setter is private to `dcc_timer`; tests reach it through
55/// `set_for_test`.
56#[derive(Debug, Default)]
57pub(crate) struct CommState {
58 initiation_restricted: AtomicBool,
59}
60
61impl CommState {
62 /// The current state.
63 pub(crate) fn get(&self) -> DccState {
64 if self.initiation_restricted() {
65 DccState::DisableInitiation
66 } else {
67 DccState::Enable
68 }
69 }
70
71 /// Whether DCC restricts initiation now; see
72 /// [`DccState::initiation_restricted`].
73 pub(crate) fn initiation_restricted(&self) -> bool {
74 self.initiation_restricted.load(Ordering::Acquire)
75 }
76
77 /// Commit a new state. Only `dcc_timer` calls this: when a DCC request is
78 /// accepted, and when its timer runs out.
79 pub(super) fn set(&self, state: DccState) {
80 self.initiation_restricted
81 .store(state.initiation_restricted(), Ordering::Release);
82 }
83
84 /// Put the state in place without a DCC request, for tests that need DCC
85 /// in force at a given step.
86 #[cfg(test)]
87 pub(crate) fn set_for_test(&self, state: DccState) {
88 self.set(state);
89 }
90}