bacnet-server 0.15.0

BACnet server: APDU dispatch and service handlers
Documentation
use super::*;
use PropertyIdentifier as P;
use bacnet_objects::{notification_class::NotificationClass, traits::BACnetObject};
use bacnet_types::bitstring::{DaysOfWeek, EventTransitionBits};
use bacnet_types::constructed::{
    BACnetDestination, BACnetRecipient, PropertyReference, ReadAccessSpecification,
};
use bacnet_types::error::RemoteError;
use bacnet_types::primitives::Time;

#[test]
fn rpm_notification_class_indexed_reads_and_constructed_bytes_are_unchanged() {
    let mut object = NotificationClass::new(7, "NC-7").unwrap();
    object.priority = [12, 34, 56];
    object.ack_required = EventTransitionBits::TO_OFFNORMAL | EventTransitionBits::TO_NORMAL;
    object
        .add_destination(BACnetDestination {
            valid_days: DaysOfWeek::all(),
            from_time: Time {
                hour: 0,
                minute: 0,
                second: 0,
                hundredths: 0,
            },
            to_time: Time {
                hour: 23,
                minute: 59,
                second: 59,
                hundredths: 99,
            },
            recipient: BACnetRecipient::Device(
                ObjectIdentifier::new(ObjectType::DEVICE, 42).unwrap(),
            ),
            process_identifier: 123,
            issue_confirmed_notifications: true,
            transitions: EventTransitionBits::TO_OFFNORMAL | EventTransitionBits::TO_NORMAL,
        })
        .unwrap();
    let oid = object.object_identifier();
    let mut db = ObjectDatabase::new();
    db.add(Box::new(object)).unwrap();
    // Independently spelled application/choice bytes; no metadata or encoder oracle.
    type ExpectedRead = Result<&'static [u8], ErrorCode>;
    let cases: &[(P, Option<u32>, ExpectedRead)] = &[
        (P::PRIORITY, None, Ok(&[0x21, 12, 0x21, 34, 0x21, 56])),
        (P::PRIORITY, Some(0), Ok(&[0x21, 3])),
        (P::PRIORITY, Some(1), Ok(&[0x21, 12])),
        (P::PRIORITY, Some(2), Ok(&[0x21, 34])),
        (P::PRIORITY, Some(3), Ok(&[0x21, 56])),
        (P::PRIORITY, Some(4), Err(ErrorCode::INVALID_ARRAY_INDEX)),
        (P::ACK_REQUIRED, None, Ok(&[0x82, 5, 0xa0])),
        (
            P::ACK_REQUIRED,
            Some(0),
            Err(ErrorCode::PROPERTY_IS_NOT_AN_ARRAY),
        ),
        (
            P::RECIPIENT_LIST,
            None,
            Ok(&[
                0x82, 1, 0xfe, 0xb4, 0, 0, 0, 0, 0xb4, 23, 59, 59, 99, 0x0c, 2, 0, 0, 42, 0x21,
                123, 0x11, 0x82, 5, 0xa0,
            ]),
        ),
        (
            P::RECIPIENT_LIST,
            Some(0),
            Err(ErrorCode::PROPERTY_IS_NOT_AN_ARRAY),
        ),
        (
            P::PROPERTY_LIST,
            None,
            // Table 12-24 has no Out_Of_Service (#1064).
            Ok(&[
                0x91, 28, 0x91, 111, 0x91, 36, 0x91, 103, 0x91, 17, 0x91, 86, 0x91, 1, 0x91, 102,
            ]),
        ),
        (P::PROPERTY_LIST, Some(0), Ok(&[0x21, 8])),
        (P::PROPERTY_LIST, Some(1), Ok(&[0x91, 28])),
        (P::PROPERTY_LIST, Some(8), Ok(&[0x91, 102])),
        (
            P::PROPERTY_LIST,
            Some(9),
            Err(ErrorCode::INVALID_ARRAY_INDEX),
        ),
    ];
    let mut request_bytes = BytesMut::new();
    ReadPropertyMultipleRequest {
        list_of_read_access_specs: vec![ReadAccessSpecification {
            object_identifier: oid,
            list_of_property_references: cases
                .iter()
                .map(|&(p, i, _)| PropertyReference {
                    property_identifier: p,
                    property_array_index: i,
                })
                .collect(),
        }],
    }
    .encode(&mut request_bytes)
    .unwrap();
    let mut legacy = BytesMut::new();
    handle_read_property_multiple(&db, &request_bytes, &mut legacy).unwrap();
    let ack = ReadPropertyMultipleACK::decode(&legacy).unwrap();
    assert_eq!(ack.list_of_read_access_results.len(), 1);
    let access = &ack.list_of_read_access_results[0];
    assert_eq!(access.object_identifier, oid);
    assert_eq!(access.list_of_results.len(), cases.len());
    for (result, &(p, i, expected)) in access.list_of_results.iter().zip(cases) {
        assert_eq!(result.property_identifier, p);
        // These table errors identify non-arrays or absent optional rows.
        let response_index = if matches!(
            expected,
            Err(ErrorCode::PROPERTY_IS_NOT_AN_ARRAY | ErrorCode::UNKNOWN_PROPERTY)
        ) {
            None
        } else {
            i
        };
        assert_eq!(result.property_array_index, response_index);
        let mut request = BytesMut::new();
        ReadPropertyRequest {
            object_identifier: oid,
            property_identifier: p,
            property_array_index: i,
        }
        .encode(&mut request);
        let mut response = BytesMut::new();
        let rp = handle_read_property(&db, &request, &mut response);
        match expected {
            Ok(bytes) => {
                assert!(result.error.is_none());
                assert_eq!(result.property_value.as_deref(), Some(bytes));
                rp.unwrap();
                let rp_ack = ReadPropertyACK::decode(&response).unwrap();
                assert_eq!(rp_ack.object_identifier, oid);
                assert_eq!(rp_ack.property_identifier, p);
                assert_eq!(rp_ack.property_array_index, i);
                assert_eq!(rp_ack.property_value, bytes);
            }
            Err(expected) => {
                assert!(result.property_value.is_none());
                assert_eq!(result.error, Some((ErrorClass::PROPERTY, expected)));
                assert!(
                    matches!(rp, Err(Error::Remote(RemoteError::Error { class, code }))
                    if class == ErrorClass::PROPERTY && code == expected)
                );
                assert!(response.is_empty());
            }
        }
    }
    let budget = crate::server::ReadPropertyMultipleBudget {
        max_result_elements: cases.len(),
        max_service_ack_bytes: legacy.len(),
    };
    use crate::handlers::{ReadFailure, rpm_budget::handle_rpm_budgeted};
    let mut bounded = BytesMut::new();
    handle_rpm_budgeted(&db, &request_bytes, &mut bounded, budget).unwrap();
    assert_eq!(bounded, legacy);
    let mut prefix = BytesMut::from(&b"prefix"[..]);
    assert!(matches!(
        handle_rpm_budgeted(
            &db,
            &request_bytes,
            &mut prefix,
            crate::server::ReadPropertyMultipleBudget {
                max_result_elements: cases.len() - 1,
                ..budget
            }
        ),
        Err(ReadFailure::Work)
    ));
    assert_eq!(&prefix[..], b"prefix");
    assert!(matches!(
        handle_rpm_budgeted(
            &db,
            &request_bytes,
            &mut prefix,
            crate::server::ReadPropertyMultipleBudget {
                max_service_ack_bytes: legacy.len() - 1,
                ..budget
            }
        ),
        Err(ReadFailure::Bytes)
    ));
    assert_eq!(&prefix[..], b"prefix");
}