use crate::error::{Error, ErrorCode, Result, bail};
use crate::msg::NodeId;
use crate::tlv::{ContainerKind, Element, Tag, TlvReader, TlvWriter, Value, set_once};
pub type EndpointId = u16;
pub type ClusterId = u32;
pub type AttributeId = u32;
pub type EventId = u32;
pub type CommandId = u32;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ListIndex {
At(u16),
Append,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct AttributePath {
pub enable_tag_compression: bool,
pub node: Option<NodeId>,
pub endpoint: Option<EndpointId>,
pub cluster: Option<ClusterId>,
pub attribute: Option<AttributeId>,
pub list_index: Option<ListIndex>,
pub wildcard_path_flags: Option<WildcardPathFlags>,
pub wildcard_filter_configuration_version: Option<u32>,
}
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct WildcardPathFlags: u32 {
const SKIP_ROOT_NODE = 0x0000_0001;
const SKIP_GLOBAL_ATTRIBUTES = 0x0000_0002;
const SKIP_ATTRIBUTE_LIST = 0x0000_0004;
const SKIP_COMMAND_LISTS = 0x0000_0010;
const SKIP_CUSTOM_ELEMENTS = 0x0000_0020;
const SKIP_FIXED_ATTRIBUTES = 0x0000_0040;
const SKIP_CHANGES_OMITTED = 0x0000_0080;
const SKIP_DIAGNOSTICS_CLUSTERS = 0x0000_0100;
}
}
impl AttributePath {
#[must_use]
pub const fn is_valid_for_read(&self) -> bool {
match (self.cluster, self.attribute) {
(None, Some(attribute)) => crate::dm::global::is_global(attribute),
_ => true,
}
}
#[must_use]
pub const fn attribute(
endpoint: EndpointId,
cluster: ClusterId,
attribute: AttributeId,
) -> Self {
Self {
enable_tag_compression: false,
node: None,
endpoint: Some(endpoint),
cluster: Some(cluster),
attribute: Some(attribute),
list_index: None,
wildcard_path_flags: None,
wildcard_filter_configuration_version: None,
}
}
#[must_use]
pub const fn cluster(endpoint: EndpointId, cluster: ClusterId) -> Self {
Self {
endpoint: Some(endpoint),
cluster: Some(cluster),
..Self::wildcard()
}
}
#[must_use]
pub const fn wildcard() -> Self {
Self {
enable_tag_compression: false,
node: None,
endpoint: None,
cluster: None,
attribute: None,
list_index: None,
wildcard_path_flags: None,
wildcard_filter_configuration_version: None,
}
}
#[must_use]
pub const fn concrete(&self) -> Option<(EndpointId, ClusterId, AttributeId)> {
match (self.endpoint, self.cluster, self.attribute) {
(Some(e), Some(c), Some(a)) => Some((e, c, a)),
_ => None,
}
}
#[must_use]
pub const fn has_wildcard(&self) -> bool {
self.concrete().is_none()
}
pub fn encode(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_list(tag)?;
if self.enable_tag_compression {
w.bool(Tag::Context(0), true)?;
}
if let Some(node) = self.node {
w.unsigned(Tag::Context(1), node.0)?;
}
if let Some(endpoint) = self.endpoint {
w.unsigned(Tag::Context(2), u64::from(endpoint))?;
}
if let Some(cluster) = self.cluster {
w.unsigned(Tag::Context(3), u64::from(cluster))?;
}
if let Some(attribute) = self.attribute {
w.unsigned(Tag::Context(4), u64::from(attribute))?;
}
match self.list_index {
Some(ListIndex::At(index)) => w.unsigned(Tag::Context(5), u64::from(index))?,
Some(ListIndex::Append) => w.null(Tag::Context(5))?,
None => {}
}
if let Some(flags) = self.wildcard_path_flags {
w.unsigned(Tag::Context(6), u64::from(flags.bits()))?;
}
if let Some(version) = self.wildcard_filter_configuration_version {
w.unsigned(Tag::Context(7), u64::from(version))?;
}
w.end_container()
}
pub fn decode(reader: &mut TlvReader<'_>) -> Result<Self> {
let mut out = Self::default();
let mut enable_tag_compression = None;
let mut node = None;
let mut endpoint = None;
let mut cluster = None;
let mut attribute = None;
let mut list_index = None;
let mut flags = None;
let mut version = None;
loop {
let Some(element) = reader.next_element()? else {
bail!(TlvTruncated)
};
if element.value == Value::EndOfContainer {
break;
}
match element.tag.context() {
Some(0) => set_once(&mut enable_tag_compression, element.bool()?)?,
Some(1) => set_once(&mut node, NodeId(element.unsigned()?))?,
Some(2) => set_once(&mut endpoint, narrow(element.unsigned()?)?)?,
Some(3) => set_once(&mut cluster, narrow32(element.unsigned()?)?)?,
Some(4) => set_once(&mut attribute, narrow32(element.unsigned()?)?)?,
Some(5) => {
let value = match element.value {
Value::Null => ListIndex::Append,
Value::Unsigned(_) => ListIndex::At(narrow(element.unsigned()?)?),
_ => bail!(TlvWrongType),
};
set_once(&mut list_index, value)?;
}
Some(6) => {
let raw = narrow32(element.unsigned()?)?;
set_once(&mut flags, WildcardPathFlags::from_bits_retain(raw))?;
}
Some(7) => set_once(&mut version, narrow32(element.unsigned()?)?)?,
_ => reader.skip_value(&element)?,
}
}
out.enable_tag_compression = enable_tag_compression.unwrap_or(false);
out.node = node;
out.endpoint = endpoint;
out.cluster = cluster;
out.attribute = attribute;
out.list_index = list_index;
out.wildcard_path_flags = flags;
out.wildcard_filter_configuration_version = version;
Ok(out)
}
pub fn from_element(reader: &mut TlvReader<'_>, element: &Element<'_>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::List) {
bail!(TlvWrongType)
}
Self::decode(reader)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ClusterPath {
pub node: Option<NodeId>,
pub endpoint: Option<EndpointId>,
pub cluster: Option<ClusterId>,
}
impl ClusterPath {
#[must_use]
pub const fn new(endpoint: EndpointId, cluster: ClusterId) -> Self {
Self {
node: None,
endpoint: Some(endpoint),
cluster: Some(cluster),
}
}
pub fn encode(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_list(tag)?;
if let Some(node) = self.node {
w.unsigned(Tag::Context(0), node.0)?;
}
if let Some(endpoint) = self.endpoint {
w.unsigned(Tag::Context(1), u64::from(endpoint))?;
}
if let Some(cluster) = self.cluster {
w.unsigned(Tag::Context(2), u64::from(cluster))?;
}
w.end_container()
}
pub fn decode(reader: &mut TlvReader<'_>) -> Result<Self> {
let mut node = None;
let mut endpoint = None;
let mut cluster = None;
loop {
let Some(element) = reader.next_element()? else {
bail!(TlvTruncated)
};
if element.value == Value::EndOfContainer {
break;
}
match element.tag.context() {
Some(0) => set_once(&mut node, NodeId(element.unsigned()?))?,
Some(1) => set_once(&mut endpoint, narrow(element.unsigned()?)?)?,
Some(2) => set_once(&mut cluster, narrow32(element.unsigned()?)?)?,
_ => reader.skip_value(&element)?,
}
}
Ok(Self {
node,
endpoint,
cluster,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct EventPath {
pub node: Option<NodeId>,
pub endpoint: Option<EndpointId>,
pub cluster: Option<ClusterId>,
pub event: Option<EventId>,
pub is_urgent: Option<bool>,
}
impl EventPath {
#[must_use]
pub const fn event(endpoint: EndpointId, cluster: ClusterId, event: EventId) -> Self {
Self {
node: None,
endpoint: Some(endpoint),
cluster: Some(cluster),
event: Some(event),
is_urgent: None,
}
}
pub fn encode(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_list(tag)?;
if let Some(node) = self.node {
w.unsigned(Tag::Context(0), node.0)?;
}
if let Some(endpoint) = self.endpoint {
w.unsigned(Tag::Context(1), u64::from(endpoint))?;
}
if let Some(cluster) = self.cluster {
w.unsigned(Tag::Context(2), u64::from(cluster))?;
}
if let Some(event) = self.event {
w.unsigned(Tag::Context(3), u64::from(event))?;
}
if let Some(urgent) = self.is_urgent {
w.bool(Tag::Context(4), urgent)?;
}
w.end_container()
}
pub fn decode(reader: &mut TlvReader<'_>) -> Result<Self> {
let mut node = None;
let mut endpoint = None;
let mut cluster = None;
let mut event = None;
let mut is_urgent = None;
loop {
let Some(element) = reader.next_element()? else {
bail!(TlvTruncated)
};
if element.value == Value::EndOfContainer {
break;
}
match element.tag.context() {
Some(0) => set_once(&mut node, NodeId(element.unsigned()?))?,
Some(1) => set_once(&mut endpoint, narrow(element.unsigned()?)?)?,
Some(2) => set_once(&mut cluster, narrow32(element.unsigned()?)?)?,
Some(3) => set_once(&mut event, narrow32(element.unsigned()?)?)?,
Some(4) => set_once(&mut is_urgent, element.bool()?)?,
_ => reader.skip_value(&element)?,
}
}
Ok(Self {
node,
endpoint,
cluster,
event,
is_urgent,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CommandPath {
pub endpoint: Option<EndpointId>,
pub cluster: Option<ClusterId>,
pub command: Option<CommandId>,
}
impl CommandPath {
#[must_use]
pub const fn command(endpoint: EndpointId, cluster: ClusterId, command: CommandId) -> Self {
Self {
endpoint: Some(endpoint),
cluster: Some(cluster),
command: Some(command),
}
}
#[must_use]
pub const fn concrete(&self) -> Option<(EndpointId, ClusterId, CommandId)> {
match (self.endpoint, self.cluster, self.command) {
(Some(e), Some(c), Some(cmd)) => Some((e, c, cmd)),
_ => None,
}
}
pub fn encode(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_list(tag)?;
if let Some(endpoint) = self.endpoint {
w.unsigned(Tag::Context(0), u64::from(endpoint))?;
}
if let Some(cluster) = self.cluster {
w.unsigned(Tag::Context(1), u64::from(cluster))?;
}
if let Some(command) = self.command {
w.unsigned(Tag::Context(2), u64::from(command))?;
}
w.end_container()
}
pub fn decode(reader: &mut TlvReader<'_>) -> Result<Self> {
let mut endpoint = None;
let mut cluster = None;
let mut command = None;
loop {
let Some(element) = reader.next_element()? else {
bail!(TlvTruncated)
};
if element.value == Value::EndOfContainer {
break;
}
match element.tag.context() {
Some(0) => set_once(&mut endpoint, narrow(element.unsigned()?)?)?,
Some(1) => set_once(&mut cluster, narrow32(element.unsigned()?)?)?,
Some(2) => set_once(&mut command, narrow32(element.unsigned()?)?)?,
_ => reader.skip_value(&element)?,
}
}
Ok(Self {
endpoint,
cluster,
command,
})
}
}
fn narrow(value: u64) -> Result<u16> {
u16::try_from(value).map_err(|_| Error::new(ErrorCode::TlvOutOfRange))
}
fn narrow32(value: u64) -> Result<u32> {
u32::try_from(value).map_err(|_| Error::new(ErrorCode::TlvOutOfRange))
}