#![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 = 0x0064;
pub const REVISION: u16 = 1;
pub const PICS: &str = "TEMPALM";
pub mod feature {
pub const OVER_TEMPERATURE: u32 = 1 << 20;
pub const UNDER_TEMPERATURE: u32 = 1 << 21;
pub const MAJOR_THRESHOLD: u32 = 1 << 22;
pub const MINOR_THRESHOLD: u32 = 1 << 23;
pub const OVER_CRITICAL_ADJUSTABLE: u32 = 1 << 24;
pub const OVER_MAJOR_ADJUSTABLE: u32 = 1 << 25;
pub const OVER_MINOR_ADJUSTABLE: u32 = 1 << 26;
pub const UNDER_MINOR_ADJUSTABLE: u32 = 1 << 27;
pub const UNDER_MAJOR_ADJUSTABLE: u32 = 1 << 28;
pub const UNDER_CRITICAL_ADJUSTABLE: u32 = 1 << 29;
}
pub const FEATURES: &[Feature] = &[
Feature {
bit: 20,
code: "OVER",
name: "OverTemperature",
conform: Conform::OPTIONAL,
},
Feature {
bit: 21,
code: "UNDER",
name: "UnderTemperature",
conform: Conform::OPTIONAL,
},
Feature {
bit: 22,
code: "MAJOR",
name: "MajorThreshold",
conform: Conform::OPTIONAL,
},
Feature {
bit: 23,
code: "MINOR",
name: "MinorThreshold",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(22),
}],
},
},
Feature {
bit: 24,
code: "OCRIADJ",
name: "OverCriticalAdjustable",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(20),
}],
},
},
Feature {
bit: 25,
code: "OMAJADJ",
name: "OverMajorAdjustable",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::All(&[Condition::Feature(20), Condition::Feature(22)]),
}],
},
},
Feature {
bit: 26,
code: "OMINADJ",
name: "OverMinorAdjustable",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::All(&[Condition::Feature(20), Condition::Feature(23)]),
}],
},
},
Feature {
bit: 27,
code: "UMINADJ",
name: "UnderMinorAdjustable",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::All(&[Condition::Feature(21), Condition::Feature(23)]),
}],
},
},
Feature {
bit: 28,
code: "UMAJADJ",
name: "UnderMajorAdjustable",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::All(&[Condition::Feature(21), Condition::Feature(22)]),
}],
},
},
Feature {
bit: 29,
code: "UCRIADJ",
name: "UnderCriticalAdjustable",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(21),
}],
},
},
];
pub mod attribute {
use crate::im::AttributeId;
pub const MASK: AttributeId = 0x00;
pub const LATCH: AttributeId = 0x01;
pub const STATE: AttributeId = 0x02;
pub const SUPPORTED: AttributeId = 0x03;
pub const CRITICAL_OVER_TEMPERATURE_THRESHOLD: AttributeId = 0x80;
pub const MAJOR_OVER_TEMPERATURE_THRESHOLD: AttributeId = 0x81;
pub const MINOR_OVER_TEMPERATURE_THRESHOLD: AttributeId = 0x82;
pub const MINOR_UNDER_TEMPERATURE_THRESHOLD: AttributeId = 0x83;
pub const MAJOR_UNDER_TEMPERATURE_THRESHOLD: AttributeId = 0x84;
pub const CRITICAL_UNDER_TEMPERATURE_THRESHOLD: AttributeId = 0x85;
}
pub mod command {
use crate::im::CommandId;
pub const RESET: CommandId = 0x00;
pub const MODIFY_ENABLED_ALARMS: CommandId = 0x01;
pub const SET_TEMPERATURE_ALARM_THRESHOLDS: CommandId = 0x80;
}
pub mod event {
use crate::im::EventId;
pub const NOTIFY: EventId = 0x00;
}
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct AlarmBitmap: u8 {
const CRITICAL_OVER_TEMPERATURE_ALARM = 1 << 0;
const MAJOR_OVER_TEMPERATURE_ALARM = 1 << 1;
const MINOR_OVER_TEMPERATURE_ALARM = 1 << 2;
const MINOR_UNDER_TEMPERATURE_ALARM = 1 << 3;
const MAJOR_UNDER_TEMPERATURE_ALARM = 1 << 4;
const CRITICAL_UNDER_TEMPERATURE_ALARM = 1 << 5;
}
}
impl ToTlv for AlarmBitmap {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.bits().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for AlarmBitmap {
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 ResetFields {
pub alarms: AlarmBitmap,
}
impl ToTlv for ResetFields {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.alarms.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for ResetFields {
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 alarms = 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) => alarms = Some(<AlarmBitmap>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
alarms: alarms.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ModifyEnabledAlarmsFields {
pub mask: AlarmBitmap,
}
impl ToTlv for ModifyEnabledAlarmsFields {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.mask.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for ModifyEnabledAlarmsFields {
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 mask = 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) => mask = Some(<AlarmBitmap>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
mask: mask.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SetTemperatureAlarmThresholdsFields {
pub critical_over_temperature_threshold: Option<i16>,
pub major_over_temperature_threshold: Option<i16>,
pub minor_over_temperature_threshold: Option<i16>,
pub minor_under_temperature_threshold: Option<i16>,
pub major_under_temperature_threshold: Option<i16>,
pub critical_under_temperature_threshold: Option<i16>,
}
impl ToTlv for SetTemperatureAlarmThresholdsFields {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
if let Some(value) = &self.critical_over_temperature_threshold {
value.to_tlv(w, Tag::Context(0))?;
}
if let Some(value) = &self.major_over_temperature_threshold {
value.to_tlv(w, Tag::Context(1))?;
}
if let Some(value) = &self.minor_over_temperature_threshold {
value.to_tlv(w, Tag::Context(2))?;
}
if let Some(value) = &self.minor_under_temperature_threshold {
value.to_tlv(w, Tag::Context(3))?;
}
if let Some(value) = &self.major_under_temperature_threshold {
value.to_tlv(w, Tag::Context(4))?;
}
if let Some(value) = &self.critical_under_temperature_threshold {
value.to_tlv(w, Tag::Context(5))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for SetTemperatureAlarmThresholdsFields {
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 critical_over_temperature_threshold = None;
let mut major_over_temperature_threshold = None;
let mut minor_over_temperature_threshold = None;
let mut minor_under_temperature_threshold = None;
let mut major_under_temperature_threshold = None;
let mut critical_under_temperature_threshold = 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) => {
critical_over_temperature_threshold = Some(<i16>::from_tlv(reader, &field)?)
}
Some(1) => {
major_over_temperature_threshold = Some(<i16>::from_tlv(reader, &field)?)
}
Some(2) => {
minor_over_temperature_threshold = Some(<i16>::from_tlv(reader, &field)?)
}
Some(3) => {
minor_under_temperature_threshold = Some(<i16>::from_tlv(reader, &field)?)
}
Some(4) => {
major_under_temperature_threshold = Some(<i16>::from_tlv(reader, &field)?)
}
Some(5) => {
critical_under_temperature_threshold = Some(<i16>::from_tlv(reader, &field)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
critical_over_temperature_threshold,
major_over_temperature_threshold,
minor_over_temperature_threshold,
minor_under_temperature_threshold,
major_under_temperature_threshold,
critical_under_temperature_threshold,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "Mask",
kind: "AlarmBitmap",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0001,
name: "Latch",
kind: "AlarmBitmap",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::FIXED,
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Named("RESET"),
}],
},
},
Attribute {
id: 0x0002,
name: "State",
kind: "AlarmBitmap",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0003,
name: "Supported",
kind: "AlarmBitmap",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::FIXED,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0080,
name: "CriticalOverTemperatureThreshold",
kind: "temperature",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(20),
}],
},
},
Attribute {
id: 0x0081,
name: "MajorOverTemperatureThreshold",
kind: "temperature",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::All(&[Condition::Feature(20), Condition::Feature(22)]),
}],
},
},
Attribute {
id: 0x0082,
name: "MinorOverTemperatureThreshold",
kind: "temperature",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::All(&[Condition::Feature(20), Condition::Feature(23)]),
}],
},
},
Attribute {
id: 0x0083,
name: "MinorUnderTemperatureThreshold",
kind: "temperature",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::All(&[Condition::Feature(21), Condition::Feature(23)]),
}],
},
},
Attribute {
id: 0x0084,
name: "MajorUnderTemperatureThreshold",
kind: "temperature",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::All(&[Condition::Feature(21), Condition::Feature(22)]),
}],
},
},
Attribute {
id: 0x0085,
name: "CriticalUnderTemperatureThreshold",
kind: "temperature",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(21),
}],
},
},
];
pub const COMMANDS: &[Command] = &[
Command {
id: 0x00,
name: "Reset",
to_server: true,
response: None,
access: Access::invoke(Privilege::Operate),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Named("RESET"),
}],
},
},
Command {
id: 0x01,
name: "ModifyEnabledAlarms",
to_server: true,
response: None,
access: Access::invoke(Privilege::Operate),
conform: Conform::OPTIONAL,
},
Command {
id: 0x80,
name: "SetTemperatureAlarmThresholds",
to_server: true,
response: None,
access: Access::invoke(Privilege::Operate),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Any(&[
Condition::Feature(24),
Condition::Feature(25),
Condition::Feature(26),
Condition::Feature(27),
Condition::Feature(28),
Condition::Feature(29),
]),
}],
},
},
];
pub const EVENTS: &[Event] = &[Event {
id: 0x00,
name: "Notify",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform::MANDATORY,
}];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Temperature Alarm",
revision: REVISION,
pics: "TEMPALM",
derived_from: Some("Alarm Base"),
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};