matc/clusters/codec/
groups.rs

1//! Generated Matter TLV encoders and decoders for Groups Cluster
2//! Cluster ID: 0x0004
3//! 
4//! This file is automatically generated from Groups.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13/// Encode AddGroup command (0x00)
14pub 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
25/// Encode ViewGroup command (0x01)
26pub 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
36/// Encode GetGroupMembership command (0x02)
37pub 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
47/// Encode RemoveGroup command (0x03)
48pub 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
58/// Encode AddGroupIfIdentifying command (0x05)
59pub 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
70// Attribute decoders
71
72/// Decode NameSupport attribute (0x0000)
73pub 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
82// JSON dispatcher function
83
84/// Decode attribute value and return as JSON string
85/// 
86/// # Parameters
87/// * `cluster_id` - The cluster identifier
88/// * `attribute_id` - The attribute identifier
89/// * `tlv_value` - The TLV value to decode
90/// 
91/// # Returns
92/// JSON string representation of the decoded value or error
93pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
94    // Verify this is the correct cluster
95    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
110/// Get list of all attributes supported by this cluster
111/// 
112/// # Returns
113/// Vector of tuples containing (attribute_id, attribute_name)
114pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
115    vec![
116        (0x0000, "NameSupport"),
117    ]
118}
119