matc/clusters/codec/
application_launcher.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[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
25pub 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
39pub 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
50pub 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
61pub 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
76pub fn decode_current_app(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<ApplicationEP>> {
78 if let tlv::TlvItemValue::List(_fields) = inp {
79 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 {
103 Ok(None)
104 }
106}
107
108
109pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
121 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
143pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
148 vec![
149 (0x0000, "CatalogList"),
150 (0x0001, "CurrentApp"),
151 ]
152}
153