matc/clusters/codec/
media_input.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, serde::Serialize)]
14pub struct InputInfo {
15 pub index: Option<u8>,
16 pub input_type: Option<u8>,
17 pub name: Option<String>,
18 pub description: Option<String>,
19}
20
21pub fn encode_select_input(index: u8) -> anyhow::Result<Vec<u8>> {
25 let tlv = tlv::TlvItemEnc {
26 tag: 0,
27 value: tlv::TlvItemValueEnc::StructInvisible(vec![
28 (0, tlv::TlvItemValueEnc::UInt8(index)).into(),
29 ]),
30 };
31 Ok(tlv.encode()?)
32}
33
34pub fn encode_rename_input(index: u8, name: String) -> anyhow::Result<Vec<u8>> {
36 let tlv = tlv::TlvItemEnc {
37 tag: 0,
38 value: tlv::TlvItemValueEnc::StructInvisible(vec![
39 (0, tlv::TlvItemValueEnc::UInt8(index)).into(),
40 (1, tlv::TlvItemValueEnc::String(name)).into(),
41 ]),
42 };
43 Ok(tlv.encode()?)
44}
45
46pub fn decode_input_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<InputInfo>> {
50 let mut res = Vec::new();
51 if let tlv::TlvItemValue::List(v) = inp {
52 for item in v {
53 res.push(InputInfo {
54 index: item.get_int(&[0]).map(|v| v as u8),
55 input_type: item.get_int(&[1]).map(|v| v as u8),
56 name: item.get_string_owned(&[2]),
57 description: item.get_string_owned(&[3]),
58 });
59 }
60 }
61 Ok(res)
62}
63
64pub fn decode_current_input(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
66 if let tlv::TlvItemValue::Int(v) = inp {
67 Ok(*v as u8)
68 } else {
69 Err(anyhow::anyhow!("Expected Integer"))
70 }
71}
72
73
74pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
86 if cluster_id != 0x0507 {
88 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0507, got {}\"}}", cluster_id);
89 }
90
91 match attribute_id {
92 0x0000 => {
93 match decode_input_list(tlv_value) {
94 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
95 Err(e) => format!("{{\"error\": \"{}\"}}", e),
96 }
97 }
98 0x0001 => {
99 match decode_current_input(tlv_value) {
100 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
101 Err(e) => format!("{{\"error\": \"{}\"}}", e),
102 }
103 }
104 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
105 }
106}
107
108pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
113 vec![
114 (0x0000, "InputList"),
115 (0x0001, "CurrentInput"),
116 ]
117}
118