#![allow(
clippy::all,
clippy::pedantic,
dead_code,
unreachable_pub,
unused_imports
)]
use crate::datatypes::SemanticTagStruct;
use crate::error::ClusterError;
use crate::types::Nullable;
use matter_codec::{ContainerKind, Element, Tag, TlvReader, TlvWriter, Value};
pub const CLUSTER_ID: u32 = 0x042D;
pub const CLUSTER_REVISION: u16 = 4;
pub mod command_id {}
pub mod attribute_id {
pub const MEASURED_VALUE: u32 = 0x0000;
pub const MIN_MEASURED_VALUE: u32 = 0x0001;
pub const MAX_MEASURED_VALUE: u32 = 0x0002;
pub const PEAK_MEASURED_VALUE: u32 = 0x0003;
pub const PEAK_MEASURED_VALUE_WINDOW: u32 = 0x0004;
pub const AVERAGE_MEASURED_VALUE: u32 = 0x0005;
pub const AVERAGE_MEASURED_VALUE_WINDOW: u32 = 0x0006;
pub const UNCERTAINTY: u32 = 0x0007;
pub const MEASUREMENT_UNIT: u32 = 0x0008;
pub const MEASUREMENT_MEDIUM: u32 = 0x0009;
pub const LEVEL_VALUE: u32 = 0x000A;
}
bitflags::bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Feature: u32 {
const MEA = 1 << 0;
const LEV = 1 << 1;
const MED = 1 << 2;
const CRI = 1 << 3;
const PEA = 1 << 4;
const AVG = 1 << 5;
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LevelValueEnum {
Unknown,
Low,
Medium,
High,
Critical,
Unrecognized(u8),
}
impl LevelValueEnum {
#[must_use]
pub fn from_raw(v: u8) -> Self {
match v {
0 => Self::Unknown,
1 => Self::Low,
2 => Self::Medium,
3 => Self::High,
4 => Self::Critical,
other => Self::Unrecognized(other),
}
}
#[must_use]
pub fn to_raw(self) -> u8 {
match self {
Self::Unknown => 0,
Self::Low => 1,
Self::Medium => 2,
Self::High => 3,
Self::Critical => 4,
Self::Unrecognized(v) => v,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MeasurementMediumEnum {
Air,
Water,
Soil,
Unknown(u8),
}
impl MeasurementMediumEnum {
#[must_use]
pub fn from_raw(v: u8) -> Self {
match v {
0 => Self::Air,
1 => Self::Water,
2 => Self::Soil,
other => Self::Unknown(other),
}
}
#[must_use]
pub fn to_raw(self) -> u8 {
match self {
Self::Air => 0,
Self::Water => 1,
Self::Soil => 2,
Self::Unknown(v) => v,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MeasurementUnitEnum {
Ppm,
Ppb,
Ppt,
Mgm3,
Ugm3,
Ngm3,
Pm3,
Bqm3,
Unknown(u8),
}
impl MeasurementUnitEnum {
#[must_use]
pub fn from_raw(v: u8) -> Self {
match v {
0 => Self::Ppm,
1 => Self::Ppb,
2 => Self::Ppt,
3 => Self::Mgm3,
4 => Self::Ugm3,
5 => Self::Ngm3,
6 => Self::Pm3,
7 => Self::Bqm3,
other => Self::Unknown(other),
}
}
#[must_use]
pub fn to_raw(self) -> u8 {
match self {
Self::Ppm => 0,
Self::Ppb => 1,
Self::Ppt => 2,
Self::Mgm3 => 3,
Self::Ugm3 => 4,
Self::Ngm3 => 5,
Self::Pm3 => 6,
Self::Bqm3 => 7,
Self::Unknown(v) => v,
}
}
}
pub fn decode_measured_value(tlv: &[u8]) -> Result<Nullable<f32>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Float(v),
..
}) => Ok(Nullable::Value(v)),
_ => Err(ClusterError::UnexpectedType {
context: "MeasuredValue",
}),
}
}
pub fn decode_min_measured_value(tlv: &[u8]) -> Result<Nullable<f32>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Float(v),
..
}) => Ok(Nullable::Value(v)),
_ => Err(ClusterError::UnexpectedType {
context: "MinMeasuredValue",
}),
}
}
pub fn decode_max_measured_value(tlv: &[u8]) -> Result<Nullable<f32>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Float(v),
..
}) => Ok(Nullable::Value(v)),
_ => Err(ClusterError::UnexpectedType {
context: "MaxMeasuredValue",
}),
}
}
pub fn decode_peak_measured_value(tlv: &[u8]) -> Result<Nullable<f32>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Float(v),
..
}) => Ok(Nullable::Value(v)),
_ => Err(ClusterError::UnexpectedType {
context: "PeakMeasuredValue",
}),
}
}
pub fn decode_peak_measured_value_window(tlv: &[u8]) -> Result<u32, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => {
Ok(u32::try_from(v)
.map_err(|_| ClusterError::InvalidLength("PeakMeasuredValueWindow"))?)
}
_ => Err(ClusterError::UnexpectedType {
context: "PeakMeasuredValueWindow",
}),
}
}
pub fn decode_average_measured_value(tlv: &[u8]) -> Result<Nullable<f32>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Float(v),
..
}) => Ok(Nullable::Value(v)),
_ => Err(ClusterError::UnexpectedType {
context: "AverageMeasuredValue",
}),
}
}
pub fn decode_average_measured_value_window(tlv: &[u8]) -> Result<u32, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(u32::try_from(v)
.map_err(|_| ClusterError::InvalidLength("AverageMeasuredValueWindow"))?),
_ => Err(ClusterError::UnexpectedType {
context: "AverageMeasuredValueWindow",
}),
}
}
pub fn decode_uncertainty(tlv: &[u8]) -> Result<f32, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Float(v),
..
}) => Ok(v),
_ => Err(ClusterError::UnexpectedType {
context: "Uncertainty",
}),
}
}
pub fn decode_measurement_unit(tlv: &[u8]) -> Result<MeasurementUnitEnum, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(MeasurementUnitEnum::from_raw(
u8::try_from(v).map_err(|_| ClusterError::InvalidLength("MeasurementUnit"))?,
)),
_ => Err(ClusterError::UnexpectedType {
context: "MeasurementUnit",
}),
}
}
pub fn decode_measurement_medium(tlv: &[u8]) -> Result<MeasurementMediumEnum, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(MeasurementMediumEnum::from_raw(
u8::try_from(v).map_err(|_| ClusterError::InvalidLength("MeasurementMedium"))?,
)),
_ => Err(ClusterError::UnexpectedType {
context: "MeasurementMedium",
}),
}
}
pub fn decode_level_value(tlv: &[u8]) -> Result<LevelValueEnum, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(LevelValueEnum::from_raw(
u8::try_from(v).map_err(|_| ClusterError::InvalidLength("LevelValue"))?,
)),
_ => Err(ClusterError::UnexpectedType {
context: "LevelValue",
}),
}
}