use crate::dm::access::Access;
use crate::im::{AttributeId, ClusterId, CommandId, EventId};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Reporting {
#[default]
OnChange,
Quieter,
ChangesOmitted,
}
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct AttributeQualities: u8 {
const NULLABLE = 1 << 0;
const NON_VOLATILE = 1 << 1;
const FIXED = 1 << 2;
const ATOMIC = 1 << 3;
const LARGE = 1 << 4;
const SCENE = 1 << 5;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AttributeDescriptor {
pub id: AttributeId,
pub access: Access,
pub reporting: Reporting,
pub qualities: AttributeQualities,
}
impl AttributeDescriptor {
#[must_use]
pub const fn read_only(id: AttributeId) -> Self {
Self {
id,
access: Access::read_only(crate::dm::access::Privilege::View),
reporting: Reporting::OnChange,
qualities: AttributeQualities::empty(),
}
}
#[must_use]
pub const fn read_write(id: AttributeId) -> Self {
Self {
id,
access: Access::read_write(),
reporting: Reporting::OnChange,
qualities: AttributeQualities::empty(),
}
}
#[must_use]
pub const fn with_access(mut self, access: Access) -> Self {
self.access = access;
self
}
#[must_use]
pub const fn with_qualities(mut self, qualities: AttributeQualities) -> Self {
self.qualities = qualities;
self
}
#[must_use]
pub const fn with_reporting(mut self, reporting: Reporting) -> Self {
self.reporting = reporting;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommandDescriptor {
pub id: CommandId,
pub access: Access,
pub response: Option<CommandId>,
}
impl CommandDescriptor {
#[must_use]
pub const fn new(id: CommandId) -> Self {
Self {
id,
access: Access::invoke(crate::dm::access::Privilege::Operate),
response: None,
}
}
#[must_use]
pub const fn with_response(mut self, response: CommandId) -> Self {
self.response = Some(response);
self
}
#[must_use]
pub const fn with_access(mut self, access: Access) -> Self {
self.access = access;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[non_exhaustive]
pub enum EventPriority {
Debug,
#[default]
Info,
Critical,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EventDescriptor {
pub id: EventId,
pub access: Access,
pub priority: EventPriority,
}
impl EventDescriptor {
#[must_use]
pub const fn new(id: EventId) -> Self {
Self {
id,
access: Access::read_only(crate::dm::access::Privilege::View),
priority: EventPriority::Info,
}
}
#[must_use]
pub const fn with_priority(mut self, priority: EventPriority) -> Self {
self.priority = priority;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ClusterDescriptor<'a> {
pub id: ClusterId,
pub revision: u16,
pub feature_map: u32,
pub attributes: &'a [AttributeDescriptor],
pub accepted_commands: &'a [CommandDescriptor],
pub generated_commands: &'a [CommandId],
pub events: &'a [EventDescriptor],
}
impl<'a> ClusterDescriptor<'a> {
#[must_use]
pub fn attribute(&self, id: AttributeId) -> Option<AttributeDescriptor> {
if let Some(global) = crate::dm::global::descriptor(id) {
return Some(global);
}
self.attributes
.binary_search_by_key(&id, |a| a.id)
.ok()
.and_then(|index| self.attributes.get(index).copied())
}
#[must_use]
pub fn accepted_command(&self, id: CommandId) -> Option<CommandDescriptor> {
self.accepted_commands
.binary_search_by_key(&id, |c| c.id)
.ok()
.and_then(|index| self.accepted_commands.get(index).copied())
}
#[must_use]
pub fn event(&self, id: EventId) -> Option<EventDescriptor> {
self.events
.binary_search_by_key(&id, |e| e.id)
.ok()
.and_then(|index| self.events.get(index).copied())
}
pub fn attribute_ids(&self) -> impl Iterator<Item = AttributeId> + 'a {
let own = self.attributes.iter().map(|a| a.id);
own.chain(crate::dm::global::ATTRIBUTE_IDS.iter().copied())
}
pub fn generated_command_ids(&self) -> impl Iterator<Item = CommandId> + 'a {
let responses = self.accepted_commands.iter().filter_map(|c| c.response);
responses.chain(self.generated_commands.iter().copied())
}
#[must_use]
pub fn is_well_formed(&self) -> bool {
fn ascending<T: Copy, K: Ord>(items: &[T], key: impl Fn(&T) -> K) -> bool {
items.windows(2).all(|w| match w {
[a, b] => key(a) < key(b),
_ => true,
})
}
ascending(self.attributes, |a| a.id)
&& ascending(self.accepted_commands, |c| c.id)
&& ascending(self.events, |e| e.id)
&& !self
.attributes
.iter()
.any(|a| crate::dm::global::is_global(a.id))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeviceType {
pub device_type: u32,
pub revision: u16,
}
impl DeviceType {
#[must_use]
pub const fn new(device_type: u32, revision: u16) -> Self {
Self {
device_type,
revision,
}
}
}