#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use bacnet_encoding::primitives;
use bacnet_encoding::tags;
use bacnet_types::enums::{PropertyIdentifier, RejectReason};
use bacnet_types::error::{EncodeError, Error};
use bacnet_types::primitives::{Date, ObjectIdentifier, Time};
use bytes::BytesMut;
use super::helpers::{
actual_time_is_valid, decode_date_time, reject, validate_actual_date_time,
validate_actual_time, validate_decoded_property_identifier, validate_property_identifier,
validate_raw_property_value,
};
use crate::common::MAX_DECODED_ITEMS;
use bacnet_encoding::constructed::tagged::{
decode_ctx_fixed, decode_ctx_object_id, decode_ctx_unsigned, expect_end, misplaced_tag, Fixed,
next_is_context,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct COVNotificationValue {
pub property_identifier: PropertyIdentifier,
pub property_array_index: Option<u32>,
pub value: Vec<u8>,
pub time_of_change: Option<Time>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct COVNotificationItem {
pub monitored_object_identifier: ObjectIdentifier,
pub list_of_values: Vec<COVNotificationValue>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct COVNotificationMultipleRequest {
pub subscriber_process_identifier: u32,
pub initiating_device_identifier: ObjectIdentifier,
pub time_remaining: u32,
pub timestamp: Option<(Date, Time)>,
pub list_of_cov_notifications: Vec<COVNotificationItem>,
}
impl COVNotificationMultipleRequest {
pub fn encode(&self, buf: &mut BytesMut) -> Result<(), Error> {
self.validate()?;
let mut encoded = BytesMut::new();
primitives::encode_ctx_unsigned(&mut encoded, 0, self.subscriber_process_identifier as u64);
primitives::encode_ctx_object_id(&mut encoded, 1, &self.initiating_device_identifier);
primitives::encode_ctx_unsigned(&mut encoded, 2, self.time_remaining as u64);
if let Some((date, time)) = &self.timestamp {
tags::encode_opening_tag(&mut encoded, 3);
primitives::encode_app_date(&mut encoded, date);
primitives::encode_app_time(&mut encoded, time);
tags::encode_closing_tag(&mut encoded, 3);
}
tags::encode_opening_tag(&mut encoded, 4);
for item in &self.list_of_cov_notifications {
primitives::encode_ctx_object_id(&mut encoded, 0, &item.monitored_object_identifier);
tags::encode_opening_tag(&mut encoded, 1);
for val in &item.list_of_values {
primitives::encode_ctx_unsigned(
&mut encoded,
0,
val.property_identifier.to_raw() as u64,
);
if let Some(idx) = val.property_array_index {
primitives::encode_ctx_unsigned(&mut encoded, 1, idx as u64);
}
tags::encode_opening_tag(&mut encoded, 2);
encoded.extend_from_slice(&val.value);
tags::encode_closing_tag(&mut encoded, 2);
if let Some(time) = &val.time_of_change {
tags::encode_tag(&mut encoded, 3, tags::TagClass::Context, 4);
encoded.extend_from_slice(&time.encode());
}
}
tags::encode_closing_tag(&mut encoded, 1);
}
tags::encode_closing_tag(&mut encoded, 4);
buf.extend_from_slice(&encoded);
Ok(())
}
fn validate(&self) -> Result<(), Error> {
if self.list_of_cov_notifications.is_empty() {
return Err(Error::encode("COVNotificationMultiple notification list must not be empty"));
}
let mut total_values = 0usize;
let mut has_time_of_change = false;
if let Some((date, time)) = &self.timestamp {
validate_actual_date_time(date, time)?;
}
for item in &self.list_of_cov_notifications {
if item.list_of_values.is_empty() {
return Err(Error::encode("COVNotificationMultiple value list must not be empty"));
}
total_values = total_values
.checked_add(item.list_of_values.len())
.ok_or_else(|| Error::encode("notification value count overflow"))?;
if total_values > MAX_DECODED_ITEMS {
return Err(EncodeError::new("COVNotificationMultiple exceeds {} values")
.with_value(MAX_DECODED_ITEMS as u32)
.into());
}
for value in &item.list_of_values {
validate_property_identifier(
value.property_identifier,
"COVNotificationMultiple property cannot be ALL, OPTIONAL, or REQUIRED",
)?;
validate_raw_property_value(&value.value)?;
if let Some(time) = &value.time_of_change {
validate_actual_time(time)?;
}
has_time_of_change |= value.time_of_change.is_some();
}
}
if self.timestamp.is_some() != has_time_of_change {
return Err(Error::encode("COVNotificationMultiple timestamp must be present iff a time-of-change is present"));
}
Ok(())
}
pub fn decode(data: &[u8]) -> Result<Self, Error> {
let mut offset = 0;
let (subscriber_process_identifier, end) =
decode_ctx_unsigned::<u32>(data, offset, 0, "COVNotificationMultiple process-id")?;
offset = end;
let (initiating_device_identifier, end) =
decode_ctx_object_id(data, offset, 1, "COVNotificationMultiple device-id")?;
offset = end;
let (time_remaining, end) =
decode_ctx_unsigned::<u32>(data, offset, 2, "COVNotificationMultiple time-remaining")?;
offset = end;
let mut timestamp = None;
if offset < data.len() {
let (tag, _) = tags::decode_tag(data, offset)?;
if tag.is_opening_tag(3) {
let (date_time, end) = decode_date_time(data, offset, 3)?;
timestamp = Some(date_time);
offset = end;
}
}
let (tag, tag_end) = tags::decode_tag(data, offset)?;
if !tag.is_opening_tag(4) {
return Err(misplaced_tag(
data,
&tag,
Some(4),
offset,
"COVNotificationMultiple expected opening tag 4",
));
}
offset = tag_end;
let mut items = Vec::new();
let mut total_values = 0usize;
let mut has_time_of_change = false;
loop {
if offset >= data.len() {
return Err(Error::missing(
offset,
"COVNotificationMultiple missing closing tag 4",
));
}
let (tag, tag_end) = tags::decode_tag(data, offset)?;
if tag.is_closing_tag(4) {
if items.is_empty() {
return Err(reject(
RejectReason::PARAMETER_OUT_OF_RANGE,
"COVNotificationMultiple notification list is empty",
));
}
offset = tag_end;
break;
}
if items.len() >= MAX_DECODED_ITEMS {
return Err(reject(
RejectReason::BUFFER_OVERFLOW,
"too many notification items",
));
}
let (oid, end) =
decode_ctx_object_id(data, offset, 0, "COVNotificationMultiple monitored-id")?;
offset = end;
let (tag, tag_end) = tags::decode_tag(data, offset)?;
if !tag.is_opening_tag(1) {
return Err(misplaced_tag(
data,
&tag,
Some(1),
offset,
"COVNotificationMultiple expected opening tag 1",
));
}
offset = tag_end;
let mut values = Vec::new();
loop {
if offset >= data.len() {
return Err(Error::missing(
offset,
"COVNotificationMultiple missing closing tag 1",
));
}
let (tag, tag_end) = tags::decode_tag(data, offset)?;
if tag.is_closing_tag(1) {
if values.is_empty() {
return Err(reject(
RejectReason::PARAMETER_OUT_OF_RANGE,
"COVNotificationMultiple value list is empty",
));
}
offset = tag_end;
break;
}
if total_values >= MAX_DECODED_ITEMS {
return Err(reject(
RejectReason::BUFFER_OVERFLOW,
"too many notification values",
));
}
let (prop_id, end) = decode_ctx_unsigned::<u32>(
data,
offset,
0,
"COVNotificationMultiple property-id",
)?;
offset = end;
let property_identifier = PropertyIdentifier::from_raw(prop_id);
validate_decoded_property_identifier(
property_identifier,
"COVNotificationMultiple property cannot be ALL, OPTIONAL, or REQUIRED",
)?;
let mut array_index = None;
if next_is_context(data, offset, 1)? {
let (value, end) = decode_ctx_unsigned::<u32>(
data,
offset,
1,
"COVNotificationMultiple array-index",
)?;
array_index = Some(value);
offset = end;
}
let (tag, tag_end) = tags::decode_tag(data, offset)?;
if !tag.is_opening_tag(2) {
return Err(misplaced_tag(
data,
&tag,
Some(2),
offset,
"COVNotificationMultiple expected opening tag 2",
));
}
let (value_bytes, new_off) = tags::extract_context_value(data, tag_end, 2)?;
if value_bytes.is_empty() {
return Err(reject(
RejectReason::INVALID_DATA_ENCODING,
"COVNotificationMultiple property value is empty",
));
}
let value = value_bytes.to_vec();
offset = new_off;
let mut time_of_change = None;
if next_is_context(data, offset, 3)? {
let malformed = || {
reject(
RejectReason::INVALID_DATA_ENCODING,
"COVNotificationMultiple time-of-change is malformed",
)
};
let (content, end) = decode_ctx_fixed(
data,
offset,
3,
Fixed::TIME,
"COVNotificationMultiple time-of-change",
)
.map_err(|error| match error {
Error::BufferTooShort { .. } => error,
_ => malformed(),
})?;
let decoded_time = Time::decode(content).map_err(|_| malformed())?;
if !actual_time_is_valid(&decoded_time) {
return Err(reject(
RejectReason::INVALID_DATA_ENCODING,
"COVNotificationMultiple time-of-change is not an actual Time",
));
}
time_of_change = Some(decoded_time);
has_time_of_change = true;
offset = end;
}
values.push(COVNotificationValue {
property_identifier,
property_array_index: array_index,
value,
time_of_change,
});
total_values += 1;
}
items.push(COVNotificationItem {
monitored_object_identifier: oid,
list_of_values: values,
});
}
expect_end(data, offset, offset, "COVNotificationMultiple")?;
if timestamp.is_some() != has_time_of_change {
return Err(reject(
RejectReason::PARAMETER_OUT_OF_RANGE,
"COVNotificationMultiple timestamp/time-of-change mismatch",
));
}
Ok(Self {
subscriber_process_identifier,
initiating_device_identifier,
time_remaining,
timestamp,
list_of_cov_notifications: items,
})
}
}