matc/clusters/codec/
ota_requestor.rs

1//! Generated Matter TLV encoders and decoders for OTA Software Update Requestor Cluster
2//! Cluster ID: 0x002A
3//! 
4//! This file is automatically generated from OTARequestor.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Struct definitions
12
13#[derive(Debug, serde::Serialize)]
14pub struct ProviderLocation {
15    pub provider_node_id: Option<u64>,
16    pub endpoint: Option<u16>,
17}
18
19// Command encoders
20
21/// Encode AnnounceOTAProvider command (0x00)
22pub fn encode_announce_ota_provider(provider_node_id: u64, vendor_id: u16, announcement_reason: u8, metadata_for_node: Vec<u8>, endpoint: u16) -> anyhow::Result<Vec<u8>> {
23    let tlv = tlv::TlvItemEnc {
24        tag: 0,
25        value: tlv::TlvItemValueEnc::StructInvisible(vec![
26        (0, tlv::TlvItemValueEnc::UInt64(provider_node_id)).into(),
27        (1, tlv::TlvItemValueEnc::UInt16(vendor_id)).into(),
28        (2, tlv::TlvItemValueEnc::UInt8(announcement_reason)).into(),
29        (3, tlv::TlvItemValueEnc::OctetString(metadata_for_node)).into(),
30        (4, tlv::TlvItemValueEnc::UInt16(endpoint)).into(),
31        ]),
32    };
33    Ok(tlv.encode()?)
34}
35
36// Attribute decoders
37
38/// Decode DefaultOTAProviders attribute (0x0000)
39pub fn decode_default_ota_providers(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<ProviderLocation>> {
40    let mut res = Vec::new();
41    if let tlv::TlvItemValue::List(v) = inp {
42        for item in v {
43            res.push(ProviderLocation {
44                provider_node_id: item.get_int(&[1]),
45                endpoint: item.get_int(&[2]).map(|v| v as u16),
46            });
47        }
48    }
49    Ok(res)
50}
51
52/// Decode UpdatePossible attribute (0x0001)
53pub fn decode_update_possible(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
54    if let tlv::TlvItemValue::Bool(v) = inp {
55        Ok(*v)
56    } else {
57        Err(anyhow::anyhow!("Expected Bool"))
58    }
59}
60
61/// Decode UpdateState attribute (0x0002)
62pub fn decode_update_state(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
63    if let tlv::TlvItemValue::Int(v) = inp {
64        Ok(*v as u8)
65    } else {
66        Err(anyhow::anyhow!("Expected Integer"))
67    }
68}
69
70/// Decode UpdateStateProgress attribute (0x0003)
71pub fn decode_update_state_progress(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
72    if let tlv::TlvItemValue::Int(v) = inp {
73        Ok(Some(*v as u8))
74    } else {
75        Ok(None)
76    }
77}
78
79
80// JSON dispatcher function
81
82/// Decode attribute value and return as JSON string
83/// 
84/// # Parameters
85/// * `cluster_id` - The cluster identifier
86/// * `attribute_id` - The attribute identifier
87/// * `tlv_value` - The TLV value to decode
88/// 
89/// # Returns
90/// JSON string representation of the decoded value or error
91pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
92    // Verify this is the correct cluster
93    if cluster_id != 0x002A {
94        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002A, got {}\"}}", cluster_id);
95    }
96    
97    match attribute_id {
98        0x0000 => {
99            match decode_default_ota_providers(tlv_value) {
100                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
101                Err(e) => format!("{{\"error\": \"{}\"}}", e),
102            }
103        }
104        0x0001 => {
105            match decode_update_possible(tlv_value) {
106                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
107                Err(e) => format!("{{\"error\": \"{}\"}}", e),
108            }
109        }
110        0x0002 => {
111            match decode_update_state(tlv_value) {
112                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
113                Err(e) => format!("{{\"error\": \"{}\"}}", e),
114            }
115        }
116        0x0003 => {
117            match decode_update_state_progress(tlv_value) {
118                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
119                Err(e) => format!("{{\"error\": \"{}\"}}", e),
120            }
121        }
122        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
123    }
124}
125
126/// Get list of all attributes supported by this cluster
127/// 
128/// # Returns
129/// Vector of tuples containing (attribute_id, attribute_name)
130pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
131    vec![
132        (0x0000, "DefaultOTAProviders"),
133        (0x0001, "UpdatePossible"),
134        (0x0002, "UpdateState"),
135        (0x0003, "UpdateStateProgress"),
136    ]
137}
138