#![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 = 0x0004;
pub const CLUSTER_REVISION: u16 = 4;
pub mod command_id {
pub const ADD_GROUP: u32 = 0x00;
pub const ADD_GROUP_RESPONSE: u32 = 0x00;
pub const VIEW_GROUP: u32 = 0x01;
pub const VIEW_GROUP_RESPONSE: u32 = 0x01;
pub const GET_GROUP_MEMBERSHIP: u32 = 0x02;
pub const GET_GROUP_MEMBERSHIP_RESPONSE: u32 = 0x02;
pub const REMOVE_GROUP: u32 = 0x03;
pub const REMOVE_GROUP_RESPONSE: u32 = 0x03;
pub const REMOVE_ALL_GROUPS: u32 = 0x04;
pub const ADD_GROUP_IF_IDENTIFYING: u32 = 0x05;
}
pub mod attribute_id {
pub const NAME_SUPPORT: u32 = 0x0000;
}
bitflags::bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Feature: u32 {
const GN = 1 << 0;
}
}
bitflags::bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct NameSupportBitmap: u8 {
const GROUP_NAMES = 1 << 7;
}
}
pub fn decode_name_support(tlv: &[u8]) -> Result<NameSupportBitmap, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => Ok(NameSupportBitmap::from_bits_retain(
u8::try_from(v).map_err(|_| ClusterError::InvalidLength("NameSupport"))?,
)),
_ => Err(ClusterError::UnexpectedType {
context: "NameSupport",
}),
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_add_group(group_id: u16, group_name: &String) -> 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(group_id))
.expect("infallible: vec writer");
w.put_utf8(Tag::Context(1), &group_name)
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct AddGroupResponse {
pub status: u8,
pub group_id: u16,
}
impl AddGroupResponse {
pub fn decode_from(r: &mut TlvReader<'_>) -> Result<Self, ClusterError> {
let mut f_status: Option<u8> = None;
let mut f_group_id: Option<u16> = None;
loop {
match r.next()? {
Some(Element::ContainerEnd) => break,
Some(Element::Scalar {
tag: Tag::Context(0),
value: Value::Uint(v),
}) => {
f_status =
Some(u8::try_from(v).map_err(|_| ClusterError::InvalidLength("Status"))?)
}
Some(Element::Scalar {
tag: Tag::Context(1),
value: Value::Uint(v),
}) => {
f_group_id =
Some(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("GroupId"))?)
}
None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
Some(Element::ContainerStart { .. }) => r.skip_container()?,
Some(_) => {} }
}
Ok(Self {
status: f_status.ok_or(ClusterError::MissingField("Status"))?,
group_id: f_group_id.ok_or(ClusterError::MissingField("GroupId"))?,
})
}
pub fn decode(tlv: &[u8]) -> Result<Self, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::ContainerStart {
kind: ContainerKind::Structure,
..
}) => {}
_ => {
return Err(ClusterError::UnexpectedType {
context: "AddGroupResponse",
})
}
}
Self::decode_from(&mut r)
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_view_group(group_id: 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(group_id))
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ViewGroupResponse {
pub status: u8,
pub group_id: u16,
pub group_name: String,
}
impl ViewGroupResponse {
pub fn decode_from(r: &mut TlvReader<'_>) -> Result<Self, ClusterError> {
let mut f_status: Option<u8> = None;
let mut f_group_id: Option<u16> = None;
let mut f_group_name: Option<String> = None;
loop {
match r.next()? {
Some(Element::ContainerEnd) => break,
Some(Element::Scalar {
tag: Tag::Context(0),
value: Value::Uint(v),
}) => {
f_status =
Some(u8::try_from(v).map_err(|_| ClusterError::InvalidLength("Status"))?)
}
Some(Element::Scalar {
tag: Tag::Context(1),
value: Value::Uint(v),
}) => {
f_group_id =
Some(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("GroupId"))?)
}
Some(Element::Scalar {
tag: Tag::Context(2),
value: Value::Utf8(v),
}) => f_group_name = Some(v),
None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
Some(Element::ContainerStart { .. }) => r.skip_container()?,
Some(_) => {} }
}
Ok(Self {
status: f_status.ok_or(ClusterError::MissingField("Status"))?,
group_id: f_group_id.ok_or(ClusterError::MissingField("GroupId"))?,
group_name: f_group_name.ok_or(ClusterError::MissingField("GroupName"))?,
})
}
pub fn decode(tlv: &[u8]) -> Result<Self, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::ContainerStart {
kind: ContainerKind::Structure,
..
}) => {}
_ => {
return Err(ClusterError::UnexpectedType {
context: "ViewGroupResponse",
})
}
}
Self::decode_from(&mut r)
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_get_group_membership(group_list: &Vec<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.start_array(Tag::Context(0))
.expect("infallible: vec writer");
for el in group_list.iter().copied() {
w.put_uint(Tag::Anonymous, u64::from(el))
.expect("infallible: vec writer");
}
w.end_container().expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct GetGroupMembershipResponse {
pub capacity: Nullable<u8>,
pub group_list: Vec<u16>,
}
impl GetGroupMembershipResponse {
pub fn decode_from(r: &mut TlvReader<'_>) -> Result<Self, ClusterError> {
let mut f_capacity: Option<Nullable<u8>> = None;
let mut f_group_list: Option<Vec<u16>> = None;
loop {
match r.next()? {
Some(Element::ContainerEnd) => break,
Some(Element::Scalar {
tag: Tag::Context(0),
value: Value::Null,
}) => f_capacity = Some(Nullable::Null),
Some(Element::Scalar {
tag: Tag::Context(0),
value: Value::Uint(v),
}) => {
f_capacity = Some(Nullable::Value(
u8::try_from(v).map_err(|_| ClusterError::InvalidLength("Capacity"))?,
))
}
Some(Element::ContainerStart {
tag: Tag::Context(1),
kind: ContainerKind::Array,
}) => {
let mut out = Vec::new();
loop {
match r.next()? {
Some(Element::ContainerEnd) => break,
Some(Element::Scalar {
value: Value::Uint(v),
..
}) => out.push(
u16::try_from(v)
.map_err(|_| ClusterError::InvalidLength("GroupList"))?,
),
None => {
return Err(ClusterError::Tlv(
matter_codec::Error::UnclosedContainer,
))
}
Some(Element::ContainerStart { .. }) => r.skip_container()?,
Some(_) => {} }
}
f_group_list = Some(out);
}
None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
Some(Element::ContainerStart { .. }) => r.skip_container()?,
Some(_) => {} }
}
Ok(Self {
capacity: f_capacity.ok_or(ClusterError::MissingField("Capacity"))?,
group_list: f_group_list.ok_or(ClusterError::MissingField("GroupList"))?,
})
}
pub fn decode(tlv: &[u8]) -> Result<Self, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::ContainerStart {
kind: ContainerKind::Structure,
..
}) => {}
_ => {
return Err(ClusterError::UnexpectedType {
context: "GetGroupMembershipResponse",
})
}
}
Self::decode_from(&mut r)
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_remove_group(group_id: 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(group_id))
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct RemoveGroupResponse {
pub status: u8,
pub group_id: u16,
}
impl RemoveGroupResponse {
pub fn decode_from(r: &mut TlvReader<'_>) -> Result<Self, ClusterError> {
let mut f_status: Option<u8> = None;
let mut f_group_id: Option<u16> = None;
loop {
match r.next()? {
Some(Element::ContainerEnd) => break,
Some(Element::Scalar {
tag: Tag::Context(0),
value: Value::Uint(v),
}) => {
f_status =
Some(u8::try_from(v).map_err(|_| ClusterError::InvalidLength("Status"))?)
}
Some(Element::Scalar {
tag: Tag::Context(1),
value: Value::Uint(v),
}) => {
f_group_id =
Some(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("GroupId"))?)
}
None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
Some(Element::ContainerStart { .. }) => r.skip_container()?,
Some(_) => {} }
}
Ok(Self {
status: f_status.ok_or(ClusterError::MissingField("Status"))?,
group_id: f_group_id.ok_or(ClusterError::MissingField("GroupId"))?,
})
}
pub fn decode(tlv: &[u8]) -> Result<Self, ClusterError> {
let mut r = TlvReader::new(tlv);
match r.next()? {
Some(Element::ContainerStart {
kind: ContainerKind::Structure,
..
}) => {}
_ => {
return Err(ClusterError::UnexpectedType {
context: "RemoveGroupResponse",
})
}
}
Self::decode_from(&mut r)
}
}
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn encode_remove_all_groups() -> 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_add_group_if_identifying(group_id: u16, group_name: &String) -> 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(group_id))
.expect("infallible: vec writer");
w.put_utf8(Tag::Context(1), &group_name)
.expect("infallible: vec writer");
w.end_container().expect("infallible: vec writer");
buf
}