#![allow(unused_imports)]
use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
use crate::dm::meta::{EventPriority, Reporting};
use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
use crate::error::Result;
use crate::im::{AttributeId, ClusterId, CommandId, EventId};
use crate::tlv::{
ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
};
pub const ID: ClusterId = 0x0104;
pub const REVISION: u16 = 1;
pub const PICS: &str = "CLCTRL";
pub mod feature {
pub const POSITIONING: u32 = 1 << 0;
pub const MOTION_LATCHING: u32 = 1 << 1;
pub const INSTANTANEOUS: u32 = 1 << 2;
pub const SPEED: u32 = 1 << 3;
pub const VENTILATION: u32 = 1 << 4;
pub const PEDESTRIAN: u32 = 1 << 5;
pub const CALIBRATION: u32 = 1 << 6;
pub const PROTECTION: u32 = 1 << 7;
pub const MANUALLY_OPERABLE: u32 = 1 << 8;
}
pub const FEATURES: &[Feature] = &[
Feature {
bit: 0,
code: "PS",
name: "Positioning",
conform: Conform::OPTIONAL,
},
Feature {
bit: 1,
code: "LT",
name: "MotionLatching",
conform: Conform::OPTIONAL,
},
Feature {
bit: 2,
code: "IS",
name: "Instantaneous",
conform: Conform::OPTIONAL,
},
Feature {
bit: 3,
code: "SP",
name: "Speed",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::All(&[
Condition::Feature(0),
Condition::Not(&Condition::Feature(2)),
]),
}],
},
},
Feature {
bit: 4,
code: "VT",
name: "Ventilation",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(0),
}],
},
},
Feature {
bit: 5,
code: "PD",
name: "Pedestrian",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(0),
}],
},
},
Feature {
bit: 6,
code: "CL",
name: "Calibration",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(0),
}],
},
},
Feature {
bit: 7,
code: "PT",
name: "Protection",
conform: Conform::OPTIONAL,
},
Feature {
bit: 8,
code: "MO",
name: "ManuallyOperable",
conform: Conform::OPTIONAL,
},
];
pub mod attribute {
use crate::im::AttributeId;
pub const COUNTDOWN_TIME: AttributeId = 0x00;
pub const MAIN_STATE: AttributeId = 0x01;
pub const CURRENT_ERROR_LIST: AttributeId = 0x02;
pub const OVERALL_CURRENT_STATE: AttributeId = 0x03;
pub const OVERALL_TARGET_STATE: AttributeId = 0x04;
pub const LATCH_CONTROL_MODES: AttributeId = 0x05;
}
pub mod command {
use crate::im::CommandId;
pub const STOP: CommandId = 0x00;
pub const MOVE_TO: CommandId = 0x01;
pub const CALIBRATE: CommandId = 0x02;
}
pub mod event {
use crate::im::EventId;
pub const OPERATIONAL_ERROR: EventId = 0x00;
pub const MOVEMENT_COMPLETED: EventId = 0x01;
pub const ENGAGE_STATE_CHANGED: EventId = 0x02;
pub const SECURE_STATE_CHANGED: EventId = 0x03;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum ClosureErrorEnum {
PhysicallyBlocked = 0,
BlockedBySensor = 1,
TemperatureLimited = 2,
MaintenanceRequired = 3,
InternalInterference = 4,
}
impl ClosureErrorEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::PhysicallyBlocked),
1 => Some(Self::BlockedBySensor),
2 => Some(Self::TemperatureLimited),
3 => Some(Self::MaintenanceRequired),
4 => Some(Self::InternalInterference),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for ClosureErrorEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for ClosureErrorEnum {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum CurrentPositionEnum {
FullyClosed = 0,
FullyOpened = 1,
PartiallyOpened = 2,
OpenedForPedestrian = 3,
OpenedForVentilation = 4,
OpenedAtSignature = 5,
}
impl CurrentPositionEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::FullyClosed),
1 => Some(Self::FullyOpened),
2 => Some(Self::PartiallyOpened),
3 => Some(Self::OpenedForPedestrian),
4 => Some(Self::OpenedForVentilation),
5 => Some(Self::OpenedAtSignature),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for CurrentPositionEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for CurrentPositionEnum {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum MainStateEnum {
Stopped = 0,
Moving = 1,
WaitingForMotion = 2,
Error = 3,
Calibrating = 4,
Protected = 5,
Disengaged = 6,
SetupRequired = 7,
}
impl MainStateEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Stopped),
1 => Some(Self::Moving),
2 => Some(Self::WaitingForMotion),
3 => Some(Self::Error),
4 => Some(Self::Calibrating),
5 => Some(Self::Protected),
6 => Some(Self::Disengaged),
7 => Some(Self::SetupRequired),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for MainStateEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for MainStateEnum {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum TargetPositionEnum {
MoveToFullyClosed = 0,
MoveToFullyOpen = 1,
MoveToPedestrianPosition = 2,
MoveToVentilationPosition = 3,
MoveToSignaturePosition = 4,
}
impl TargetPositionEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::MoveToFullyClosed),
1 => Some(Self::MoveToFullyOpen),
2 => Some(Self::MoveToPedestrianPosition),
3 => Some(Self::MoveToVentilationPosition),
4 => Some(Self::MoveToSignaturePosition),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for TargetPositionEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for TargetPositionEnum {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct LatchControlModesBitmap: u8 {
const REMOTE_LATCHING = 1 << 0;
const REMOTE_UNLATCHING = 1 << 1;
}
}
impl ToTlv for LatchControlModesBitmap {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.bits().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for LatchControlModesBitmap {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let bits = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_bits(bits).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OverallCurrentStateStruct {
pub position: Option<Nullable<CurrentPositionEnum>>,
pub latch: Option<Nullable<bool>>,
pub speed: Option<crate::clusters::generated::globals::ThreeLevelAutoEnum>,
pub secure_state: Nullable<bool>,
}
impl ToTlv for OverallCurrentStateStruct {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
if let Some(value) = &self.position {
value.to_tlv(w, Tag::Context(0))?;
}
if let Some(value) = &self.latch {
value.to_tlv(w, Tag::Context(1))?;
}
if let Some(value) = &self.speed {
value.to_tlv(w, Tag::Context(2))?;
}
self.secure_state.to_tlv(w, Tag::Context(3))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for OverallCurrentStateStruct {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut position = None;
let mut latch = None;
let mut speed = None;
let mut secure_state = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => {
position = Some(<Nullable<CurrentPositionEnum>>::from_tlv(reader, &field)?)
}
Some(1) => latch = Some(<Nullable<bool>>::from_tlv(reader, &field)?),
Some(2) => {
speed = Some(
<crate::clusters::generated::globals::ThreeLevelAutoEnum>::from_tlv(
reader, &field,
)?,
)
}
Some(3) => secure_state = Some(<Nullable<bool>>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
position,
latch,
speed,
secure_state: secure_state.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OverallTargetStateStruct {
pub position: Option<Nullable<TargetPositionEnum>>,
pub latch: Option<Nullable<bool>>,
pub speed: Option<crate::clusters::generated::globals::ThreeLevelAutoEnum>,
}
impl ToTlv for OverallTargetStateStruct {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
if let Some(value) = &self.position {
value.to_tlv(w, Tag::Context(0))?;
}
if let Some(value) = &self.latch {
value.to_tlv(w, Tag::Context(1))?;
}
if let Some(value) = &self.speed {
value.to_tlv(w, Tag::Context(2))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for OverallTargetStateStruct {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut position = None;
let mut latch = None;
let mut speed = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => {
position = Some(<Nullable<TargetPositionEnum>>::from_tlv(reader, &field)?)
}
Some(1) => latch = Some(<Nullable<bool>>::from_tlv(reader, &field)?),
Some(2) => {
speed = Some(
<crate::clusters::generated::globals::ThreeLevelAutoEnum>::from_tlv(
reader, &field,
)?,
)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
position,
latch,
speed,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MoveToFields {
pub position: Option<TargetPositionEnum>,
pub latch: Option<bool>,
pub speed: Option<crate::clusters::generated::globals::ThreeLevelAutoEnum>,
}
impl ToTlv for MoveToFields {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
if let Some(value) = &self.position {
value.to_tlv(w, Tag::Context(0))?;
}
if let Some(value) = &self.latch {
value.to_tlv(w, Tag::Context(1))?;
}
if let Some(value) = &self.speed {
value.to_tlv(w, Tag::Context(2))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for MoveToFields {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut position = None;
let mut latch = None;
let mut speed = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => position = Some(<TargetPositionEnum>::from_tlv(reader, &field)?),
Some(1) => latch = Some(<bool>::from_tlv(reader, &field)?),
Some(2) => {
speed = Some(
<crate::clusters::generated::globals::ThreeLevelAutoEnum>::from_tlv(
reader, &field,
)?,
)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
position,
latch,
speed,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "CountdownTime",
kind: "elapsed-s",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::Quieter,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::All(&[
Condition::Feature(0),
Condition::Not(&Condition::Feature(2)),
]),
}],
},
},
Attribute {
id: 0x0001,
name: "MainState",
kind: "MainStateEnum",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0002,
name: "CurrentErrorList",
kind: "list[ClosureErrorEnum]",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0003,
name: "OverallCurrentState",
kind: "OverallCurrentStateStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0004,
name: "OverallTargetState",
kind: "OverallTargetStateStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0005,
name: "LatchControlModes",
kind: "LatchControlModesBitmap",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::FIXED,
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(1),
}],
},
},
];
pub const COMMANDS: &[Command] = &[
Command {
id: 0x00,
name: "Stop",
to_server: true,
response: None,
access: Access::invoke(Privilege::Operate),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Not(&Condition::Feature(2)),
}],
},
},
Command {
id: 0x01,
name: "MoveTo",
to_server: true,
response: None,
access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::TIMED),
conform: Conform::MANDATORY,
},
Command {
id: 0x02,
name: "Calibrate",
to_server: true,
response: None,
access: Access::invoke(Privilege::Manage).with_qualities(AccessQualities::TIMED),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(6),
}],
},
},
];
pub const EVENTS: &[Event] = &[
Event {
id: 0x00,
name: "OperationalError",
priority: EventPriority::Critical,
access: Access::read_only(Privilege::View),
conform: Conform::MANDATORY,
},
Event {
id: 0x01,
name: "MovementCompleted",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Not(&Condition::Feature(2)),
}],
},
},
Event {
id: 0x02,
name: "EngageStateChanged",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(8),
}],
},
},
Event {
id: 0x03,
name: "SecureStateChanged",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform::MANDATORY,
},
];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Closure Control",
revision: REVISION,
pics: "CLCTRL",
derived_from: None,
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};