#![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 = 0x0008;
pub const CLUSTER_REVISION: u16 = 7;
pub mod command_id {
pub const MOVE_TO_LEVEL: u32 = 0x00;
pub const MOVE: u32 = 0x01;
pub const STEP: u32 = 0x02;
pub const STOP: u32 = 0x03;
pub const MOVE_TO_LEVEL_WITH_ON_OFF: u32 = 0x04;
pub const MOVE_WITH_ON_OFF: u32 = 0x05;
pub const STEP_WITH_ON_OFF: u32 = 0x06;
pub const STOP_WITH_ON_OFF: u32 = 0x07;
pub const MOVE_TO_CLOSEST_FREQUENCY: u32 = 0x08;
}
pub mod attribute_id {
pub const CURRENT_LEVEL: u32 = 0x0000;
pub const REMAINING_TIME: u32 = 0x0001;
pub const MIN_LEVEL: u32 = 0x0002;
pub const MAX_LEVEL: u32 = 0x0003;
pub const CURRENT_FREQUENCY: u32 = 0x0004;
pub const MIN_FREQUENCY: u32 = 0x0005;
pub const MAX_FREQUENCY: u32 = 0x0006;
pub const OPTIONS: u32 = 0x000F;
pub const ON_OFF_TRANSITION_TIME: u32 = 0x0010;
pub const ON_LEVEL: u32 = 0x0011;
pub const ON_TRANSITION_TIME: u32 = 0x0012;
pub const OFF_TRANSITION_TIME: u32 = 0x0013;
pub const DEFAULT_MOVE_RATE: u32 = 0x0014;
pub const START_UP_CURRENT_LEVEL: u32 = 0x4000;
}
bitflags::bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Feature: u32 {
const OO = 1 << 0;
const LT = 1 << 1;
const FQ = 1 << 2;
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MoveModeEnum {
Up,
Down,
Unknown(u8),
}
impl MoveModeEnum {
#[must_use]
pub fn from_raw(v: u8) -> Self {
match v {
0 => Self::Up,
1 => Self::Down,
other => Self::Unknown(other),
}
}
#[must_use]
pub fn to_raw(self) -> u8 {
match self {
Self::Up => 0,
Self::Down => 1,
Self::Unknown(v) => v,
}
}
}
bitflags::bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct OptionsBitmap: u8 {
const EXECUTE_IF_OFF = 1 << 0;
const COUPLE_COLOR_TEMP_TO_LEVEL = 1 << 1;
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum StepModeEnum {
Up,
Down,
Unknown(u8),
}
impl StepModeEnum {
#[must_use]
pub fn from_raw(v: u8) -> Self {
match v {
0 => Self::Up,
1 => Self::Down,
other => Self::Unknown(other),
}
}
#[must_use]
pub fn to_raw(self) -> u8 {
match self {
Self::Up => 0,
Self::Down => 1,
Self::Unknown(v) => v,
}
}
}
pub fn decode_current_level(tlv: &[u8]) -> Result<Nullable<u8>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => {
Ok(Nullable::Value(u8::try_from(v).map_err(|_| {
ClusterError::InvalidLength("CurrentLevel")
})?))
}
_ => Err(ClusterError::UnexpectedType {
context: "CurrentLevel",
}),
}
}
pub fn decode_remaining_time(tlv: &[u8]) -> Result<u16, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("RemainingTime"))?),
_ => Err(ClusterError::UnexpectedType {
context: "RemainingTime",
}),
}
}
pub fn decode_min_level(tlv: &[u8]) -> Result<u8, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(u8::try_from(v).map_err(|_| ClusterError::InvalidLength("MinLevel"))?),
_ => Err(ClusterError::UnexpectedType {
context: "MinLevel",
}),
}
}
pub fn decode_max_level(tlv: &[u8]) -> Result<u8, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(u8::try_from(v).map_err(|_| ClusterError::InvalidLength("MaxLevel"))?),
_ => Err(ClusterError::UnexpectedType {
context: "MaxLevel",
}),
}
}
pub fn decode_current_frequency(tlv: &[u8]) -> Result<u16, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("CurrentFrequency"))?),
_ => Err(ClusterError::UnexpectedType {
context: "CurrentFrequency",
}),
}
}
pub fn decode_min_frequency(tlv: &[u8]) -> Result<u16, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("MinFrequency"))?),
_ => Err(ClusterError::UnexpectedType {
context: "MinFrequency",
}),
}
}
pub fn decode_max_frequency(tlv: &[u8]) -> Result<u16, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("MaxFrequency"))?),
_ => Err(ClusterError::UnexpectedType {
context: "MaxFrequency",
}),
}
}
pub fn decode_options(tlv: &[u8]) -> Result<OptionsBitmap, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(OptionsBitmap::from_bits_retain(
u8::try_from(v).map_err(|_| ClusterError::InvalidLength("Options"))?,
)),
_ => Err(ClusterError::UnexpectedType { context: "Options" }),
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_options(value: OptionsBitmap) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.put_uint(Tag::Anonymous, u64::from(value.bits()))
.expect("infallible: vec writer");
buf
}
pub fn decode_on_off_transition_time(tlv: &[u8]) -> Result<u16, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("OnOffTransitionTime"))?),
_ => Err(ClusterError::UnexpectedType {
context: "OnOffTransitionTime",
}),
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_on_off_transition_time(value: u16) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.put_uint(Tag::Anonymous, u64::from(value))
.expect("infallible: vec writer");
buf
}
pub fn decode_on_level(tlv: &[u8]) -> Result<Nullable<u8>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(Nullable::Value(
u8::try_from(v).map_err(|_| ClusterError::InvalidLength("OnLevel"))?,
)),
_ => Err(ClusterError::UnexpectedType { context: "OnLevel" }),
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_on_level(value: Nullable<u8>) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
match value {
Nullable::Null => w.put_null(Tag::Anonymous).expect("infallible: vec writer"),
Nullable::Value(value) => {
w.put_uint(Tag::Anonymous, u64::from(value))
.expect("infallible: vec writer");
}
}
buf
}
pub fn decode_on_transition_time(tlv: &[u8]) -> Result<Nullable<u16>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => {
Ok(Nullable::Value(u16::try_from(v).map_err(|_| {
ClusterError::InvalidLength("OnTransitionTime")
})?))
}
_ => Err(ClusterError::UnexpectedType {
context: "OnTransitionTime",
}),
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_on_transition_time(value: Nullable<u16>) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
match value {
Nullable::Null => w.put_null(Tag::Anonymous).expect("infallible: vec writer"),
Nullable::Value(value) => {
w.put_uint(Tag::Anonymous, u64::from(value))
.expect("infallible: vec writer");
}
}
buf
}
pub fn decode_off_transition_time(tlv: &[u8]) -> Result<Nullable<u16>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => {
Ok(Nullable::Value(u16::try_from(v).map_err(|_| {
ClusterError::InvalidLength("OffTransitionTime")
})?))
}
_ => Err(ClusterError::UnexpectedType {
context: "OffTransitionTime",
}),
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_off_transition_time(value: Nullable<u16>) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
match value {
Nullable::Null => w.put_null(Tag::Anonymous).expect("infallible: vec writer"),
Nullable::Value(value) => {
w.put_uint(Tag::Anonymous, u64::from(value))
.expect("infallible: vec writer");
}
}
buf
}
pub fn decode_default_move_rate(tlv: &[u8]) -> Result<Nullable<u8>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => {
Ok(Nullable::Value(u8::try_from(v).map_err(|_| {
ClusterError::InvalidLength("DefaultMoveRate")
})?))
}
_ => Err(ClusterError::UnexpectedType {
context: "DefaultMoveRate",
}),
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_default_move_rate(value: Nullable<u8>) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
match value {
Nullable::Null => w.put_null(Tag::Anonymous).expect("infallible: vec writer"),
Nullable::Value(value) => {
w.put_uint(Tag::Anonymous, u64::from(value))
.expect("infallible: vec writer");
}
}
buf
}
pub fn decode_start_up_current_level(tlv: &[u8]) -> Result<Nullable<u8>, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Null, ..
}) => Ok(Nullable::Null),
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(Nullable::Value(u8::try_from(v).map_err(|_| {
ClusterError::InvalidLength("StartUpCurrentLevel")
})?)),
_ => Err(ClusterError::UnexpectedType {
context: "StartUpCurrentLevel",
}),
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_start_up_current_level(value: Nullable<u8>) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
match value {
Nullable::Null => w.put_null(Tag::Anonymous).expect("infallible: vec writer"),
Nullable::Value(value) => {
w.put_uint(Tag::Anonymous, u64::from(value))
.expect("infallible: vec writer");
}
}
buf
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_move_to_level(
level: u8,
transition_time: Nullable<u16>,
options_mask: OptionsBitmap,
options_override: OptionsBitmap,
) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous)
.expect("infallible: vec writer");
w.put_uint(Tag::Context(0), u64::from(level))
.expect("infallible: vec writer");
match transition_time {
Nullable::Null => w.put_null(Tag::Context(1)).expect("infallible: vec writer"),
Nullable::Value(transition_time) => {
w.put_uint(Tag::Context(1), u64::from(transition_time))
.expect("infallible: vec writer");
}
}
w.put_uint(Tag::Context(2), u64::from(options_mask.bits()))
.expect("infallible: vec writer");
w.put_uint(Tag::Context(3), u64::from(options_override.bits()))
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_move(
move_mode: MoveModeEnum,
rate: Nullable<u8>,
options_mask: OptionsBitmap,
options_override: OptionsBitmap,
) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous)
.expect("infallible: vec writer");
w.put_uint(Tag::Context(0), u64::from(move_mode.to_raw()))
.expect("infallible: vec writer");
match rate {
Nullable::Null => w.put_null(Tag::Context(1)).expect("infallible: vec writer"),
Nullable::Value(rate) => {
w.put_uint(Tag::Context(1), u64::from(rate))
.expect("infallible: vec writer");
}
}
w.put_uint(Tag::Context(2), u64::from(options_mask.bits()))
.expect("infallible: vec writer");
w.put_uint(Tag::Context(3), u64::from(options_override.bits()))
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_step(
step_mode: StepModeEnum,
step_size: u8,
transition_time: Nullable<u16>,
options_mask: OptionsBitmap,
options_override: OptionsBitmap,
) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous)
.expect("infallible: vec writer");
w.put_uint(Tag::Context(0), u64::from(step_mode.to_raw()))
.expect("infallible: vec writer");
w.put_uint(Tag::Context(1), u64::from(step_size))
.expect("infallible: vec writer");
match transition_time {
Nullable::Null => w.put_null(Tag::Context(2)).expect("infallible: vec writer"),
Nullable::Value(transition_time) => {
w.put_uint(Tag::Context(2), u64::from(transition_time))
.expect("infallible: vec writer");
}
}
w.put_uint(Tag::Context(3), u64::from(options_mask.bits()))
.expect("infallible: vec writer");
w.put_uint(Tag::Context(4), u64::from(options_override.bits()))
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_stop(options_mask: OptionsBitmap, options_override: OptionsBitmap) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous)
.expect("infallible: vec writer");
w.put_uint(Tag::Context(0), u64::from(options_mask.bits()))
.expect("infallible: vec writer");
w.put_uint(Tag::Context(1), u64::from(options_override.bits()))
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_move_to_level_with_on_off() -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous)
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_move_with_on_off() -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous)
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_step_with_on_off() -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous)
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_stop_with_on_off() -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous)
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_move_to_closest_frequency(frequency: u16) -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous)
.expect("infallible: vec writer");
w.put_uint(Tag::Context(0), u64::from(frequency))
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}