use crate::common;
use crate::prelude::*;
use bacnet_types::error::Error;
const SITE_LOCAL_GROUP: [u8; 16] = [0xFF, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xBA, 0xC0];
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Bip6PortConfig {
pub network_number: u16,
pub apdu_length: u32,
pub ip_address: [u8; 16],
pub prefix_length: u8,
pub udp_port: u16,
pub default_gateway: [u8; 16],
pub dns_servers: Vec<[u8; 16]>,
pub multicast_address: [u8; 16],
pub vmac: [u8; 3],
}
impl Default for Bip6PortConfig {
fn default() -> Self {
Self {
network_number: 0,
apdu_length: 1476,
ip_address: [0; 16],
prefix_length: 64,
udp_port: 47808,
default_gateway: [0; 16],
dns_servers: vec![[0; 16]],
multicast_address: SITE_LOCAL_GROUP,
vmac: [0; 3],
}
}
}
impl Bip6PortConfig {
pub(super) fn validate(&self, instance: u32) -> Result<(), Error> {
if !(1..=255).contains(&instance)
|| core::net::Ipv6Addr::from(self.ip_address)
.to_ipv4_mapped()
.is_some()
|| self.network_number == u16::MAX
|| self.apdu_length < 50
|| self.dns_servers.is_empty()
|| !(1..=128).contains(&self.prefix_length)
|| self.multicast_address[0] != 0xFF
{
return Err(common::value_out_of_range_error());
}
Ok(())
}
pub(super) fn multicast_with_port(&self) -> Vec<u8> {
let mut value = self.multicast_address.to_vec();
value.extend_from_slice(&self.udp_port.to_be_bytes());
value
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Bip6Bind {
pub ip: [u8; 16],
pub udp: u16,
pub vmac: [u8; 3],
pub multicast: [u8; 16],
}