bacnet-encoding 0.13.0

BACnet ASN.1/BER encoding, APDU and NPDU codecs per ASHRAE 135-2020
Documentation
//! `Recipient_List` full ASN.1 framing (ASHRAE 135-2020 Clauses 12.21, 21).
//!
//! The property is a BACnetLIST of `BACnetDestination`, so on the wire it is
//! the destinations back to back with no list wrapper. A destination has
//! seven members and none carries a context tag, so they travel in this order
//! as application-tagged elements (the recipient CHOICE brings its own tags):
//!
//! | Member | Wire form |
//! |---|---|
//! | `valid-days` | Bit String (`BACnetDaysOfWeek`) |
//! | `from-time` | Time |
//! | `to-time` | Time |
//! | `recipient` | `BACnetRecipient` CHOICE, see below |
//! | `process-identifier` | Unsigned, 32-bit range |
//! | `issue-confirmed-notifications` | Boolean |
//! | `transitions` | Bit String (`BACnetEventTransitionBits`) |
//!
//! `BACnetRecipient` picks either `device [0]`, an object identifier, or
//! `address [1]`, a `BACnetAddress`. The device form tags a primitive
//! ObjectIdentifier — a context-specific PRIMITIVE tag 0, length 4.
//! `BACnetAddress` is constructed (a 16-bit network number, then the MAC
//! address as an OCTET STRING, both application-tagged), so `address [1]` is
//! an opening tag 1 / application-tagged Unsigned16 + OCTET STRING / closing
//! tag 1.
//!
//! A `BACnetAddress` MAC is at most [`BACnetAddress::MAX_MAC_LEN`] octets in
//! both directions: [`decode_recipient`] refuses a longer one before copying
//! it (#1124 for configured recipients, #1156 everywhere else) and
//! [`encode_recipient`] refuses to write one. Every other codec that carries a
//! `BACnetAddress` (ValueSource, the AuditLogQuery filters) shares
//! [`check_decoded_mac_len`] and [`check_encoded_mac_len`], so the bound has
//! one definition. The source addresses the stack learns off the network fit
//! it too: the NPDU codec bounds SADR the same way (#1141), and the network
//! layer drops a frame from a longer link-layer source MAC (#1198).

#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};

use bacnet_types::bitstring::{DaysOfWeek, EventTransitionBits};
use bacnet_types::constructed::{BACnetAddress, BACnetDestination, BACnetRecipient};
use bacnet_types::error::Error;
use bacnet_types::primitives::Time;
use bacnet_types::MacAddr;
use bytes::BytesMut;

use crate::primitives;
use crate::tags::{self, TagClass};

use super::tagged::{
    contents, decode_app_bit_string, decode_app_unsigned, decode_ctx_object_id, expect_closing,
    misplaced_tag,
};
use super::MAX_FRAMED_ITEMS;

/// The message for a `BACnetAddress` MAC of `length` octets, or `None` when it
/// fits [`BACnetAddress::MAX_MAC_LEN`]. The one place the bound is compared.
fn mac_len_excess(what: &str, length: usize) -> Option<String> {
    (length > BACnetAddress::MAX_MAC_LEN).then(|| {
        format!(
            "{what}: mac-address of {length} octets exceeds the {}-octet limit",
            BACnetAddress::MAX_MAC_LEN
        )
    })
}

/// Refuse, as a decode error at `offset`, a `BACnetAddress` mac-address whose
/// OCTET STRING holds more than [`BACnetAddress::MAX_MAC_LEN`] octets (#1156),
/// or any other OCTET STRING that names a MAC on a data link, such as the
/// You-Are device MAC (#1200). Decoders call it on the tag's length, before
/// copying any octet.
pub fn check_decoded_mac_len(length: usize, offset: usize, what: &str) -> Result<(), Error> {
    mac_len_excess(what, length).map_or(Ok(()), |message| Err(Error::overflow(offset, message)))
}

