#![allow(clippy::too_many_arguments)]
use crate::tlv;
use anyhow;
use serde_json;
#[derive(Debug, serde::Serialize)]
pub struct DeviceType {
pub device_type: Option<u32>,
pub revision: Option<u16>,
}
pub fn decode_device_type_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<DeviceType>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
res.push(DeviceType {
device_type: item.get_int(&[0]).map(|v| v as u32),
revision: item.get_int(&[1]).map(|v| v as u16),
});
}
}
Ok(res)
}
pub fn decode_server_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u32>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
res.push(*i as u32);
}
}
}
Ok(res)
}
pub fn decode_client_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u32>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
res.push(*i as u32);
}
}
}
Ok(res)
}
pub fn decode_parts_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
res.push(*i as u16);
}
}
}
Ok(res)
}
pub fn decode_tag_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u8>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
res.push(*i as u8);
}
}
}
Ok(res)
}
pub fn decode_endpoint_unique_id(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
if let tlv::TlvItemValue::String(v) = inp {
Ok(v.clone())
} else {
Err(anyhow::anyhow!("Expected String"))
}
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x001D {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x001D, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_device_type_list(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_server_list(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_client_list(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0003 => {
match decode_parts_list(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0004 => {
match decode_tag_list(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0005 => {
match decode_endpoint_unique_id(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, "DeviceTypeList"),
(0x0001, "ServerList"),
(0x0002, "ClientList"),
(0x0003, "PartsList"),
(0x0004, "TagList"),
(0x0005, "EndpointUniqueID"),
]
}
pub async fn read_device_type_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<DeviceType>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_DEVICETYPELIST).await?;
decode_device_type_list(&tlv)
}
pub async fn read_server_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u32>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_SERVERLIST).await?;
decode_server_list(&tlv)
}
pub async fn read_client_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u32>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_CLIENTLIST).await?;
decode_client_list(&tlv)
}
pub async fn read_parts_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_PARTSLIST).await?;
decode_parts_list(&tlv)
}
pub async fn read_tag_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u8>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_TAGLIST).await?;
decode_tag_list(&tlv)
}
pub async fn read_endpoint_unique_id(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<String> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_ENDPOINTUNIQUEID).await?;
decode_endpoint_unique_id(&tlv)
}