use bacnet_types::constructed::{
BACnetAssignedAccessRights, BACnetAuthenticationFactor, BACnetAuthenticationFactorFormat,
BACnetCredentialAuthenticationFactor,
};
use bacnet_types::enums::{AccessAuthenticationFactorDisable, AuthenticationFactorType};
use bacnet_types::error::Error;
use bytes::BytesMut;
use super::tagged::{
decode_ctx_boolean, decode_ctx_octet_string, decode_ctx_unsigned, decode_optional_ctx,
expect_closing, expect_opening,
};
use super::{decode_device_object_reference, encode_device_object_reference};
use crate::{primitives, tags};
const RIGHTS: &str = "BACnetAssignedAccessRights";
const FACTOR: &str = "BACnetAuthenticationFactor";
const CREDENTIAL_FACTOR: &str = "BACnetCredentialAuthenticationFactor";
const FACTOR_FORMAT: &str = "BACnetAuthenticationFactorFormat";
pub fn encode_assigned_access_rights(buf: &mut BytesMut, value: &BACnetAssignedAccessRights) {
tags::encode_opening_tag(buf, 0);
encode_device_object_reference(buf, &value.assigned_access_rights);
tags::encode_closing_tag(buf, 0);
primitives::encode_ctx_boolean(buf, 1, value.enable);
}
pub fn decode_assigned_access_rights(
data: &[u8],
offset: usize,
) -> Result<(BACnetAssignedAccessRights, usize), Error> {
let pos = expect_opening(data, offset, 0, RIGHTS)?;
let (assigned_access_rights, pos) = decode_device_object_reference(data, pos)?;
let pos = expect_closing(data, pos, 0, RIGHTS)?;
let (enable, end) = decode_ctx_boolean(data, pos, 1, RIGHTS)?;
Ok((
BACnetAssignedAccessRights {
assigned_access_rights,
enable,
},
end,
))
}
pub fn encode_authentication_factor(buf: &mut BytesMut, value: &BACnetAuthenticationFactor) {
primitives::encode_ctx_enumerated(buf, 0, value.format_type.to_raw());
primitives::encode_ctx_unsigned(buf, 1, u64::from(value.format_class));
primitives::encode_ctx_octet_string(buf, 2, &value.value);
}
pub fn decode_authentication_factor(
data: &[u8],
offset: usize,
) -> Result<(BACnetAuthenticationFactor, usize), Error> {
let (format_type, pos) = decode_ctx_unsigned::<u32>(data, offset, 0, FACTOR)?;
let (format_class, pos) = decode_ctx_unsigned::<u32>(data, pos, 1, FACTOR)?;
let (value, end) = decode_ctx_octet_string(data, pos, 2, FACTOR)?;
Ok((
BACnetAuthenticationFactor {
format_type: AuthenticationFactorType::from_raw(format_type),
format_class,
value,
},
end,
))
}
pub fn encode_authentication_factor_format(
buf: &mut BytesMut,
value: &BACnetAuthenticationFactorFormat,
) {
primitives::encode_ctx_enumerated(buf, 0, value.format_type.to_raw());
if let Some(vendor_id) = value.vendor_id {
primitives::encode_ctx_unsigned(buf, 1, u64::from(vendor_id));
}
if let Some(vendor_format) = value.vendor_format {
primitives::encode_ctx_unsigned(buf, 2, u64::from(vendor_format));
}
}
pub fn decode_authentication_factor_format(
data: &[u8],
offset: usize,
) -> Result<(BACnetAuthenticationFactorFormat, usize), Error> {
let (format_type, pos) = decode_ctx_unsigned::<u32>(data, offset, 0, FACTOR_FORMAT)?;
let (vendor_id, pos) =
decode_optional_ctx(data, pos, 1, FACTOR_FORMAT, decode_ctx_unsigned::<u16>)?;
let (vendor_format, end) =
decode_optional_ctx(data, pos, 2, FACTOR_FORMAT, decode_ctx_unsigned::<u16>)?;
Ok((
BACnetAuthenticationFactorFormat {
format_type: AuthenticationFactorType::from_raw(format_type),
vendor_id,
vendor_format,
},
end,
))
}
pub fn encode_credential_authentication_factor(
buf: &mut BytesMut,
value: &BACnetCredentialAuthenticationFactor,
) {
primitives::encode_ctx_enumerated(buf, 0, value.disable.to_raw());
tags::encode_opening_tag(buf, 1);
encode_authentication_factor(buf, &value.authentication_factor);
tags::encode_closing_tag(buf, 1);
}
pub fn decode_credential_authentication_factor(
data: &[u8],
offset: usize,
) -> Result<(BACnetCredentialAuthenticationFactor, usize), Error> {
let (disable, pos) = decode_ctx_unsigned::<u32>(data, offset, 0, CREDENTIAL_FACTOR)?;
let pos = expect_opening(data, pos, 1, CREDENTIAL_FACTOR)?;
let (authentication_factor, pos) = decode_authentication_factor(data, pos)?;
let end = expect_closing(data, pos, 1, CREDENTIAL_FACTOR)?;
Ok((
BACnetCredentialAuthenticationFactor {
disable: AccessAuthenticationFactorDisable::from_raw(disable),
authentication_factor,
},
end,
))
}