matc/clusters/codec/
identify.rs

1//! Generated Matter TLV encoders and decoders for Identify Cluster
2//! Cluster ID: 0x0003
3//! 
4//! This file is automatically generated from Identify.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13/// Encode Identify command (0x00)
14pub fn encode_identify(identify_time: u16) -> anyhow::Result<Vec<u8>> {
15    let tlv = tlv::TlvItemEnc {
16        tag: 0,
17        value: tlv::TlvItemValueEnc::StructInvisible(vec![
18        (0, tlv::TlvItemValueEnc::UInt16(identify_time)).into(),
19        ]),
20    };
21    Ok(tlv.encode()?)
22}
23
24/// Encode TriggerEffect command (0x40)
25pub fn encode_trigger_effect(effect_identifier: u8, effect_variant: u8) -> anyhow::Result<Vec<u8>> {
26    let tlv = tlv::TlvItemEnc {
27        tag: 0,
28        value: tlv::TlvItemValueEnc::StructInvisible(vec![
29        (0, tlv::TlvItemValueEnc::UInt8(effect_identifier)).into(),
30        (1, tlv::TlvItemValueEnc::UInt8(effect_variant)).into(),
31        ]),
32    };
33    Ok(tlv.encode()?)
34}
35
36// Attribute decoders
37
38/// Decode IdentifyTime attribute (0x0000)
39pub fn decode_identify_time(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
40    if let tlv::TlvItemValue::Int(v) = inp {
41        Ok(*v as u16)
42    } else {
43        Err(anyhow::anyhow!("Expected Integer"))
44    }
45}
46
47/// Decode IdentifyType attribute (0x0001)
48pub fn decode_identify_type(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
49    if let tlv::TlvItemValue::Int(v) = inp {
50        Ok(*v as u8)
51    } else {
52        Err(anyhow::anyhow!("Expected Integer"))
53    }
54}
55
56
57// JSON dispatcher function
58
59/// Decode attribute value and return as JSON string
60/// 
61/// # Parameters
62/// * `cluster_id` - The cluster identifier
63/// * `attribute_id` - The attribute identifier
64/// * `tlv_value` - The TLV value to decode
65/// 
66/// # Returns
67/// JSON string representation of the decoded value or error
68pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
69    // Verify this is the correct cluster
70    if cluster_id != 0x0003 {
71        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0003, got {}\"}}", cluster_id);
72    }
73    
74    match attribute_id {
75        0x0000 => {
76            match decode_identify_time(tlv_value) {
77                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
78                Err(e) => format!("{{\"error\": \"{}\"}}", e),
79            }
80        }
81        0x0001 => {
82            match decode_identify_type(tlv_value) {
83                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
84                Err(e) => format!("{{\"error\": \"{}\"}}", e),
85            }
86        }
87        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
88    }
89}
90
91/// Get list of all attributes supported by this cluster
92/// 
93/// # Returns
94/// Vector of tuples containing (attribute_id, attribute_name)
95pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
96    vec![
97        (0x0000, "IdentifyTime"),
98        (0x0001, "IdentifyType"),
99    ]
100}
101