use super::*;
use alloc::sync::Arc;
use bacnet_types::bip_port::BipPortMode;
impl NetworkPortObject {
pub(crate) fn network_number_internal(
&mut self,
announcement: Option<(u16, u8)>,
) -> NetworkNumber {
if let Some((number, flag)) = announcement
&& self.network_number.observe(number, flag)
== bacnet_types::network_number::Observation::ConfiguredConflict
{
log_debug!(
configured = self.network_number.snapshot().0,
announced = number,
"local Network Number configuration conflict"
);
}
self.network_number
}
pub(crate) fn configuration_internal(&self) -> Option<&BipPortConfig> {
match &self.ip {
Some(IpConfig::V4(config)) => Some(config),
_ => None,
}
}
pub(crate) fn bip6_configuration_internal(&self) -> Option<&Bip6PortConfig> {
match &self.ip {
Some(IpConfig::V6(config)) => Some(config),
_ => None,
}
}
pub(crate) fn configured_address_internal(&self) -> Option<PortAddress> {
self.ip.as_ref().map(IpConfig::address)
}
pub(crate) fn reserve_internal(&mut self, lease: &Arc<()>) -> Result<(), Error> {
let Some(config) = self
.ip
.as_ref()
.filter(|_| !self.is_bound() && !self.out_of_service)
else {
return Err(Error::Encoding(
"Network Port is unavailable for B/IP registration".into(),
));
};
self.network_number =
NetworkNumber::configured(config.network_number()).expect("validated Network Number");
self.mode = bip_mode::LiveMode::Normal;
self.pending = Default::default();
self.multicast = Default::default();
self.activation = false;
self.binding = Arc::downgrade(lease);
Ok(())
}
pub(crate) fn reconcile_internal(
&mut self,
lease: &Arc<()>,
bind: PortBind,
capacity: u32,
mode: BipPortMode,
) -> Result<(), Error> {
if !self
.binding
.upgrade()
.is_some_and(|current| Arc::ptr_eq(¤t, lease))
{
return Err(Error::Encoding(
"Network Port registration owner mismatch".into(),
));
}
let config = self
.ip
.as_ref()
.ok_or_else(|| Error::Encoding("not B/IP".into()))?
.with_bind(bind, capacity)?;
config.validate(self.oid.instance_number())?;
let mode = bip_mode::LiveMode::from_port(mode)?;
self.network_number =
NetworkNumber::configured(config.network_number()).expect("validated Network Number");
self.apdu_length = capacity;
self.mac_address = config.mac();
self.ip = Some(config);
self.mode = mode;
self.pending = Default::default();
self.multicast.clear_pending();
Ok(())
}
pub(crate) fn set_multicast_internal(
&mut self,
view: Option<Arc<dyn bacnet_types::bip_port::IpMulticast>>,
) {
self.multicast = super::multicast::Multicast::lent(view);
}
}
impl NetworkPortObject {
pub(crate) fn set_activation_internal(&mut self, activation: bool) {
self.activation = activation;
}
pub(super) fn is_bound(&self) -> bool {
self.binding.strong_count() != 0
}
}