matc/clusters/codec/
ecosystem_information_cluster.rs

1//! Matter TLV encoders and decoders for Ecosystem Information Cluster
2//! Cluster ID: 0x0750
3//!
4//! This file is automatically generated from EcosystemInformationCluster.xml
5
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Struct definitions
14
15#[derive(Debug, serde::Serialize)]
16pub struct DeviceType {
17    pub device_type: Option<u32>,
18    pub revision: Option<u16>,
19}
20
21#[derive(Debug, serde::Serialize)]
22pub struct EcosystemDevice {
23    pub device_name: Option<String>,
24    pub device_name_last_edit: Option<u64>,
25    pub bridged_endpoint: Option<u16>,
26    pub original_endpoint: Option<u16>,
27    pub device_types: Option<Vec<DeviceType>>,
28    pub unique_location_i_ds: Option<Vec<String>>,
29    pub unique_location_i_ds_last_edit: Option<u64>,
30}
31
32#[derive(Debug, serde::Serialize)]
33pub struct EcosystemLocation {
34    pub unique_location_id: Option<String>,
35    pub location_descriptor: Option<LocationDescriptor>,
36    pub location_descriptor_last_edit: Option<u64>,
37}
38
39#[derive(Debug, serde::Serialize)]
40pub struct LocationDescriptor {
41    pub location_name: Option<String>,
42    pub floor_number: Option<u16>,
43    pub area_type: Option<u8>,
44}
45
46// Attribute decoders
47
48/// Decode DeviceDirectory attribute (0x0000)
49pub fn decode_device_directory(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<EcosystemDevice>> {
50    let mut res = Vec::new();
51    if let tlv::TlvItemValue::List(v) = inp {
52        for item in v {
53            res.push(EcosystemDevice {
54                device_name: item.get_string_owned(&[0]),
55                device_name_last_edit: item.get_int(&[1]),
56                bridged_endpoint: item.get_int(&[2]).map(|v| v as u16),
57                original_endpoint: item.get_int(&[3]).map(|v| v as u16),
58                device_types: {
59                    if let Some(tlv::TlvItemValue::List(l)) = item.get(&[4]) {
60                        let mut items = Vec::new();
61                        for list_item in l {
62                            items.push(DeviceType {
63                device_type: list_item.get_int(&[0]).map(|v| v as u32),
64                revision: list_item.get_int(&[1]).map(|v| v as u16),
65                            });
66                        }
67                        Some(items)
68                    } else {
69                        None
70                    }
71                },
72                unique_location_i_ds: {
73                    if let Some(tlv::TlvItemValue::List(l)) = item.get(&[5]) {
74                        let items: Vec<String> = l.iter().filter_map(|e| { if let tlv::TlvItemValue::String(v) = &e.value { Some(v.clone()) } else { None } }).collect();
75                        Some(items)
76                    } else {
77                        None
78                    }
79                },
80                unique_location_i_ds_last_edit: item.get_int(&[6]),
81            });
82        }
83    }
84    Ok(res)
85}
86
87/// Decode LocationDirectory attribute (0x0001)
88pub fn decode_location_directory(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<EcosystemLocation>> {
89    let mut res = Vec::new();
90    if let tlv::TlvItemValue::List(v) = inp {
91        for item in v {
92            res.push(EcosystemLocation {
93                unique_location_id: item.get_string_owned(&[0]),
94                location_descriptor: {
95                    if let Some(nested_tlv) = item.get(&[1]) {
96                        if let tlv::TlvItemValue::List(_) = nested_tlv {
97                            let nested_item = tlv::TlvItem { tag: 1, value: nested_tlv.clone() };
98                            Some(LocationDescriptor {
99                location_name: nested_item.get_string_owned(&[0]),
100                floor_number: nested_item.get_int(&[1]).map(|v| v as u16),
101                area_type: nested_item.get_int(&[2]).map(|v| v as u8),
102                            })
103                        } else {
104                            None
105                        }
106                    } else {
107                        None
108                    }
109                },
110                location_descriptor_last_edit: item.get_int(&[2]),
111            });
112        }
113    }
114    Ok(res)
115}
116
117
118// JSON dispatcher function
119
120/// Decode attribute value and return as JSON string
121///
122/// # Parameters
123/// * `cluster_id` - The cluster identifier
124/// * `attribute_id` - The attribute identifier
125/// * `tlv_value` - The TLV value to decode
126///
127/// # Returns
128/// JSON string representation of the decoded value or error
129pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
130    // Verify this is the correct cluster
131    if cluster_id != 0x0750 {
132        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0750, got {}\"}}", cluster_id);
133    }
134
135    match attribute_id {
136        0x0000 => {
137            match decode_device_directory(tlv_value) {
138                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
139                Err(e) => format!("{{\"error\": \"{}\"}}", e),
140            }
141        }
142        0x0001 => {
143            match decode_location_directory(tlv_value) {
144                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
145                Err(e) => format!("{{\"error\": \"{}\"}}", e),
146            }
147        }
148        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
149    }
150}
151
152/// Get list of all attributes supported by this cluster
153///
154/// # Returns
155/// Vector of tuples containing (attribute_id, attribute_name)
156pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
157    vec![
158        (0x0000, "DeviceDirectory"),
159        (0x0001, "LocationDirectory"),
160    ]
161}
162
163// Typed facade (invokes + reads)
164
165/// Read `DeviceDirectory` attribute from cluster `Ecosystem Information`.
166pub async fn read_device_directory(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<EcosystemDevice>> {
167    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_ECOSYSTEM_INFORMATION, crate::clusters::defs::CLUSTER_ECOSYSTEM_INFORMATION_ATTR_ID_DEVICEDIRECTORY).await?;
168    decode_device_directory(&tlv)
169}
170
171/// Read `LocationDirectory` attribute from cluster `Ecosystem Information`.
172pub async fn read_location_directory(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<EcosystemLocation>> {
173    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_ECOSYSTEM_INFORMATION, crate::clusters::defs::CLUSTER_ECOSYSTEM_INFORMATION_ATTR_ID_LOCATIONDIRECTORY).await?;
174    decode_location_directory(&tlv)
175}
176