matc/clusters/codec/
groups.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn encode_add_group(group_id: u8, group_name: String) -> anyhow::Result<Vec<u8>> {
15 let tlv = tlv::TlvItemEnc {
16 tag: 0,
17 value: tlv::TlvItemValueEnc::StructInvisible(vec![
18 (0, tlv::TlvItemValueEnc::UInt8(group_id)).into(),
19 (1, tlv::TlvItemValueEnc::String(group_name)).into(),
20 ]),
21 };
22 Ok(tlv.encode()?)
23}
24
25pub fn encode_view_group(group_id: u8) -> anyhow::Result<Vec<u8>> {
27 let tlv = tlv::TlvItemEnc {
28 tag: 0,
29 value: tlv::TlvItemValueEnc::StructInvisible(vec![
30 (0, tlv::TlvItemValueEnc::UInt8(group_id)).into(),
31 ]),
32 };
33 Ok(tlv.encode()?)
34}
35
36pub fn encode_get_group_membership(group_list: Vec<u8>) -> anyhow::Result<Vec<u8>> {
38 let tlv = tlv::TlvItemEnc {
39 tag: 0,
40 value: tlv::TlvItemValueEnc::StructInvisible(vec![
41 (0, tlv::TlvItemValueEnc::StructAnon(group_list.into_iter().map(|v| (0, tlv::TlvItemValueEnc::UInt8(v)).into()).collect())).into(),
42 ]),
43 };
44 Ok(tlv.encode()?)
45}
46
47pub fn encode_remove_group(group_id: u8) -> anyhow::Result<Vec<u8>> {
49 let tlv = tlv::TlvItemEnc {
50 tag: 0,
51 value: tlv::TlvItemValueEnc::StructInvisible(vec![
52 (0, tlv::TlvItemValueEnc::UInt8(group_id)).into(),
53 ]),
54 };
55 Ok(tlv.encode()?)
56}
57
58pub fn encode_add_group_if_identifying(group_id: u8, group_name: String) -> anyhow::Result<Vec<u8>> {
60 let tlv = tlv::TlvItemEnc {
61 tag: 0,
62 value: tlv::TlvItemValueEnc::StructInvisible(vec![
63 (0, tlv::TlvItemValueEnc::UInt8(group_id)).into(),
64 (1, tlv::TlvItemValueEnc::String(group_name)).into(),
65 ]),
66 };
67 Ok(tlv.encode()?)
68}
69
70pub fn decode_name_support(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
74 if let tlv::TlvItemValue::Int(v) = inp {
75 Ok(*v as u8)
76 } else {
77 Err(anyhow::anyhow!("Expected Integer"))
78 }
79}
80
81
82pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
94 if cluster_id != 0x0004 {
96 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0004, got {}\"}}", cluster_id);
97 }
98
99 match attribute_id {
100 0x0000 => {
101 match decode_name_support(tlv_value) {
102 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
103 Err(e) => format!("{{\"error\": \"{}\"}}", e),
104 }
105 }
106 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
107 }
108}
109
110pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
115 vec![
116 (0x0000, "NameSupport"),
117 ]
118}
119