matc/clusters/codec/
media_input.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u8)]
15pub enum InputType {
16 Internal = 0,
18 Aux = 1,
19 Coax = 2,
20 Composite = 3,
21 Hdmi = 4,
22 Input = 5,
23 Line = 6,
24 Optical = 7,
25 Video = 8,
26 Scart = 9,
27 Usb = 10,
28 Other = 11,
29}
30
31impl InputType {
32 pub fn from_u8(value: u8) -> Option<Self> {
34 match value {
35 0 => Some(InputType::Internal),
36 1 => Some(InputType::Aux),
37 2 => Some(InputType::Coax),
38 3 => Some(InputType::Composite),
39 4 => Some(InputType::Hdmi),
40 5 => Some(InputType::Input),
41 6 => Some(InputType::Line),
42 7 => Some(InputType::Optical),
43 8 => Some(InputType::Video),
44 9 => Some(InputType::Scart),
45 10 => Some(InputType::Usb),
46 11 => Some(InputType::Other),
47 _ => None,
48 }
49 }
50
51 pub fn to_u8(self) -> u8 {
53 self as u8
54 }
55}
56
57impl From<InputType> for u8 {
58 fn from(val: InputType) -> Self {
59 val as u8
60 }
61}
62
63#[derive(Debug, serde::Serialize)]
66pub struct InputInfo {
67 pub index: Option<u8>,
68 pub input_type: Option<InputType>,
69 pub name: Option<String>,
70 pub description: Option<String>,
71}
72
73pub fn encode_select_input(index: u8) -> anyhow::Result<Vec<u8>> {
77 let tlv = tlv::TlvItemEnc {
78 tag: 0,
79 value: tlv::TlvItemValueEnc::StructInvisible(vec![
80 (0, tlv::TlvItemValueEnc::UInt8(index)).into(),
81 ]),
82 };
83 Ok(tlv.encode()?)
84}
85
86pub fn encode_rename_input(index: u8, name: String) -> anyhow::Result<Vec<u8>> {
88 let tlv = tlv::TlvItemEnc {
89 tag: 0,
90 value: tlv::TlvItemValueEnc::StructInvisible(vec![
91 (0, tlv::TlvItemValueEnc::UInt8(index)).into(),
92 (1, tlv::TlvItemValueEnc::String(name)).into(),
93 ]),
94 };
95 Ok(tlv.encode()?)
96}
97
98pub fn decode_input_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<InputInfo>> {
102 let mut res = Vec::new();
103 if let tlv::TlvItemValue::List(v) = inp {
104 for item in v {
105 res.push(InputInfo {
106 index: item.get_int(&[0]).map(|v| v as u8),
107 input_type: item.get_int(&[1]).and_then(|v| InputType::from_u8(v as u8)),
108 name: item.get_string_owned(&[2]),
109 description: item.get_string_owned(&[3]),
110 });
111 }
112 }
113 Ok(res)
114}
115
116pub fn decode_current_input(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
118 if let tlv::TlvItemValue::Int(v) = inp {
119 Ok(*v as u8)
120 } else {
121 Err(anyhow::anyhow!("Expected UInt8"))
122 }
123}
124
125
126pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
138 if cluster_id != 0x0507 {
140 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0507, got {}\"}}", cluster_id);
141 }
142
143 match attribute_id {
144 0x0000 => {
145 match decode_input_list(tlv_value) {
146 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
147 Err(e) => format!("{{\"error\": \"{}\"}}", e),
148 }
149 }
150 0x0001 => {
151 match decode_current_input(tlv_value) {
152 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
153 Err(e) => format!("{{\"error\": \"{}\"}}", e),
154 }
155 }
156 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
157 }
158}
159
160pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
165 vec![
166 (0x0000, "InputList"),
167 (0x0001, "CurrentInput"),
168 ]
169}
170