#![allow(clippy::too_many_arguments)]
use crate::tlv;
use anyhow;
use serde_json;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum InputType {
Internal = 0,
Aux = 1,
Coax = 2,
Composite = 3,
Hdmi = 4,
Input = 5,
Line = 6,
Optical = 7,
Video = 8,
Scart = 9,
Usb = 10,
Other = 11,
}
impl InputType {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(InputType::Internal),
1 => Some(InputType::Aux),
2 => Some(InputType::Coax),
3 => Some(InputType::Composite),
4 => Some(InputType::Hdmi),
5 => Some(InputType::Input),
6 => Some(InputType::Line),
7 => Some(InputType::Optical),
8 => Some(InputType::Video),
9 => Some(InputType::Scart),
10 => Some(InputType::Usb),
11 => Some(InputType::Other),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<InputType> for u8 {
fn from(val: InputType) -> Self {
val as u8
}
}
#[derive(Debug, serde::Serialize)]
pub struct InputInfo {
pub index: Option<u8>,
pub input_type: Option<InputType>,
pub name: Option<String>,
pub description: Option<String>,
}
pub fn encode_select_input(index: u8) -> anyhow::Result<Vec<u8>> {
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(vec![
(0, tlv::TlvItemValueEnc::UInt8(index)).into(),
]),
};
Ok(tlv.encode()?)
}
pub fn encode_rename_input(index: u8, name: String) -> anyhow::Result<Vec<u8>> {
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(vec![
(0, tlv::TlvItemValueEnc::UInt8(index)).into(),
(1, tlv::TlvItemValueEnc::String(name)).into(),
]),
};
Ok(tlv.encode()?)
}
pub fn decode_input_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<InputInfo>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
res.push(InputInfo {
index: item.get_int(&[0]).map(|v| v as u8),
input_type: item.get_int(&[1]).and_then(|v| InputType::from_u8(v as u8)),
name: item.get_string_owned(&[2]),
description: item.get_string_owned(&[3]),
});
}
}
Ok(res)
}
pub fn decode_current_input(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected UInt8"))
}
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x0507 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0507, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_input_list(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_current_input(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
_ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
}
}
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
vec![
(0x0000, "InputList"),
(0x0001, "CurrentInput"),
]
}
pub fn get_command_list() -> Vec<(u32, &'static str)> {
vec![
(0x00, "SelectInput"),
(0x01, "ShowInputStatus"),
(0x02, "HideInputStatus"),
(0x03, "RenameInput"),
]
}
pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
match cmd_id {
0x00 => Some("SelectInput"),
0x01 => Some("ShowInputStatus"),
0x02 => Some("HideInputStatus"),
0x03 => Some("RenameInput"),
_ => None,
}
}
pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
match cmd_id {
0x00 => Some(vec![
crate::clusters::codec::CommandField { tag: 0, name: "index", kind: crate::clusters::codec::FieldKind::U8, optional: false, nullable: false },
]),
0x01 => Some(vec![]),
0x02 => Some(vec![]),
0x03 => Some(vec![
crate::clusters::codec::CommandField { tag: 0, name: "index", kind: crate::clusters::codec::FieldKind::U8, optional: false, nullable: false },
crate::clusters::codec::CommandField { tag: 1, name: "name", kind: crate::clusters::codec::FieldKind::String, optional: false, nullable: false },
]),
_ => None,
}
}
pub fn encode_command_json(cmd_id: u32, args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
match cmd_id {
0x00 => {
let index = crate::clusters::codec::json_util::get_u8(args, "index")?;
encode_select_input(index)
}
0x01 => Ok(vec![]),
0x02 => Ok(vec![]),
0x03 => {
let index = crate::clusters::codec::json_util::get_u8(args, "index")?;
let name = crate::clusters::codec::json_util::get_string(args, "name")?;
encode_rename_input(index, name)
}
_ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
}
}
pub async fn select_input(conn: &crate::controller::Connection, endpoint: u16, index: u8) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_MEDIA_INPUT, crate::clusters::defs::CLUSTER_MEDIA_INPUT_CMD_ID_SELECTINPUT, &encode_select_input(index)?).await?;
Ok(())
}
pub async fn show_input_status(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_MEDIA_INPUT, crate::clusters::defs::CLUSTER_MEDIA_INPUT_CMD_ID_SHOWINPUTSTATUS, &[]).await?;
Ok(())
}
pub async fn hide_input_status(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_MEDIA_INPUT, crate::clusters::defs::CLUSTER_MEDIA_INPUT_CMD_ID_HIDEINPUTSTATUS, &[]).await?;
Ok(())
}
pub async fn rename_input(conn: &crate::controller::Connection, endpoint: u16, index: u8, name: String) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_MEDIA_INPUT, crate::clusters::defs::CLUSTER_MEDIA_INPUT_CMD_ID_RENAMEINPUT, &encode_rename_input(index, name)?).await?;
Ok(())
}
pub async fn read_input_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<InputInfo>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_MEDIA_INPUT, crate::clusters::defs::CLUSTER_MEDIA_INPUT_ATTR_ID_INPUTLIST).await?;
decode_input_list(&tlv)
}
pub async fn read_current_input(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_MEDIA_INPUT, crate::clusters::defs::CLUSTER_MEDIA_INPUT_ATTR_ID_CURRENTINPUT).await?;
decode_current_input(&tlv)
}