use crate::dm::conformance::{Conform, Conformance, Supports};
use crate::dm::node::Endpoint;
use crate::im::{AttributeId, ClusterId, CommandId, EventId};
#[derive(Debug, Clone, Copy)]
pub struct RequiredCluster {
pub id: ClusterId,
pub name: &'static str,
pub server: bool,
pub conform: Conform,
pub features: &'static [(&'static str, Conform)],
pub attributes: &'static [(&'static str, Conform)],
pub commands: &'static [(&'static str, Conform)],
pub events: &'static [(&'static str, Conform)],
}
#[derive(Debug, Clone, Copy)]
pub struct DeviceType {
pub id: u32,
pub name: &'static str,
pub revision: u16,
pub class: &'static str,
pub scope: &'static str,
pub clusters: &'static [RequiredCluster],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Defect {
MissingCluster(ClusterId),
DisallowedCluster(ClusterId),
MissingFeature {
cluster: ClusterId,
code: &'static str,
},
MissingClient(ClusterId),
MissingElement {
cluster: ClusterId,
name: &'static str,
},
}
struct Furnished<'a> {
endpoint: &'a Endpoint<'a>,
}
impl Supports for Furnished<'_> {
fn feature_map(&self) -> u32 {
0
}
fn has_attribute(&self, _id: AttributeId) -> bool {
false
}
fn has_command(&self, _id: CommandId) -> bool {
false
}
fn has_event(&self, _id: EventId) -> bool {
false
}
fn has_cluster(&self, id: ClusterId) -> bool {
self.endpoint
.clusters
.iter()
.any(|cluster| cluster.id == id)
}
}
impl DeviceType {
pub fn validate(
&self,
endpoint: &Endpoint<'_>,
spec_of: impl Fn(ClusterId) -> Option<&'static crate::dm::spec::Cluster>,
mut found: impl FnMut(Defect),
) {
let furnished = Furnished { endpoint };
for required in self.clusters {
let verdict = required.conform.verdict(&furnished);
if !required.server {
if verdict == Conformance::Mandatory && !endpoint.clients.contains(&required.id) {
found(Defect::MissingClient(required.id));
}
continue;
}
let present = furnished.has_cluster(required.id);
match verdict {
Conformance::Mandatory if !present => {
found(Defect::MissingCluster(required.id));
continue;
}
Conformance::Disallowed if present => {
found(Defect::DisallowedCluster(required.id));
continue;
}
_ => {}
}
if !present {
continue;
}
Self::check_features(endpoint, required, &spec_of, &mut found);
Self::check_elements(endpoint, required, &spec_of, &mut found);
}
}
fn check_elements(
endpoint: &Endpoint<'_>,
required: &RequiredCluster,
spec_of: &impl Fn(ClusterId) -> Option<&'static crate::dm::spec::Cluster>,
found: &mut impl FnMut(Defect),
) {
if required.attributes.is_empty()
&& required.commands.is_empty()
&& required.events.is_empty()
{
return;
}
let Some(served) = endpoint
.clusters
.iter()
.find(|cluster| cluster.id == required.id)
else {
return;
};
let Some(defined) = spec_of(required.id) else {
return;
};
let furnished = Furnished { endpoint };
let mut demand = |name: &'static str, conform: &Conform, present: Option<bool>| {
if conform.verdict(&furnished) != Conformance::Mandatory {
return;
}
if present == Some(false) {
found(Defect::MissingElement {
cluster: required.id,
name,
});
}
};
for (name, conform) in required.attributes {
let present = defined
.attributes
.iter()
.find(|a| a.name == *name)
.map(|a| served.attribute(a.id).is_some());
demand(name, conform, present);
}
for (name, conform) in required.commands {
let present = defined.commands.iter().find(|c| c.name == *name).map(|c| {
if c.to_server {
served.accepted_command(c.id).is_some()
} else {
served.generated_commands.contains(&c.id)
|| served
.accepted_commands
.iter()
.any(|a| a.response == Some(c.id))
}
});
demand(name, conform, present);
}
for (name, conform) in required.events {
let present = defined
.events
.iter()
.find(|e| e.name == *name)
.map(|e| served.events.iter().any(|served| served.id == e.id));
demand(name, conform, present);
}
}
fn check_features(
endpoint: &Endpoint<'_>,
required: &RequiredCluster,
spec_of: &impl Fn(ClusterId) -> Option<&'static crate::dm::spec::Cluster>,
found: &mut impl FnMut(Defect),
) {
if required.features.is_empty() {
return;
}
let Some(served) = endpoint
.clusters
.iter()
.find(|cluster| cluster.id == required.id)
else {
return;
};
let Some(defined) = spec_of(required.id) else {
return;
};
for (code, conform) in required.features {
if conform.verdict(&Furnished { endpoint }) != Conformance::Mandatory {
continue;
}
let Some(feature) = defined.feature(code) else {
continue;
};
if feature.bit < 32 && served.feature_map & (1u32 << feature.bit) == 0 {
found(Defect::MissingFeature {
cluster: required.id,
code,
});
}
}
}
#[must_use]
pub fn is_satisfied_by(
&self,
endpoint: &Endpoint<'_>,
spec_of: impl Fn(ClusterId) -> Option<&'static crate::dm::spec::Cluster>,
) -> bool {
let mut ok = true;
self.validate(endpoint, spec_of, |_| ok = false);
ok
}
}