/// Refuse to encode a MAC longer than [`BACnetAddress::MAX_MAC_LEN`] octets,
/// which [`check_decoded_mac_len`] would refuse on the way back in: a
/// `BACnetAddress` mac-address (#1156) or the You-Are device MAC (#1200).
pub fn check_encoded_mac_len(mac: &[u8], what: &str) -> Result<(), Error> {
    mac_len_excess(what, mac.len()).map_or(Ok(()), |message| Err(Error::Encoding(message)))
}

/// [`check_encoded_mac_len`] for the address form of a recipient; the device
/// form always encodes.
pub(super) fn check_encoded_recipient(recipient: &BACnetRecipient) -> Result<(), Error> {
    match recipient {
        BACnetRecipient::Device(_) => Ok(()),
        BACnetRecipient::Address(address) => {
            check_encoded_mac_len(&address.mac_address, "BACnetRecipient")
        }
    }
}

/// Encode one [`BACnetDestination`].
///
/// The two bit-string members travel MSB-first per Clause 20.2.10:
/// `BACnetDaysOfWeek` is 7 bits (monday(0) at 0x80), so the fill octet has 1
/// unused bit; `BACnetEventTransitionBits` is 3 bits, so 5 unused.
///
/// A recipient MAC past [`BACnetAddress::MAX_MAC_LEN`] octets is an error,
/// returned before `buf` changes.
pub fn encode_destination(buf: &mut BytesMut, dest: &BACnetDestination) -> Result<(), Error> {
    check_encoded_recipient(&dest.recipient)?;
    write_destination(buf, dest);
    Ok(())
}

/// Write a destination whose recipient has passed [`check_encoded_recipient`].
fn write_destination(buf: &mut BytesMut, dest: &BACnetDestination) {
    primitives::encode_app_bit_string(buf, 1, &[dest.valid_days.to_bacnet()]);
    primitives::encode_app_time(buf, &dest.from_time);
    primitives::encode_app_time(buf, &dest.to_time);
    write_recipient(buf, &dest.recipient);
    primitives::encode_app_unsigned(buf, dest.process_identifier as u64);
    primitives::encode_app_boolean(buf, dest.issue_confirmed_notifications);
    primitives::encode_app_bit_string(buf, 5, &[dest.transitions.to_bacnet()]);
}

/// Encode a `BACnetLIST of BACnetDestination`: plain concatenation, no
/// wrapper (Clause 12.21 `Recipient_List`). Every destination is checked
/// before `buf` changes, so a refused list writes nothing.
pub fn encode_destination_list(
    buf: &mut BytesMut,
    destinations: &[BACnetDestination],
) -> Result<(), Error> {
    for dest in destinations {
        check_encoded_recipient(&dest.recipient)?;
    }
    for dest in destinations {
        write_destination(buf, dest);
    }
    Ok(())
}

/// Encode a [`BACnetRecipient`]. An address MAC past
/// [`BACnetAddress::MAX_MAC_LEN`] octets is an error, returned before `buf`
/// changes.
pub fn encode_recipient(buf: &mut BytesMut, recipient: &BACnetRecipient) -> Result<(), Error> {
    check_encoded_recipient(recipient)?;
    write_recipient(buf, recipient);
    Ok(())
}

/// Write a recipient that has passed [`check_encoded_recipient`].
pub(super) fn write_recipient(buf: &mut BytesMut, recipient: &BACnetRecipient) {
    match recipient {
        BACnetRecipient::Device(oid) => {
            // device [0] tags the primitive ObjectIdentifier.
            tags::encode_tag(buf, 0, TagClass::Context, 4);
            buf.extend_from_slice(&oid.encode());
        }
        BACnetRecipient::Address(address) => {
            // address [1] tags the constructed BACnetAddress.
            tags::encode_opening_tag(buf, 1);
            primitives::encode_app_unsigned(buf, address.network_number as u64);
            primitives::encode_app_octet_string(buf, &address.mac_address);
            tags::encode_closing_tag(buf, 1);
        }
    }
}

