#![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 = 0x0050;
pub const REVISION: u16 = 2;
pub const PICS: &str = "MOD";
pub mod feature {
pub const ON_OFF: u32 = 1 << 0;
}
pub const FEATURES: &[Feature] = &[Feature {
bit: 0,
code: "DEPONOFF",
name: "OnOff",
conform: Conform::OPTIONAL,
}];
pub mod attribute {
use crate::im::AttributeId;
pub const DESCRIPTION: AttributeId = 0x00;
pub const STANDARD_NAMESPACE: AttributeId = 0x01;
pub const SUPPORTED_MODES: AttributeId = 0x02;
pub const CURRENT_MODE: AttributeId = 0x03;
pub const START_UP_MODE: AttributeId = 0x04;
pub const ON_MODE: AttributeId = 0x05;
}
pub mod command {
use crate::im::CommandId;
pub const CHANGE_TO_MODE: CommandId = 0x00;
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ModeOptionStruct<'a> {
pub label: &'a str,
pub mode: u8,
pub semantic_tags: TlvList<'a, SemanticTagStruct>,
}
impl<'a> ToTlv for ModeOptionStruct<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.label.to_tlv(w, Tag::Context(0))?;
self.mode.to_tlv(w, Tag::Context(1))?;
self.semantic_tags.to_tlv(w, Tag::Context(2))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for ModeOptionStruct<'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 label = None;
let mut mode = None;
let mut semantic_tags = 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) => label = Some(<&'a str>::from_tlv(reader, &field)?),
Some(1) => mode = Some(<u8>::from_tlv(reader, &field)?),
Some(2) => {
semantic_tags =
Some(<TlvList<'a, SemanticTagStruct>>::from_tlv(reader, &field)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
label: label.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
mode: mode.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
semantic_tags: semantic_tags.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SemanticTagStruct {
pub mfg_code: crate::msg::VendorId,
pub value: u16,
}
impl ToTlv for SemanticTagStruct {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.mfg_code.to_tlv(w, Tag::Context(0))?;
self.value.to_tlv(w, Tag::Context(1))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for SemanticTagStruct {
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 mfg_code = None;
let mut value = 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) => mfg_code = Some(<crate::msg::VendorId>::from_tlv(reader, &field)?),
Some(1) => value = Some(<u16>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
mfg_code: mfg_code.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
value: value.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ChangeToModeFields {
pub new_mode: u8,
}
impl ToTlv for ChangeToModeFields {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.new_mode.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for ChangeToModeFields {
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 new_mode = 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) => new_mode = Some(<u8>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
new_mode: new_mode.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "Description",
kind: "string",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::FIXED,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0001,
name: "StandardNamespace",
kind: "enum16",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE.union(AttributeQualities::FIXED),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0002,
name: "SupportedModes",
kind: "list[ModeOptionStruct]",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::FIXED,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0003,
name: "CurrentMode",
kind: "uint8",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NON_VOLATILE,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0004,
name: "StartUpMode",
kind: "uint8",
access: Access::read_write_with(Privilege::View, Privilege::Operate),
qualities: AttributeQualities::NULLABLE.union(AttributeQualities::NON_VOLATILE),
reporting: Reporting::OnChange,
conform: Conform::OPTIONAL,
},
Attribute {
id: 0x0005,
name: "OnMode",
kind: "uint8",
access: Access::read_write_with(Privilege::View, Privilege::Operate),
qualities: AttributeQualities::NULLABLE.union(AttributeQualities::NON_VOLATILE),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(0),
}],
},
},
];
pub const COMMANDS: &[Command] = &[Command {
id: 0x00,
name: "ChangeToMode",
to_server: true,
response: None,
access: Access::invoke(Privilege::Operate),
conform: Conform::MANDATORY,
}];
pub const EVENTS: &[Event] = &[];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Mode Select",
revision: REVISION,
pics: "MOD",
derived_from: None,
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};