#![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 = 0x0060;
pub const REVISION: u16 = 3;
pub const PICS: &str = "OPSTATE";
pub const FEATURES: &[Feature] = &[];
pub mod attribute {
use crate::im::AttributeId;
pub const PHASE_LIST: AttributeId = 0x00;
pub const CURRENT_PHASE: AttributeId = 0x01;
pub const COUNTDOWN_TIME: AttributeId = 0x02;
pub const OPERATIONAL_STATE_LIST: AttributeId = 0x03;
pub const OPERATIONAL_STATE: AttributeId = 0x04;
pub const OPERATIONAL_ERROR: AttributeId = 0x05;
}
pub mod command {
use crate::im::CommandId;
pub const PAUSE: CommandId = 0x00;
pub const STOP: CommandId = 0x01;
pub const START: CommandId = 0x02;
pub const RESUME: CommandId = 0x03;
pub const OPERATIONAL_COMMAND_RESPONSE: CommandId = 0x04;
}
pub mod event {
use crate::im::EventId;
pub const OPERATIONAL_ERROR: EventId = 0x00;
pub const OPERATION_COMPLETION: EventId = 0x01;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum ErrorStateEnum {
NoError = 0,
UnableToStartOrResume = 1,
UnableToCompleteOperation = 2,
CommandInvalidInState = 3,
}
impl ErrorStateEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::NoError),
1 => Some(Self::UnableToStartOrResume),
2 => Some(Self::UnableToCompleteOperation),
3 => Some(Self::CommandInvalidInState),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for ErrorStateEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for ErrorStateEnum {
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 OperationalStateEnum {
Stopped = 0,
Running = 1,
Paused = 2,
Error = 3,
}
impl OperationalStateEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Stopped),
1 => Some(Self::Running),
2 => Some(Self::Paused),
3 => Some(Self::Error),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for OperationalStateEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for OperationalStateEnum {
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)]
pub struct ErrorStateStruct<'a> {
pub error_state_id: ErrorStateEnum,
pub error_state_label: Option<&'a str>,
pub error_state_details: Option<&'a str>,
}
impl<'a> ToTlv for ErrorStateStruct<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.error_state_id.to_tlv(w, Tag::Context(0))?;
if let Some(value) = &self.error_state_label {
value.to_tlv(w, Tag::Context(1))?;
}
if let Some(value) = &self.error_state_details {
value.to_tlv(w, Tag::Context(2))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for ErrorStateStruct<'a> {
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 error_state_id = None;
let mut error_state_label = None;
let mut error_state_details = 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) => error_state_id = Some(<ErrorStateEnum>::from_tlv(reader, &field)?),
Some(1) => error_state_label = Some(<&'a str>::from_tlv(reader, &field)?),
Some(2) => error_state_details = Some(<&'a str>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
error_state_id: error_state_id
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
error_state_label,
error_state_details,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OperationalStateStruct<'a> {
pub operational_state_id: OperationalStateEnum,
pub operational_state_label: Option<&'a str>,
}
impl<'a> ToTlv for OperationalStateStruct<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.operational_state_id.to_tlv(w, Tag::Context(0))?;
if let Some(value) = &self.operational_state_label {
value.to_tlv(w, Tag::Context(1))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for OperationalStateStruct<'a> {
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 operational_state_id = None;
let mut operational_state_label = 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) => {
operational_state_id = Some(<OperationalStateEnum>::from_tlv(reader, &field)?)
}
Some(1) => operational_state_label = Some(<&'a str>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
operational_state_id: operational_state_id
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
operational_state_label,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OperationalCommandResponseFields<'a> {
pub command_response_state: ErrorStateStruct<'a>,
}
impl<'a> ToTlv for OperationalCommandResponseFields<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.command_response_state.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for OperationalCommandResponseFields<'a> {
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 command_response_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) => {
command_response_state = Some(<ErrorStateStruct<'a>>::from_tlv(reader, &field)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
command_response_state: command_response_state
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "PhaseList",
kind: "list[string]",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0001,
name: "CurrentPhase",
kind: "uint8",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0002,
name: "CountdownTime",
kind: "elapsed-s",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::Quieter,
conform: Conform::OPTIONAL,
},
Attribute {
id: 0x0003,
name: "OperationalStateList",
kind: "list[OperationalStateStruct]",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0004,
name: "OperationalState",
kind: "OperationalStateEnum",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0005,
name: "OperationalError",
kind: "ErrorStateStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
];
pub const COMMANDS: &[Command] = &[
Command {
id: 0x00,
name: "Pause",
to_server: true,
response: Some(0x04),
access: Access::invoke(Privilege::Operate),
conform: Conform {
clauses: &[
Clause {
verdict: Conformance::Mandatory,
when: Condition::Command(0x03),
},
Clause {
verdict: Conformance::Optional,
when: Condition::Always,
},
],
},
},
Command {
id: 0x01,
name: "Stop",
to_server: true,
response: Some(0x04),
access: Access::invoke(Privilege::Operate),
conform: Conform {
clauses: &[
Clause {
verdict: Conformance::Mandatory,
when: Condition::Command(0x02),
},
Clause {
verdict: Conformance::Optional,
when: Condition::Always,
},
],
},
},
Command {
id: 0x02,
name: "Start",
to_server: true,
response: Some(0x04),
access: Access::invoke(Privilege::Operate),
conform: Conform::OPTIONAL,
},
Command {
id: 0x03,
name: "Resume",
to_server: true,
response: Some(0x04),
access: Access::invoke(Privilege::Operate),
conform: Conform {
clauses: &[
Clause {
verdict: Conformance::Mandatory,
when: Condition::Command(0x00),
},
Clause {
verdict: Conformance::Optional,
when: Condition::Always,
},
],
},
},
Command {
id: 0x04,
name: "OperationalCommandResponse",
to_server: false,
response: None,
access: Access::invoke(Privilege::Operate),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Any(&[
Condition::Command(0x00),
Condition::Command(0x01),
Condition::Command(0x02),
Condition::Command(0x03),
]),
}],
},
},
];
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: "OperationCompletion",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform::OPTIONAL,
},
];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Operational State",
revision: REVISION,
pics: "OPSTATE",
derived_from: None,
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};