/// Decode one [`BACnetRecipient`] at `offset`.
///
/// An address whose MAC is longer than [`BACnetAddress::MAX_MAC_LEN`] octets
/// names no node this stack reaches, so it is a decode error, raised before
/// the MAC is copied. That holds for a configured recipient (a Recipient_List
/// destination or the Audit_Notification_Recipient, #1124) and for one a
/// service carries or the stack reports (COV subscription lists, audit
/// records, enrollment filters, #1156).
pub fn decode_recipient(data: &[u8], offset: usize) -> Result<(BACnetRecipient, usize), Error> {
    let what = "BACnetRecipient";
    let (tag, pos) = tags::decode_tag(data, offset)?;
    if tag.is_context(0) {
        // device [0] tags the primitive ObjectIdentifier.
        let (device, end) = decode_ctx_object_id(data, offset, 0, "BACnetRecipient device")?;
        return Ok((BACnetRecipient::Device(device), end));
    }
    if tag.is_opening_tag(1) {
        // network-number Unsigned16
        let (network_number, pos) = decode_app_unsigned::<u64>(data, pos, what)?;
        let network_number = u16::try_from(network_number).map_err(|_| {
            Error::out_of_range(pos, format!("{what}: network-number exceeds Unsigned16"))
        })?;
        // mac-address OCTET STRING (a zero-length string is a broadcast)
        let (mac_address, pos) = decode_app_mac_address(data, pos, what)?;
        let end = expect_closing(data, pos, 1, "BACnetRecipient address")?;
        return Ok((
            BACnetRecipient::Address(BACnetAddress {
                network_number,
                mac_address,
            }),
            end,
        ));
    }
    Err(misplaced_tag(
        data,
        &tag,
        None,
        offset,
        format!(
            "{what}: expected [0] (device) or [1] (address), got {}",
            if tag.is_opening {
                format!("opening tag [{}]", tag.number)
            } else if tag.is_closing {
                format!("closing tag [{}]", tag.number)
            } else {
                match tag.class {
                    TagClass::Context => format!("primitive context tag [{}]", tag.number),
                    TagClass::Application => format!("application tag {}", tag.number),
                }
            }
        ),
    ))
}

/// Decode one [`BACnetDestination`] at `offset`; returns it and the offset
/// past the last member. Its recipient decodes through [`decode_recipient`],
/// so an address MAC past [`BACnetAddress::MAX_MAC_LEN`] octets is refused
/// (#1124).
pub fn decode_destination(data: &[u8], offset: usize) -> Result<(BACnetDestination, usize), Error> {
    let what = "BACnetDestination";
    // valid-days: BACnetDaysOfWeek (bit 0 = Monday, MSB-first on the wire).
    let ((days_unused, days_data), pos) = decode_app_bit_string(data, offset, what)?;
    check_fixed_bit_string(
        days_unused,
        &days_data,
        1,
        "valid-days (BACnetDaysOfWeek: 7 bits)",
    )?;
    let valid_days = DaysOfWeek::from_bacnet(&days_data);
    // from-time / to-time.
    let (from_time, pos) = decode_app_time(data, pos, what)?;
    let (to_time, pos) = decode_app_time(data, pos, what)?;
    // recipient CHOICE, its address MAC bounded.
    let (recipient, pos) = decode_recipient(data, pos)?;
    // process-identifier Unsigned32.
    let (process_identifier, pos) = decode_app_unsigned::<u64>(data, pos, what)?;
    let process_identifier = u32::try_from(process_identifier).map_err(|_| {
        Error::out_of_range(
            pos,
            format!("{what}: process-identifier exceeds Unsigned32"),
        )
    })?;
    // issue-confirmed-notifications BOOLEAN (application-tagged: the value
    // rides in the tag's L/V/T bits with no contents octets).
    let (tag, pos) = tags::decode_tag(data, pos)?;
    if tag.class != TagClass::Application || tag.number != tags::app_tag::BOOLEAN {
        return Err(misplaced_tag(
            data,
            &tag,
            None,
            pos,
            format!("{what}: expected application-tagged BOOLEAN"),
        ));
    }
    let issue_confirmed_notifications = tag.length != 0;
    // transitions: BACnetEventTransitionBits (3 bits).
    let ((transitions_unused, transitions_data), pos) = decode_app_bit_string(data, pos, what)?;
    check_fixed_bit_string(
        transitions_unused,
        &transitions_data,
        5,
        "transitions (BACnetEventTransitionBits: 3 bits)",
    )?;
    let transitions = EventTransitionBits::from_bacnet(&transitions_data);

    Ok((
        BACnetDestination {
            valid_days,
            from_time,
            to_time,
            recipient,
            process_identifier,
            issue_confirmed_notifications,
            transitions,
        },
        pos,
    ))
}

