use core::net::SocketAddrV4;
use bacnet_bip_codec::BvlcResultCode;
use super::exchange::Operation;
use super::registration::AttemptOutcome;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ExchangeCounters {
pub sent: u32,
pub acknowledgements: u32,
pub results: u32,
pub last_result: Option<BvlcResultCode>,
pub timeouts: u32,
pub busy: u32,
pub local_errors: u32,
pub malformed_responses: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct RegisterAttempt {
pub bbmd: SocketAddrV4,
pub ttl: u16,
pub outcome: AttemptOutcome,
pub result: Option<BvlcResultCode>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct NodeCounters {
pub read_bdt: ExchangeCounters,
pub write_bdt: ExchangeCounters,
pub read_fdt: ExchangeCounters,
pub delete_fdt_entry: ExchangeCounters,
pub register_foreign_device: ExchangeCounters,
pub registration_attempts: u32,
pub registrations_accepted: u32,
pub registrations_refused: u32,
pub last_registration_attempt: Option<RegisterAttempt>,
pub unmatched_responses: u32,
pub late_responses: u32,
pub last_unmatched_result: Option<BvlcResultCode>,
pub malformed_responses: u32,
pub renewal_hints: u32,
pub lapses: u32,
pub broadcasts_held: u32,
pub broadcasts_released: u32,
pub broadcasts_discarded: u32,
pub broadcasts_hold_full: u32,
pub broadcasts_unregistered: u32,
}
impl NodeCounters {
#[must_use]
pub const fn exchange(&self, operation: Operation) -> &ExchangeCounters {
match operation {
Operation::ReadBdt => &self.read_bdt,
Operation::WriteBdt => &self.write_bdt,
Operation::ReadFdt => &self.read_fdt,
Operation::DeleteFdtEntry => &self.delete_fdt_entry,
Operation::RegisterForeignDevice => &self.register_foreign_device,
}
}
pub(crate) fn exchange_mut(&mut self, operation: Operation) -> &mut ExchangeCounters {
match operation {
Operation::ReadBdt => &mut self.read_bdt,
Operation::WriteBdt => &mut self.write_bdt,
Operation::ReadFdt => &mut self.read_fdt,
Operation::DeleteFdtEntry => &mut self.delete_fdt_entry,
Operation::RegisterForeignDevice => &mut self.register_foreign_device,
}
}
}
pub(crate) fn bump(counter: &mut u32) {
*counter = counter.saturating_add(1);
}
pub(crate) fn add(counter: &mut u32, count: u16) {
*counter = counter.saturating_add(u32::from(count));
}