matc 0.1.3

Matter protocol library (controller side)
Documentation
//! Matter TLV encoders and decoders for Descriptor Cluster
//! Cluster ID: 0x001D
//!
//! This file is automatically generated from Descriptor-Cluster.xml

#![allow(clippy::too_many_arguments)]

use crate::tlv;
use anyhow;
use serde_json;


// Struct definitions

#[derive(Debug, serde::Serialize)]
pub struct DeviceType {
    pub device_type: Option<u32>,
    pub revision: Option<u16>,
}

// Attribute decoders

/// Decode DeviceTypeList attribute (0x0000)
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)
}

/// Decode ServerList attribute (0x0001)
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)
}

/// Decode ClientList attribute (0x0002)
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)
}

/// Decode PartsList attribute (0x0003)
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)
}

/// Decode TagList attribute (0x0004)
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)
}

/// Decode EndpointUniqueID attribute (0x0005)
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"))
    }
}


// JSON dispatcher function

/// Decode attribute value and return as JSON string
///
/// # Parameters
/// * `cluster_id` - The cluster identifier
/// * `attribute_id` - The attribute identifier
/// * `tlv_value` - The TLV value to decode
///
/// # Returns
/// JSON string representation of the decoded value or error
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
    // Verify this is the correct cluster
    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),
    }
}

/// Get list of all attributes supported by this cluster
///
/// # Returns
/// Vector of tuples containing (attribute_id, attribute_name)
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
    vec![
        (0x0000, "DeviceTypeList"),
        (0x0001, "ServerList"),
        (0x0002, "ClientList"),
        (0x0003, "PartsList"),
        (0x0004, "TagList"),
        (0x0005, "EndpointUniqueID"),
    ]
}

// Typed facade (invokes + reads)

/// Read `DeviceTypeList` attribute from cluster `Descriptor`.
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)
}

/// Read `ServerList` attribute from cluster `Descriptor`.
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)
}

/// Read `ClientList` attribute from cluster `Descriptor`.
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)
}

/// Read `PartsList` attribute from cluster `Descriptor`.
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)
}

/// Read `TagList` attribute from cluster `Descriptor`.
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)
}

/// Read `EndpointUniqueID` attribute from cluster `Descriptor`.
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)
}