/// Decode a whole `Recipient_List` (`BACnetLIST of BACnetDestination` —
/// concatenated entries). Every entry must parse; trailing garbage errors.
pub fn decode_destination_list(data: &[u8]) -> Result<Vec<BACnetDestination>, Error> {
    let mut destinations = Vec::new();
    let mut pos = 0;
    while pos < data.len() {
        if destinations.len() >= MAX_FRAMED_ITEMS {
            return Err(Error::overflow(
                pos,
                "Recipient_List: destination count exceeds limit",
            ));
        }
        let (dest, new_pos) = decode_destination(data, pos)?;
        destinations.push(dest);
        pos = new_pos;
    }
    Ok(destinations)
}

// ---------------------------------------------------------------------------
// Small application-tagged member helpers
// ---------------------------------------------------------------------------

/// Decode the application-tagged OCTET STRING of a `BACnetAddress` MAC,
/// refusing one longer than [`BACnetAddress::MAX_MAC_LEN`] octets before
/// copying it.
pub(super) fn decode_app_mac_address(
    data: &[u8],
    offset: usize,
    what: &str,
) -> Result<(MacAddr, usize), Error> {
    let (tag, pos) = tags::decode_tag(data, offset)?;
    if tag.class != TagClass::Application || tag.number != tags::app_tag::OCTET_STRING {
        return Err(misplaced_tag(
            data,
            &tag,
            None,
            offset,
            format!("{what}: expected application-tagged OCTET STRING"),
        ));
    }
    check_decoded_mac_len(tag.length as usize, offset, what)?;
    let (mac, end) = contents(data, pos, tag.length)?;
    Ok((MacAddr::from_slice(mac), end))
}

/// Decode an application-tagged Time member (exactly 4 contents octets).
fn decode_app_time(data: &[u8], offset: usize, what: &str) -> Result<(Time, usize), Error> {
    let (tag, pos) = tags::decode_tag(data, offset)?;
    if tag.class != TagClass::Application || tag.number != tags::app_tag::TIME {
        return Err(misplaced_tag(
            data,
            &tag,
            None,
            offset,
            format!("{what}: expected application-tagged Time"),
        ));
    }
    if tag.length != 4 {
        return Err(Error::decoding(
            offset,
            format!(
                "{what}: Time has {} contents octets, expected 4",
                tag.length
            ),
        ));
    }
    let (octets, end) = contents(data, pos, 4)?;
    Ok((Time::decode(octets)?, end))
}

/// Fixed-width bit-string member validation. Per Clause 20.2.10 (initial
/// octet = unused-bit count; defined bits MSB-first) and the Clause 21
/// productions, the named-bit sets here fit in exactly one subsequent
/// octet: `BACnetDaysOfWeek` has 7 defined bits (unused_bits 1) and
/// `BACnetEventTransitionBits` has 3 (unused_bits 5). A zero-length,
/// overlong, or mismatched-unused-bits encoding is not a destination a
/// notifier may act on — reject it rather than unpacking an all-zero
/// (dormant) one.
fn check_fixed_bit_string(
    unused_bits: u8,
    data: &[u8],
    expected_unused: u8,
    name: &str,
) -> Result<(), Error> {
    if data.len() != 1 || unused_bits != expected_unused {
        return Err(Error::decoding(
            0,
            format!(
                "{name}: expected 1 content octet with unused_bits {expected_unused} \
                 (got {} content octet(s), unused_bits {unused_bits})",
                data.len()
            ),
        ));
    }
    Ok(())
}