bacnet_encoding/constructed/
access_credential.rs1use bacnet_types::constructed::{
18 BACnetAssignedAccessRights, BACnetAuthenticationFactor, BACnetAuthenticationFactorFormat,
19 BACnetCredentialAuthenticationFactor,
20};
21use bacnet_types::enums::{AccessAuthenticationFactorDisable, AuthenticationFactorType};
22use bacnet_types::error::Error;
23use bytes::BytesMut;
24
25use super::tagged::{
26 decode_ctx_boolean, decode_ctx_octet_string, decode_ctx_unsigned, decode_optional_ctx,
27 expect_closing, expect_opening,
28};
29use super::{decode_device_object_reference, encode_device_object_reference};
30use crate::{primitives, tags};
31
32const RIGHTS: &str = "BACnetAssignedAccessRights";
33const FACTOR: &str = "BACnetAuthenticationFactor";
34const CREDENTIAL_FACTOR: &str = "BACnetCredentialAuthenticationFactor";
35const FACTOR_FORMAT: &str = "BACnetAuthenticationFactorFormat";
36
37pub fn encode_assigned_access_rights(buf: &mut BytesMut, value: &BACnetAssignedAccessRights) {
40 tags::encode_opening_tag(buf, 0);
41 encode_device_object_reference(buf, &value.assigned_access_rights);
42 tags::encode_closing_tag(buf, 0);
43 primitives::encode_ctx_boolean(buf, 1, value.enable);
44}
45
46pub fn decode_assigned_access_rights(
52 data: &[u8],
53 offset: usize,
54) -> Result<(BACnetAssignedAccessRights, usize), Error> {
55 let pos = expect_opening(data, offset, 0, RIGHTS)?;
56 let (assigned_access_rights, pos) = decode_device_object_reference(data, pos)?;
57 let pos = expect_closing(data, pos, 0, RIGHTS)?;
58 let (enable, end) = decode_ctx_boolean(data, pos, 1, RIGHTS)?;
59 Ok((
60 BACnetAssignedAccessRights {
61 assigned_access_rights,
62 enable,
63 },
64 end,
65 ))
66}
67
68pub fn encode_authentication_factor(buf: &mut BytesMut, value: &BACnetAuthenticationFactor) {
71 primitives::encode_ctx_enumerated(buf, 0, value.format_type.to_raw());
72 primitives::encode_ctx_unsigned(buf, 1, u64::from(value.format_class));
73 primitives::encode_ctx_octet_string(buf, 2, &value.value);
74}
75
76pub fn decode_authentication_factor(
83 data: &[u8],
84 offset: usize,
85) -> Result<(BACnetAuthenticationFactor, usize), Error> {
86 let (format_type, pos) = decode_ctx_unsigned::<u32>(data, offset, 0, FACTOR)?;
87 let (format_class, pos) = decode_ctx_unsigned::<u32>(data, pos, 1, FACTOR)?;
88 let (value, end) = decode_ctx_octet_string(data, pos, 2, FACTOR)?;
89 Ok((
90 BACnetAuthenticationFactor {
91 format_type: AuthenticationFactorType::from_raw(format_type),
92 format_class,
93 value,
94 },
95 end,
96 ))
97}
98
99pub fn encode_authentication_factor_format(
103 buf: &mut BytesMut,
104 value: &BACnetAuthenticationFactorFormat,
105) {
106 primitives::encode_ctx_enumerated(buf, 0, value.format_type.to_raw());
107 if let Some(vendor_id) = value.vendor_id {
108 primitives::encode_ctx_unsigned(buf, 1, u64::from(vendor_id));
109 }
110 if let Some(vendor_format) = value.vendor_format {
111 primitives::encode_ctx_unsigned(buf, 2, u64::from(vendor_format));
112 }
113}
114
115pub fn decode_authentication_factor_format(
124 data: &[u8],
125 offset: usize,
126) -> Result<(BACnetAuthenticationFactorFormat, usize), Error> {
127 let (format_type, pos) = decode_ctx_unsigned::<u32>(data, offset, 0, FACTOR_FORMAT)?;
128 let (vendor_id, pos) =
129 decode_optional_ctx(data, pos, 1, FACTOR_FORMAT, decode_ctx_unsigned::<u16>)?;
130 let (vendor_format, end) =
131 decode_optional_ctx(data, pos, 2, FACTOR_FORMAT, decode_ctx_unsigned::<u16>)?;
132 Ok((
133 BACnetAuthenticationFactorFormat {
134 format_type: AuthenticationFactorType::from_raw(format_type),
135 vendor_id,
136 vendor_format,
137 },
138 end,
139 ))
140}
141
142pub fn encode_credential_authentication_factor(
145 buf: &mut BytesMut,
146 value: &BACnetCredentialAuthenticationFactor,
147) {
148 primitives::encode_ctx_enumerated(buf, 0, value.disable.to_raw());
149 tags::encode_opening_tag(buf, 1);
150 encode_authentication_factor(buf, &value.authentication_factor);
151 tags::encode_closing_tag(buf, 1);
152}
153
154pub fn decode_credential_authentication_factor(
160 data: &[u8],
161 offset: usize,
162) -> Result<(BACnetCredentialAuthenticationFactor, usize), Error> {
163 let (disable, pos) = decode_ctx_unsigned::<u32>(data, offset, 0, CREDENTIAL_FACTOR)?;
164 let pos = expect_opening(data, pos, 1, CREDENTIAL_FACTOR)?;
165 let (authentication_factor, pos) = decode_authentication_factor(data, pos)?;
166 let end = expect_closing(data, pos, 1, CREDENTIAL_FACTOR)?;
167 Ok((
168 BACnetCredentialAuthenticationFactor {
169 disable: AccessAuthenticationFactorDisable::from_raw(disable),
170 authentication_factor,
171 },
172 end,
173 ))
174}