use crate::dm::meta::ClusterDescriptor;
use crate::im::{AttributeId, AttributePath, ClusterId, EndpointId};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Endpoint<'a> {
pub id: EndpointId,
pub clusters: &'a [ClusterDescriptor<'a>],
pub device_types: &'a [crate::dm::meta::DeviceType],
pub clients: &'a [ClusterId],
}
impl<'a> Endpoint<'a> {
#[must_use]
pub const fn new(id: EndpointId, clusters: &'a [ClusterDescriptor<'a>]) -> Self {
Self {
id,
clusters,
device_types: &[],
clients: &[],
}
}
#[must_use]
pub const fn with_clients(mut self, clients: &'a [ClusterId]) -> Self {
self.clients = clients;
self
}
#[must_use]
pub const fn with_device_types(
mut self,
device_types: &'a [crate::dm::meta::DeviceType],
) -> Self {
self.device_types = device_types;
self
}
#[must_use]
pub fn has_device_type(&self, device_type: u32) -> bool {
self.device_types
.iter()
.any(|d| d.device_type == device_type)
}
}
impl<'a> Endpoint<'a> {
#[must_use]
pub fn cluster(&self, id: ClusterId) -> Option<&'a ClusterDescriptor<'a>> {
self.clusters
.binary_search_by_key(&id, |c| c.id)
.ok()
.and_then(|index| self.clusters.get(index))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Node<'a> {
pub endpoints: &'a [Endpoint<'a>],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Malformed {
Endpoints,
Clusters {
endpoint: EndpointId,
},
Cluster {
endpoint: EndpointId,
cluster: ClusterId,
},
}
#[derive(Debug, Clone, Copy)]
pub struct Resolved<'a> {
pub endpoint: EndpointId,
pub cluster: &'a ClusterDescriptor<'a>,
pub attribute: AttributeId,
}
impl Resolved<'_> {
#[must_use]
pub const fn path(&self) -> AttributePath {
AttributePath::attribute(self.endpoint, self.cluster.id, self.attribute)
}
}
#[derive(Debug, Clone, Copy)]
pub struct ResolvedCommand<'a> {
pub endpoint: EndpointId,
pub cluster: &'a ClusterDescriptor<'a>,
pub command: crate::dm::meta::CommandDescriptor,
}
impl ResolvedCommand<'_> {
#[must_use]
pub const fn path(&self) -> crate::im::CommandPath {
crate::im::CommandPath::command(self.endpoint, self.cluster.id, self.command.id)
}
}
impl<'a> Node<'a> {
#[must_use]
pub const fn new(endpoints: &'a [Endpoint<'a>]) -> Self {
Self { endpoints }
}
#[must_use]
pub fn endpoint(&self, id: EndpointId) -> Option<&'a Endpoint<'a>> {
self.endpoints
.binary_search_by_key(&id, |e| e.id)
.ok()
.and_then(|index| self.endpoints.get(index))
}
#[must_use]
pub fn cluster(
&self,
endpoint: EndpointId,
cluster: ClusterId,
) -> Option<&'a ClusterDescriptor<'a>> {
self.endpoint(endpoint)?.cluster(cluster)
}
pub fn validate(&self) -> Result<(), Malformed> {
if !self
.endpoints
.windows(2)
.all(|w| matches!(w, [a, b] if a.id < b.id))
{
return Err(Malformed::Endpoints);
}
for endpoint in self.endpoints {
if !endpoint
.clusters
.windows(2)
.all(|w| matches!(w, [a, b] if a.id < b.id))
{
return Err(Malformed::Clusters {
endpoint: endpoint.id,
});
}
for cluster in endpoint.clusters {
if !cluster.is_well_formed() {
return Err(Malformed::Cluster {
endpoint: endpoint.id,
cluster: cluster.id,
});
}
}
}
Ok(())
}
#[must_use]
pub fn expand(&self, path: &AttributePath) -> Expand<'a> {
self.expand_from(path, ExpandCursor::START)
}
#[must_use]
pub fn expand_from(&self, path: &AttributePath, cursor: ExpandCursor) -> Expand<'a> {
Expand {
endpoints: self.endpoints,
endpoint_filter: path.endpoint,
cluster_filter: path.cluster,
attribute_filter: path.attribute,
endpoint_index: cursor.endpoint,
cluster_index: cursor.cluster,
attribute_index: cursor.attribute,
}
}
pub fn resolve(
&self,
endpoint: EndpointId,
cluster: ClusterId,
attribute: AttributeId,
) -> Result<Resolved<'a>, Missing> {
let Some(endpoint_ref) = self.endpoint(endpoint) else {
return Err(Missing::Endpoint);
};
let Some(cluster_ref) = endpoint_ref.cluster(cluster) else {
return Err(Missing::Cluster);
};
if cluster_ref.attribute(attribute).is_none() {
return Err(Missing::Attribute);
}
Ok(Resolved {
endpoint,
cluster: cluster_ref,
attribute,
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct ResolvedEvent<'a> {
pub endpoint: EndpointId,
pub cluster: &'a ClusterDescriptor<'a>,
pub event: crate::dm::EventDescriptor,
}
impl ResolvedEvent<'_> {
#[must_use]
pub const fn path(&self) -> crate::im::EventPath {
crate::im::EventPath {
node: None,
endpoint: Some(self.endpoint),
cluster: Some(self.cluster.id),
event: Some(self.event.id),
is_urgent: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MissingCommand {
Endpoint,
Cluster,
Command,
}
impl MissingCommand {
#[must_use]
pub const fn status(self) -> crate::im::Status {
match self {
Self::Endpoint => crate::im::Status::UnsupportedEndpoint,
Self::Cluster => crate::im::Status::UnsupportedCluster,
Self::Command => crate::im::Status::UnsupportedCommand,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Missing {
Endpoint,
Cluster,
Attribute,
}
impl Missing {
#[must_use]
pub const fn status(self) -> crate::im::Status {
match self {
Self::Endpoint => crate::im::Status::UnsupportedEndpoint,
Self::Cluster => crate::im::Status::UnsupportedCluster,
Self::Attribute => crate::im::Status::UnsupportedAttribute,
}
}
}
impl<'a> Node<'a> {
pub fn resolve_event(
&self,
endpoint: EndpointId,
cluster: ClusterId,
event: crate::im::EventId,
) -> Result<ResolvedEvent<'a>, Missing> {
let Some(endpoint_ref) = self.endpoint(endpoint) else {
return Err(Missing::Endpoint);
};
let Some(cluster_ref) = endpoint_ref.cluster(cluster) else {
return Err(Missing::Cluster);
};
let Some(descriptor) = cluster_ref.events.iter().find(|e| e.id == event) else {
return Err(Missing::Attribute);
};
Ok(ResolvedEvent {
endpoint,
cluster: cluster_ref,
event: *descriptor,
})
}
pub fn resolve_command(
&self,
endpoint: EndpointId,
cluster: crate::im::ClusterId,
command: crate::im::CommandId,
) -> Result<ResolvedCommand<'a>, MissingCommand> {
let Some(endpoint_ref) = self.endpoint(endpoint) else {
return Err(MissingCommand::Endpoint);
};
let Some(cluster_ref) = endpoint_ref.cluster(cluster) else {
return Err(MissingCommand::Cluster);
};
let Some(descriptor) = cluster_ref.accepted_command(command) else {
return Err(MissingCommand::Command);
};
Ok(ResolvedCommand {
endpoint,
cluster: cluster_ref,
command: descriptor,
})
}
#[must_use]
pub fn expand_commands(&self, path: &crate::im::CommandPath) -> ExpandCommands<'a> {
ExpandCommands {
endpoints: self.endpoints,
endpoint_filter: path.endpoint,
cluster_filter: path.cluster,
command_filter: path.command,
endpoint_index: 0,
cluster_index: 0,
command_index: 0,
}
}
}
#[derive(Debug, Clone)]
pub struct ExpandCommands<'a> {
endpoints: &'a [Endpoint<'a>],
endpoint_filter: Option<EndpointId>,
cluster_filter: Option<crate::im::ClusterId>,
command_filter: Option<crate::im::CommandId>,
endpoint_index: usize,
cluster_index: usize,
command_index: usize,
}
impl<'a> Iterator for ExpandCommands<'a> {
type Item = ResolvedCommand<'a>;
fn next(&mut self) -> Option<Self::Item> {
loop {
let endpoint = self.endpoints.get(self.endpoint_index)?;
if self
.endpoint_filter
.is_some_and(|wanted| wanted != endpoint.id)
{
self.endpoint_index = self.endpoint_index.saturating_add(1);
self.cluster_index = 0;
self.command_index = 0;
continue;
}
let Some(cluster) = endpoint.clusters.get(self.cluster_index) else {
self.endpoint_index = self.endpoint_index.saturating_add(1);
self.cluster_index = 0;
self.command_index = 0;
continue;
};
if self
.cluster_filter
.is_some_and(|wanted| wanted != cluster.id)
{
self.cluster_index = self.cluster_index.saturating_add(1);
self.command_index = 0;
continue;
}
let Some(command) = cluster.accepted_commands.get(self.command_index) else {
self.cluster_index = self.cluster_index.saturating_add(1);
self.command_index = 0;
continue;
};
self.command_index = self.command_index.saturating_add(1);
if self
.command_filter
.is_some_and(|wanted| wanted != command.id)
{
continue;
}
return Some(ResolvedCommand {
endpoint: endpoint.id,
cluster,
command: *command,
});
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ExpandCursor {
endpoint: usize,
cluster: usize,
attribute: usize,
}
impl ExpandCursor {
pub const START: Self = Self {
endpoint: 0,
cluster: 0,
attribute: 0,
};
#[must_use]
pub const fn is_start(&self) -> bool {
self.endpoint == 0 && self.cluster == 0 && self.attribute == 0
}
}
#[derive(Debug, Clone)]
pub struct Expand<'a> {
endpoints: &'a [Endpoint<'a>],
endpoint_filter: Option<EndpointId>,
cluster_filter: Option<ClusterId>,
attribute_filter: Option<AttributeId>,
endpoint_index: usize,
cluster_index: usize,
attribute_index: usize,
}
impl Expand<'_> {
#[must_use]
pub const fn cursor(&self) -> ExpandCursor {
ExpandCursor {
endpoint: self.endpoint_index,
cluster: self.cluster_index,
attribute: self.attribute_index,
}
}
}
impl<'a> Iterator for Expand<'a> {
type Item = Resolved<'a>;
fn next(&mut self) -> Option<Self::Item> {
loop {
let endpoint = self.endpoints.get(self.endpoint_index)?;
if self
.endpoint_filter
.is_some_and(|wanted| wanted != endpoint.id)
{
self.endpoint_index = self.endpoint_index.saturating_add(1);
self.cluster_index = 0;
self.attribute_index = 0;
continue;
}
let Some(cluster) = endpoint.clusters.get(self.cluster_index) else {
self.endpoint_index = self.endpoint_index.saturating_add(1);
self.cluster_index = 0;
self.attribute_index = 0;
continue;
};
if self
.cluster_filter
.is_some_and(|wanted| wanted != cluster.id)
{
self.cluster_index = self.cluster_index.saturating_add(1);
self.attribute_index = 0;
continue;
}
let own = cluster.attributes.len();
let attribute = match self.attribute_index.checked_sub(own) {
Some(global) => crate::dm::global::ATTRIBUTE_IDS.get(global).copied(),
None => cluster.attributes.get(self.attribute_index).map(|a| a.id),
};
let Some(attribute) = attribute else {
self.cluster_index = self.cluster_index.saturating_add(1);
self.attribute_index = 0;
continue;
};
self.attribute_index = self.attribute_index.saturating_add(1);
if self
.attribute_filter
.is_some_and(|wanted| wanted != attribute)
{
continue;
}
return Some(Resolved {
endpoint: endpoint.id,
cluster,
attribute,
});
}
}
}