matc/clusters/codec/
application_launcher.rs

1//! Generated Matter TLV encoders and decoders for Application Launcher Cluster
2//! Cluster ID: 0x050C
3//! 
4//! This file is automatically generated from ApplicationLauncher.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Struct definitions
12
13#[derive(Debug, serde::Serialize)]
14pub struct ApplicationEP {
15    pub application: Option<Application>,
16    pub endpoint: Option<u16>,
17}
18
19#[derive(Debug, serde::Serialize)]
20pub struct Application {
21    pub catalog_vendor_id: Option<u16>,
22    pub application_id: Option<String>,
23}
24
25// Command encoders
26
27/// Encode LaunchApp command (0x00)
28pub fn encode_launch_app(application: u8, data: Vec<u8>) -> anyhow::Result<Vec<u8>> {
29    let tlv = tlv::TlvItemEnc {
30        tag: 0,
31        value: tlv::TlvItemValueEnc::StructInvisible(vec![
32        (0, tlv::TlvItemValueEnc::UInt8(application)).into(),
33        (1, tlv::TlvItemValueEnc::OctetString(data)).into(),
34        ]),
35    };
36    Ok(tlv.encode()?)
37}
38
39/// Encode StopApp command (0x01)
40pub fn encode_stop_app(application: u8) -> anyhow::Result<Vec<u8>> {
41    let tlv = tlv::TlvItemEnc {
42        tag: 0,
43        value: tlv::TlvItemValueEnc::StructInvisible(vec![
44        (0, tlv::TlvItemValueEnc::UInt8(application)).into(),
45        ]),
46    };
47    Ok(tlv.encode()?)
48}
49
50/// Encode HideApp command (0x02)
51pub fn encode_hide_app(application: u8) -> anyhow::Result<Vec<u8>> {
52    let tlv = tlv::TlvItemEnc {
53        tag: 0,
54        value: tlv::TlvItemValueEnc::StructInvisible(vec![
55        (0, tlv::TlvItemValueEnc::UInt8(application)).into(),
56        ]),
57    };
58    Ok(tlv.encode()?)
59}
60
61// Attribute decoders
62
63/// Decode CatalogList attribute (0x0000)
64pub fn decode_catalog_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
65    let mut res = Vec::new();
66    if let tlv::TlvItemValue::List(v) = inp {
67        for item in v {
68            if let tlv::TlvItemValue::Int(i) = &item.value {
69                res.push(*i as u16);
70            }
71        }
72    }
73    Ok(res)
74}
75
76/// Decode CurrentApp attribute (0x0001)
77pub fn decode_current_app(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<ApplicationEP>> {
78    if let tlv::TlvItemValue::List(_fields) = inp {
79        // Struct with fields
80        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
81        Ok(Some(ApplicationEP {
82                application: {
83                    if let Some(nested_tlv) = item.get(&[0]) {
84                        if let tlv::TlvItemValue::List(_) = nested_tlv {
85                            let nested_item = tlv::TlvItem { tag: 0, value: nested_tlv.clone() };
86                            Some(Application {
87                catalog_vendor_id: nested_item.get_int(&[0]).map(|v| v as u16),
88                application_id: nested_item.get_string_owned(&[1]),
89                            })
90                        } else {
91                            None
92                        }
93                    } else {
94                        None
95                    }
96                },
97                endpoint: item.get_int(&[1]).map(|v| v as u16),
98        }))
99    //} else if let tlv::TlvItemValue::Null = inp {
100    //    // Null value for nullable struct
101    //    Ok(None)
102    } else {
103    Ok(None)
104    //    Err(anyhow::anyhow!("Expected struct fields or null"))
105    }
106}
107
108
109// JSON dispatcher function
110
111/// Decode attribute value and return as JSON string
112/// 
113/// # Parameters
114/// * `cluster_id` - The cluster identifier
115/// * `attribute_id` - The attribute identifier
116/// * `tlv_value` - The TLV value to decode
117/// 
118/// # Returns
119/// JSON string representation of the decoded value or error
120pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
121    // Verify this is the correct cluster
122    if cluster_id != 0x050C {
123        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x050C, got {}\"}}", cluster_id);
124    }
125    
126    match attribute_id {
127        0x0000 => {
128            match decode_catalog_list(tlv_value) {
129                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
130                Err(e) => format!("{{\"error\": \"{}\"}}", e),
131            }
132        }
133        0x0001 => {
134            match decode_current_app(tlv_value) {
135                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
136                Err(e) => format!("{{\"error\": \"{}\"}}", e),
137            }
138        }
139        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
140    }
141}
142
143/// Get list of all attributes supported by this cluster
144/// 
145/// # Returns
146/// Vector of tuples containing (attribute_id, attribute_name)
147pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
148    vec![
149        (0x0000, "CatalogList"),
150        (0x0001, "CurrentApp"),
151    ]
152}
153